From 3a046706343f2d4ef39b1209e5a3d1f6045af719 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Tue, 24 Aug 2021 21:32:17 +0800 Subject: [PATCH 01/13] fix malicious mapping --- .../api-server/src/methods/modules/poly.ts | 119 +++++++++++++++++- 1 file changed, 115 insertions(+), 4 deletions(-) diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 28bd4e74..0ea25eb8 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -1,16 +1,36 @@ import { middleware, validators } from "../validator"; -import { Hash, HexNumber, Address } from "@ckb-lumos/base"; +import { Hash, HexNumber, Address, HexString } from "@ckb-lumos/base"; import { toHexNumber } from "../../base/types/uint"; import { envConfig } from "../../base/env-config"; -import { InternalError, InvalidParamsError, Web3Error } from "../error"; +import { + InternalError, + InvalidParamsError, + RpcError, + Web3Error, +} from "../error"; import { Query } from "../../db"; import { isAddressMatch } from "../../base/address"; +import { RPC } from "ckb-js-toolkit"; +import { GW_RPC_REQUEST_ERROR } from "../error-code"; +import { + deserializeL2TransactionWithAddressMapping, + deserializeRawL2TransactionWithAddressMapping, + serializeL2Transaction, + serializeRawL2Transaction, +} from "@polyjuice-provider/base"; +import { + AddressMapping, + L2TransactionWithAddressMapping, + RawL2TransactionWithAddressMapping, +} from "@polyjuice-provider/godwoken/lib/addressTypes"; export class Poly { private query: Query; + private rpc: RPC; constructor() { this.query = new Query(envConfig.databaseUrl); + this.rpc = new RPC(envConfig.godwokenJsonRpc); this.getEthAddressByGodwokenShortAddress = middleware( this.getEthAddressByGodwokenShortAddress.bind(this), @@ -50,14 +70,52 @@ export class Poly { } } + async submitL2Transaction(args: any[]) { + try { + const data = args[0]; + const txWithAddressMapping: L2TransactionWithAddressMapping = + deserializeL2TransactionWithAddressMapping(data); + const l2_tx = serializeL2Transaction(txWithAddressMapping.tx); + const result = await this.rpc.gw_submit_l2transaction(l2_tx); + // if result is fine, then tx is legal, we can start thinking to store the address mapping + // todo: check in eth tx data, not args + this.saveAddressMappingToDb( + txWithAddressMapping.addresses, + txWithAddressMapping.tx.raw.args + ); + return result; + } catch (error) { + parseError(error); + } + } + + async executeRawL2Transaction(args: any[]) { + try { + const data = args[0]; + const txWithAddressMapping: RawL2TransactionWithAddressMapping = + deserializeRawL2TransactionWithAddressMapping(data); + const raw_l2_tx = serializeRawL2Transaction(txWithAddressMapping.raw_tx); + const result = await this.rpc.gw_execute_raw_l2transaction(raw_l2_tx); + // if result is fine, then tx is legal, we can start thinking to store the address mapping + // todo: check in eth tx data, not args + this.saveAddressMappingToDb( + txWithAddressMapping.addresses, + txWithAddressMapping.raw_tx.args + ); + return result; + } catch (error) { + parseError(error); + } + } + async saveEthAddressGodwokenShortAddressMapping( args: [string, string] ): Promise { try { const ethAddress = args[0]; const godwokenShortAddress = args[1]; - // todo: save before check if it not exsit; - // TODO: check exists + + // check if it exist const exists = await this.query.accounts.exists( ethAddress, godwokenShortAddress @@ -141,4 +199,57 @@ export class Poly { throw new Web3Error(error.message); } } + + private async saveAddressMappingToDb( + addressMapping: AddressMapping, + ethTxData: HexString + ) { + addressMapping.data.forEach(async (item) => { + const ethAddress: HexString = item.eth_address; + const godwokenShortAddress: HexString = item.gw_short_address; + + if (!ethTxData.includes(godwokenShortAddress)) { + // the short address not in eth tx data param, don't save. + return; + } + + try { + const exists = await this.query.accounts.exists( + ethAddress, + godwokenShortAddress + ); + if (exists) { + return; + } + if (!isAddressMatch(ethAddress, godwokenShortAddress)) { + throw new Error( + "eth_address and godwoken_short_address unmatched! abort saving!" + ); + } + + await this.query.accounts.save(ethAddress, godwokenShortAddress); + console.log( + `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` + ); + return; + } catch (error) { + console.log( + `abort saving addressMapping [${godwokenShortAddress}]: ${ethAddress} , will keep saving the rest. =>`, + error + ); + } + }); + } +} + +function parseError(error: any): void { + const prefix = "JSONRPCError: server error "; + let message: string = error.message; + if (message.startsWith(prefix)) { + const jsonErr = message.slice(prefix.length); + const err = JSON.parse(jsonErr); + throw new RpcError(err.code, err.message); + } + + throw new RpcError(GW_RPC_REQUEST_ERROR, error.message); } From 182bc8d528a49d1207cbbea2ff33a0af82932e01 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Wed, 25 Aug 2021 16:08:24 +0800 Subject: [PATCH 02/13] fix: check address mapping in eth tx data --- .../api-server/src/methods/modules/eth.ts | 10 +++-- .../api-server/src/methods/modules/poly.ts | 41 ++++++++++--------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/api-server/src/methods/modules/eth.ts b/packages/api-server/src/methods/modules/eth.ts index bf855c9d..3922cb63 100644 --- a/packages/api-server/src/methods/modules/eth.ts +++ b/packages/api-server/src/methods/modules/eth.ts @@ -45,7 +45,7 @@ import { EthTransactionReceipt, } from "../../base/types/api"; import { filterWeb3Transaction } from "../../filter-web3-tx"; -import { Abi } from "@polyjuice-provider/base"; +import { Abi, ShortAddress, ShortAddressType } from "@polyjuice-provider/base"; import { SUDT_ERC20_PROXY_ABI, allowedAddresses } from "../../erc20"; import { FilterManager } from "../../cache"; import { toHex } from "../../util"; @@ -1078,9 +1078,13 @@ async function ethCallTx( } const abi = new Abi(SUDT_ERC20_PROXY_ABI); - const ethToGwAddr = async (addr: HexString): Promise => { + // TODO: save addressMapping into db when encounter not-exist-eth-eoa-address + const ethToGwAddr = async (addr: HexString): Promise => { const result = await allTypeEthAddressToShortAddress(rpc, addr); - return result!; + return { + value: result!, + type: ShortAddressType.eoaAddress, // TODO: return correct address type + }; }; // TODO: find by db.addresses when not found diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 0ea25eb8..99ff3745 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -13,13 +13,13 @@ import { isAddressMatch } from "../../base/address"; import { RPC } from "ckb-js-toolkit"; import { GW_RPC_REQUEST_ERROR } from "../error-code"; import { + decodeArgs, deserializeL2TransactionWithAddressMapping, deserializeRawL2TransactionWithAddressMapping, serializeL2Transaction, serializeRawL2Transaction, } from "@polyjuice-provider/base"; import { - AddressMapping, L2TransactionWithAddressMapping, RawL2TransactionWithAddressMapping, } from "@polyjuice-provider/godwoken/lib/addressTypes"; @@ -75,14 +75,10 @@ export class Poly { const data = args[0]; const txWithAddressMapping: L2TransactionWithAddressMapping = deserializeL2TransactionWithAddressMapping(data); - const l2_tx = serializeL2Transaction(txWithAddressMapping.tx); - const result = await this.rpc.gw_submit_l2transaction(l2_tx); + const l2Tx = serializeL2Transaction(txWithAddressMapping.tx); + const result = await this.rpc.gw_submit_l2transaction(l2Tx); // if result is fine, then tx is legal, we can start thinking to store the address mapping - // todo: check in eth tx data, not args - this.saveAddressMappingToDb( - txWithAddressMapping.addresses, - txWithAddressMapping.tx.raw.args - ); + this.saveAddressMapping(txWithAddressMapping); return result; } catch (error) { parseError(error); @@ -94,14 +90,10 @@ export class Poly { const data = args[0]; const txWithAddressMapping: RawL2TransactionWithAddressMapping = deserializeRawL2TransactionWithAddressMapping(data); - const raw_l2_tx = serializeRawL2Transaction(txWithAddressMapping.raw_tx); - const result = await this.rpc.gw_execute_raw_l2transaction(raw_l2_tx); + const rawL2Tx = serializeRawL2Transaction(txWithAddressMapping.raw_tx); + const result = await this.rpc.gw_execute_raw_l2transaction(rawL2Tx); // if result is fine, then tx is legal, we can start thinking to store the address mapping - // todo: check in eth tx data, not args - this.saveAddressMappingToDb( - txWithAddressMapping.addresses, - txWithAddressMapping.raw_tx.args - ); + this.saveAddressMapping(txWithAddressMapping); return result; } catch (error) { parseError(error); @@ -200,16 +192,25 @@ export class Poly { } } - private async saveAddressMappingToDb( - addressMapping: AddressMapping, - ethTxData: HexString + private async saveAddressMapping( + txWithAddressMapping: + | L2TransactionWithAddressMapping + | RawL2TransactionWithAddressMapping ) { - addressMapping.data.forEach(async (item) => { + const polyjuice_args = //@ts-ignore + txWithAddressMapping.tx.raw.args || txWithAddressMapping.raw_tx.args; + const ethTxData = decodeArgs(polyjuice_args).data; + + txWithAddressMapping.addresses.data.forEach(async (item) => { const ethAddress: HexString = item.eth_address; const godwokenShortAddress: HexString = item.gw_short_address; if (!ethTxData.includes(godwokenShortAddress)) { + // TODO: decode txData with abi // the short address not in eth tx data param, don't save. + console.log( + `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the eth tx data.` + ); return; } @@ -223,7 +224,7 @@ export class Poly { } if (!isAddressMatch(ethAddress, godwokenShortAddress)) { throw new Error( - "eth_address and godwoken_short_address unmatched! abort saving!" + `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` ); } From 25b0f9a29aeb3d2d20e182e678e869b738fe240e Mon Sep 17 00:00:00 2001 From: RetricSu Date: Wed, 25 Aug 2021 16:59:59 +0800 Subject: [PATCH 03/13] fix: only save address not on chain --- packages/api-server/src/base/address.ts | 13 ++ .../api-server/src/methods/modules/poly.ts | 114 ++++++++++-------- 2 files changed, 76 insertions(+), 51 deletions(-) diff --git a/packages/api-server/src/base/address.ts b/packages/api-server/src/base/address.ts index 97add62f..a9368128 100644 --- a/packages/api-server/src/base/address.ts +++ b/packages/api-server/src/base/address.ts @@ -48,3 +48,16 @@ export function isAddressMatch( const computedShortAddress = ethAddressToShortAddress(ethAddress); return shortAddress === computedShortAddress; } + +export async function isShortAddressOnChain( + godwokenClient: GodwokenClient, + shortAddress: HexString +) { + const scriptHash = await godwokenClient.getScriptHashByShortAddress( + shortAddress + ); + if (scriptHash == null) { + return false; + } + return true; +} diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 99ff3745..7d52fe97 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -9,28 +9,26 @@ import { Web3Error, } from "../error"; import { Query } from "../../db"; -import { isAddressMatch } from "../../base/address"; -import { RPC } from "ckb-js-toolkit"; +import { isAddressMatch, isShortAddressOnChain } from "../../base/address"; import { GW_RPC_REQUEST_ERROR } from "../error-code"; import { decodeArgs, deserializeL2TransactionWithAddressMapping, deserializeRawL2TransactionWithAddressMapping, - serializeL2Transaction, - serializeRawL2Transaction, } from "@polyjuice-provider/base"; import { L2TransactionWithAddressMapping, RawL2TransactionWithAddressMapping, } from "@polyjuice-provider/godwoken/lib/addressTypes"; +import { GodwokenClient } from "@godwoken-web3/godwoken"; export class Poly { private query: Query; - private rpc: RPC; + private rpc: GodwokenClient; constructor() { this.query = new Query(envConfig.databaseUrl); - this.rpc = new RPC(envConfig.godwokenJsonRpc); + this.rpc = new GodwokenClient(envConfig.godwokenJsonRpc); this.getEthAddressByGodwokenShortAddress = middleware( this.getEthAddressByGodwokenShortAddress.bind(this), @@ -75,10 +73,10 @@ export class Poly { const data = args[0]; const txWithAddressMapping: L2TransactionWithAddressMapping = deserializeL2TransactionWithAddressMapping(data); - const l2Tx = serializeL2Transaction(txWithAddressMapping.tx); - const result = await this.rpc.gw_submit_l2transaction(l2Tx); + const l2Tx = txWithAddressMapping.tx; + const result = await this.rpc.submitL2Transaction(l2Tx); // if result is fine, then tx is legal, we can start thinking to store the address mapping - this.saveAddressMapping(txWithAddressMapping); + saveAddressMapping(this.query, this.rpc, txWithAddressMapping); return result; } catch (error) { parseError(error); @@ -90,10 +88,10 @@ export class Poly { const data = args[0]; const txWithAddressMapping: RawL2TransactionWithAddressMapping = deserializeRawL2TransactionWithAddressMapping(data); - const rawL2Tx = serializeRawL2Transaction(txWithAddressMapping.raw_tx); - const result = await this.rpc.gw_execute_raw_l2transaction(rawL2Tx); + const rawL2Tx = txWithAddressMapping.raw_tx; + const result = await this.rpc.executeRawL2Transaction(rawL2Tx); // if result is fine, then tx is legal, we can start thinking to store the address mapping - this.saveAddressMapping(txWithAddressMapping); + saveAddressMapping(this.query, this.rpc, txWithAddressMapping); return result; } catch (error) { parseError(error); @@ -103,6 +101,7 @@ export class Poly { async saveEthAddressGodwokenShortAddressMapping( args: [string, string] ): Promise { + // TODO: remove this function when provider is migrate to new version try { const ethAddress = args[0]; const godwokenShortAddress = args[1]; @@ -191,56 +190,69 @@ export class Poly { throw new Web3Error(error.message); } } +} - private async saveAddressMapping( - txWithAddressMapping: - | L2TransactionWithAddressMapping - | RawL2TransactionWithAddressMapping - ) { - const polyjuice_args = //@ts-ignore - txWithAddressMapping.tx.raw.args || txWithAddressMapping.raw_tx.args; - const ethTxData = decodeArgs(polyjuice_args).data; +async function saveAddressMapping( + query: Query, + rpc: GodwokenClient, + txWithAddressMapping: + | L2TransactionWithAddressMapping + | RawL2TransactionWithAddressMapping +) { + let raw_tx; + if ("raw_tx" in txWithAddressMapping) { + raw_tx = txWithAddressMapping.raw_tx; + } else { + raw_tx = txWithAddressMapping.tx.raw; + } + const polyjuice_args = raw_tx.args; + const ethTxData = decodeArgs(polyjuice_args).data; - txWithAddressMapping.addresses.data.forEach(async (item) => { - const ethAddress: HexString = item.eth_address; - const godwokenShortAddress: HexString = item.gw_short_address; + txWithAddressMapping.addresses.data.forEach(async (item) => { + const ethAddress: HexString = item.eth_address; + const godwokenShortAddress: HexString = item.gw_short_address; - if (!ethTxData.includes(godwokenShortAddress)) { - // TODO: decode txData with abi - // the short address not in eth tx data param, don't save. - console.log( - `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the eth tx data.` - ); + if (!ethTxData.includes(godwokenShortAddress)) { + // TODO: decode txData with abi, and check address with abi + console.log( + `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the eth tx data.` + ); + return; + } + + try { + const exists = await query.accounts.exists( + ethAddress, + godwokenShortAddress + ); + if (exists) { return; } - - try { - const exists = await this.query.accounts.exists( - ethAddress, - godwokenShortAddress + if (!isAddressMatch(ethAddress, godwokenShortAddress)) { + throw new Error( + `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` ); - if (exists) { - return; - } - if (!isAddressMatch(ethAddress, godwokenShortAddress)) { - throw new Error( - `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` - ); - } + } - await this.query.accounts.save(ethAddress, godwokenShortAddress); + if (isShortAddressOnChain(rpc, godwokenShortAddress)) { console.log( - `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` + `abort saving, since godwoken_short_address ${godwokenShortAddress} is already on chain.` ); return; - } catch (error) { - console.log( - `abort saving addressMapping [${godwokenShortAddress}]: ${ethAddress} , will keep saving the rest. =>`, - error - ); } - }); - } + + await query.accounts.save(ethAddress, godwokenShortAddress); + console.log( + `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` + ); + return; + } catch (error) { + console.log( + `abort saving addressMapping [${godwokenShortAddress}]: ${ethAddress} , will keep saving the rest. =>`, + error + ); + } + }); } function parseError(error: any): void { From 5a60a57310ad5debe872bfd0728f1a25b57abc48 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Thu, 26 Aug 2021 14:40:56 +0800 Subject: [PATCH 04/13] fix: db account exist --- packages/api-server/src/db/accounts.ts | 5 ++--- packages/api-server/src/methods/modules/poly.ts | 14 ++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/api-server/src/db/accounts.ts b/packages/api-server/src/db/accounts.ts index 06ac285d..c2b25a4e 100644 --- a/packages/api-server/src/db/accounts.ts +++ b/packages/api-server/src/db/accounts.ts @@ -46,10 +46,9 @@ export class AccountsQuery { shortAddress: HexString ): Promise { const result = await this.knex(ACCOUNTS_TABLE_NAME) - .where("eth_address", ethAddress) - .orWhere("gw_short_address", shortAddress) + .where("eth_address", toBuffer(ethAddress)) + .orWhere("gw_short_address", toBuffer(shortAddress)) .first(); - return result != null; } diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 7d52fe97..faae4990 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -76,7 +76,7 @@ export class Poly { const l2Tx = txWithAddressMapping.tx; const result = await this.rpc.submitL2Transaction(l2Tx); // if result is fine, then tx is legal, we can start thinking to store the address mapping - saveAddressMapping(this.query, this.rpc, txWithAddressMapping); + await saveAddressMapping(this.query, this.rpc, txWithAddressMapping); return result; } catch (error) { parseError(error); @@ -91,7 +91,7 @@ export class Poly { const rawL2Tx = txWithAddressMapping.raw_tx; const result = await this.rpc.executeRawL2Transaction(rawL2Tx); // if result is fine, then tx is legal, we can start thinking to store the address mapping - saveAddressMapping(this.query, this.rpc, txWithAddressMapping); + await saveAddressMapping(this.query, this.rpc, txWithAddressMapping); return result; } catch (error) { parseError(error); @@ -207,12 +207,11 @@ async function saveAddressMapping( } const polyjuice_args = raw_tx.args; const ethTxData = decodeArgs(polyjuice_args).data; - txWithAddressMapping.addresses.data.forEach(async (item) => { const ethAddress: HexString = item.eth_address; const godwokenShortAddress: HexString = item.gw_short_address; - if (!ethTxData.includes(godwokenShortAddress)) { + if (!ethTxData.includes(godwokenShortAddress.slice(2))) { // TODO: decode txData with abi, and check address with abi console.log( `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the eth tx data.` @@ -226,6 +225,9 @@ async function saveAddressMapping( godwokenShortAddress ); if (exists) { + console.log( + `abort saving, since godwoken_short_address ${godwokenShortAddress} is already saved on database.` + ); return; } if (!isAddressMatch(ethAddress, godwokenShortAddress)) { @@ -233,8 +235,8 @@ async function saveAddressMapping( `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` ); } - - if (isShortAddressOnChain(rpc, godwokenShortAddress)) { + const isExistOnChain = await isShortAddressOnChain(rpc, godwokenShortAddress); + if (isExistOnChain) { console.log( `abort saving, since godwoken_short_address ${godwokenShortAddress} is already on chain.` ); From 8125671a084cce285721ff2c6c9f2c5535b01c45 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Thu, 26 Aug 2021 14:43:54 +0800 Subject: [PATCH 05/13] chore: fm --- packages/api-server/src/methods/modules/poly.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index faae4990..ef0cb63e 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -235,7 +235,10 @@ async function saveAddressMapping( `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` ); } - const isExistOnChain = await isShortAddressOnChain(rpc, godwokenShortAddress); + const isExistOnChain = await isShortAddressOnChain( + rpc, + godwokenShortAddress + ); if (isExistOnChain) { console.log( `abort saving, since godwoken_short_address ${godwokenShortAddress} is already on chain.` From 2fb525019369700abc83371148c2528ad0663aee Mon Sep 17 00:00:00 2001 From: RetricSu Date: Thu, 26 Aug 2021 16:57:15 +0800 Subject: [PATCH 06/13] fix: remove poly_saveEthAddressGodwokenShortAddressMapping method --- .../api-server/src/methods/modules/poly.ts | 33 ++----------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index ef0cb63e..1eaa93be 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -101,36 +101,9 @@ export class Poly { async saveEthAddressGodwokenShortAddressMapping( args: [string, string] ): Promise { - // TODO: remove this function when provider is migrate to new version - try { - const ethAddress = args[0]; - const godwokenShortAddress = args[1]; - - // check if it exist - const exists = await this.query.accounts.exists( - ethAddress, - godwokenShortAddress - ); - if (exists) { - return "ok"; - } - - if (!isAddressMatch(ethAddress, godwokenShortAddress)) { - throw new Error( - "eth_address and godwoken_short_address unmatched! abort saving!" - ); - } - - await this.query.accounts.save(ethAddress, godwokenShortAddress); - - console.log( - `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` - ); - return "ok"; - } catch (error) { - console.log(error); - throw new InvalidParamsError(error.message); - } + throw new Web3Error( + "this method is deprecated! please upgrade @polyjuice-provider over 0.0.1-rc9 version! see: https://www.npmjs.com/org/polyjuice-provider" + ); } async getCreatorId(_args: []): Promise { From b8b8bdd479540aa18b87ba46dccd7ff3a8b80f53 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Fri, 27 Aug 2021 15:49:14 +0800 Subject: [PATCH 07/13] fix: rename abi.get_interested_abi_item_by_encoded_data --- packages/api-server/src/methods/modules/eth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-server/src/methods/modules/eth.ts b/packages/api-server/src/methods/modules/eth.ts index 3922cb63..792a1df8 100644 --- a/packages/api-server/src/methods/modules/eth.ts +++ b/packages/api-server/src/methods/modules/eth.ts @@ -1118,7 +1118,7 @@ async function ethCallTx( blockNumber ); - const abiItem = abi.get_intereted_abi_item_by_encoded_data(data); + const abiItem = abi.get_interested_abi_item_by_encoded_data(data); if (abiItem && isEthWallet) { const returnDataWithShortAddress = From 7f9a94b148ea84e8ef638f81535911f64dc69e54 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Mon, 30 Aug 2021 19:15:00 +0800 Subject: [PATCH 08/13] fix: check abiItem for addressMappinp --- .../api-server/src/methods/modules/eth.ts | 1 + .../api-server/src/methods/modules/poly.ts | 50 ++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/api-server/src/methods/modules/eth.ts b/packages/api-server/src/methods/modules/eth.ts index 792a1df8..b8c52dd3 100644 --- a/packages/api-server/src/methods/modules/eth.ts +++ b/packages/api-server/src/methods/modules/eth.ts @@ -1078,6 +1078,7 @@ async function ethCallTx( } const abi = new Abi(SUDT_ERC20_PROXY_ABI); + // TODO: save addressMapping into db when encounter not-exist-eth-eoa-address const ethToGwAddr = async (addr: HexString): Promise => { const result = await allTypeEthAddressToShortAddress(rpc, addr); diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 1eaa93be..76c5cbab 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -15,7 +15,11 @@ import { decodeArgs, deserializeL2TransactionWithAddressMapping, deserializeRawL2TransactionWithAddressMapping, + deserializeAbiItem, + getAddressesFromInputDataByAbi, + EMPTY_ABI_ITEM_SERIALIZE_STR, } from "@polyjuice-provider/base"; +import { AbiItem } from "@polyjuice-provider/godwoken/lib/abiTypes"; import { L2TransactionWithAddressMapping, RawL2TransactionWithAddressMapping, @@ -172,22 +176,52 @@ async function saveAddressMapping( | L2TransactionWithAddressMapping | RawL2TransactionWithAddressMapping ) { - let raw_tx; + console.log(JSON.stringify(txWithAddressMapping, null, 2)); + + if ( + txWithAddressMapping.addresses.length === "0x0" || + txWithAddressMapping.addresses.data.length === 0 + ) { + console.log(`empty addressMapping, abort saving.`); + return; + } + + if (txWithAddressMapping.extra === EMPTY_ABI_ITEM_SERIALIZE_STR) { + console.log(`addressMapping without abiItem, abort saving.`); + return; + } + + let rawTx; if ("raw_tx" in txWithAddressMapping) { - raw_tx = txWithAddressMapping.raw_tx; + rawTx = txWithAddressMapping.raw_tx; } else { - raw_tx = txWithAddressMapping.tx.raw; + rawTx = txWithAddressMapping.tx.raw; + } + const ethTxData = decodeArgs(rawTx.args).data; + const abiItemStr = txWithAddressMapping.extra; + const abiItem: AbiItem = deserializeAbiItem(abiItemStr); + const addressesFromEthTxData = getAddressesFromInputDataByAbi( + ethTxData, + abiItem + ); + if (addressesFromEthTxData.length === 0) { + console.log( + `eth tx data ${ethTxData} contains no valid address, abort saving.` + ); + return; } - const polyjuice_args = raw_tx.args; - const ethTxData = decodeArgs(polyjuice_args).data; + txWithAddressMapping.addresses.data.forEach(async (item) => { const ethAddress: HexString = item.eth_address; const godwokenShortAddress: HexString = item.gw_short_address; - if (!ethTxData.includes(godwokenShortAddress.slice(2))) { - // TODO: decode txData with abi, and check address with abi + if (!addressesFromEthTxData.includes(godwokenShortAddress)) { console.log( - `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the eth tx data.` + `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the ethTxData. expected addresses: ${JSON.stringify( + addressesFromEthTxData, + null, + 2 + )}` ); return; } From a368c79eaa421b0e2397d766c60450799933b8ae Mon Sep 17 00:00:00 2001 From: RetricSu Date: Mon, 30 Aug 2021 19:26:00 +0800 Subject: [PATCH 09/13] chore: update version tips rc10 --- packages/api-server/src/methods/modules/poly.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 76c5cbab..ccdc2b3f 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -106,7 +106,7 @@ export class Poly { args: [string, string] ): Promise { throw new Web3Error( - "this method is deprecated! please upgrade @polyjuice-provider over 0.0.1-rc9 version! see: https://www.npmjs.com/org/polyjuice-provider" + "this method is deprecated! please upgrade @polyjuice-provider over 0.0.1-rc10 version! see: https://www.npmjs.com/org/polyjuice-provider" ); } From be1b52015ee0ce202231f73f3e534f834880f429 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Tue, 31 Aug 2021 13:32:49 +0800 Subject: [PATCH 10/13] fix: apply review suggestion --- .../api-server/src/methods/modules/poly.ts | 90 ++++++++++--------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index ccdc2b3f..2c655f91 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -211,60 +211,62 @@ async function saveAddressMapping( return; } - txWithAddressMapping.addresses.data.forEach(async (item) => { - const ethAddress: HexString = item.eth_address; - const godwokenShortAddress: HexString = item.gw_short_address; + await Promise.all( + txWithAddressMapping.addresses.data.map(async (item) => { + const ethAddress: HexString = item.eth_address; + const godwokenShortAddress: HexString = item.gw_short_address; - if (!addressesFromEthTxData.includes(godwokenShortAddress)) { - console.log( - `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the ethTxData. expected addresses: ${JSON.stringify( - addressesFromEthTxData, - null, - 2 - )}` - ); - return; - } - - try { - const exists = await query.accounts.exists( - ethAddress, - godwokenShortAddress - ); - if (exists) { + if (!addressesFromEthTxData.includes(godwokenShortAddress)) { console.log( - `abort saving, since godwoken_short_address ${godwokenShortAddress} is already saved on database.` + `illegal address mapping, since godwoken_short_address ${godwokenShortAddress} is not in the ethTxData. expected addresses: ${JSON.stringify( + addressesFromEthTxData, + null, + 2 + )}` ); return; } - if (!isAddressMatch(ethAddress, godwokenShortAddress)) { - throw new Error( - `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` + + try { + const exists = await query.accounts.exists( + ethAddress, + godwokenShortAddress ); - } - const isExistOnChain = await isShortAddressOnChain( - rpc, - godwokenShortAddress - ); - if (isExistOnChain) { + if (exists) { + console.log( + `abort saving, since godwoken_short_address ${godwokenShortAddress} is already saved on database.` + ); + return; + } + if (!isAddressMatch(ethAddress, godwokenShortAddress)) { + throw new Error( + `eth_address ${ethAddress} and godwoken_short_address ${godwokenShortAddress} unmatched! abort saving!` + ); + } + const isExistOnChain = await isShortAddressOnChain( + rpc, + godwokenShortAddress + ); + if (isExistOnChain) { + console.log( + `abort saving, since godwoken_short_address ${godwokenShortAddress} is already on chain.` + ); + return; + } + + await query.accounts.save(ethAddress, godwokenShortAddress); console.log( - `abort saving, since godwoken_short_address ${godwokenShortAddress} is already on chain.` + `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` ); return; + } catch (error) { + console.log( + `abort saving addressMapping [${godwokenShortAddress}]: ${ethAddress} , will keep saving the rest. =>`, + error + ); } - - await query.accounts.save(ethAddress, godwokenShortAddress); - console.log( - `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` - ); - return; - } catch (error) { - console.log( - `abort saving addressMapping [${godwokenShortAddress}]: ${ethAddress} , will keep saving the rest. =>`, - error - ); - } - }); + }) + ); } function parseError(error: any): void { From da09ea78c3582c1bd981c4b14d0addbda031a362 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Mon, 6 Sep 2021 14:31:52 +0800 Subject: [PATCH 11/13] chore: re-open poly_saveEthAddressGodwokenShortAddressMapping --- .../api-server/src/methods/modules/poly.ts | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index 2c655f91..90cee99c 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -105,9 +105,39 @@ export class Poly { async saveEthAddressGodwokenShortAddressMapping( args: [string, string] ): Promise { - throw new Web3Error( - "this method is deprecated! please upgrade @polyjuice-provider over 0.0.1-rc10 version! see: https://www.npmjs.com/org/polyjuice-provider" - ); + // TODO: remove this function later + // throw new Web3Error( + // "this method is deprecated! please upgrade @polyjuice-provider over 0.0.1-rc10 version! see: https://www.npmjs.com/org/polyjuice-provider" + // ); + try { + const ethAddress = args[0]; + const godwokenShortAddress = args[1]; + + // check if it exist + const exists = await this.query.accounts.exists( + ethAddress, + godwokenShortAddress + ); + if (exists) { + return "ok"; + } + + if (!isAddressMatch(ethAddress, godwokenShortAddress)) { + throw new Error( + "eth_address and godwoken_short_address unmatched! abort saving!" + ); + } + + await this.query.accounts.save(ethAddress, godwokenShortAddress); + + console.log( + `poly_save: insert one record, [${godwokenShortAddress}]: ${ethAddress}` + ); + return "ok"; + } catch (error) { + console.log(error); + throw new InvalidParamsError(error.message); + } } async getCreatorId(_args: []): Promise { From da917f9f1c69c9f228783071ec49a9f851b49230 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Tue, 7 Sep 2021 13:20:31 +0800 Subject: [PATCH 12/13] chore: bump polyjuice-provider 0.0.1-rc10 --- packages/api-server/package.json | 2 +- yarn.lock | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/api-server/package.json b/packages/api-server/package.json index 114aa06f..b6110a67 100644 --- a/packages/api-server/package.json +++ b/packages/api-server/package.json @@ -17,7 +17,7 @@ "dependencies": { "@ckb-lumos/base": "^0.16.0", "@godwoken-web3/godwoken": "0.6.0-rc5", - "@polyjuice-provider/base": "^0.0.1-rc7", + "@polyjuice-provider/base": "^0.0.1-rc10", "@sentry/node": "^6.11.0", "blake2b": "2.1.3", "ckb-js-toolkit": "^0.10.2", diff --git a/yarn.lock b/yarn.lock index 8ef35d4a..3ec0e190 100644 --- a/yarn.lock +++ b/yarn.lock @@ -289,13 +289,13 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@polyjuice-provider/base@^0.0.1-rc7": - version "0.0.1-rc7" - resolved "https://registry.yarnpkg.com/@polyjuice-provider/base/-/base-0.0.1-rc7.tgz#5df16cccc178f049192139758ecd7852e4529480" - integrity sha512-wFwDBf2xpDBySAjBKI0A1ugfJ8398MW+LiLPQiJoY1Ca1e85+I6OMYCjQGGGYB2gbALWKwAcplin7RVbGiajPA== +"@polyjuice-provider/base@^0.0.1-rc10": + version "0.0.1-rc10" + resolved "https://registry.yarnpkg.com/@polyjuice-provider/base/-/base-0.0.1-rc10.tgz#23e8c74993eaaaa18a391a103e18f1aedcc369f4" + integrity sha512-2cGKXh2ST57qMlthL8tKVv5/JM+RtrJnIjifD1Sx7zWpKuFa+m6MWjwK+LUNWJzO3/6NKjhotIEMOqr3jI++HQ== dependencies: "@ckb-lumos/base" "^0.16.0" - "@polyjuice-provider/godwoken" "^0.0.1-rc7" + "@polyjuice-provider/godwoken" "^0.0.1-rc10" buffer "^6.0.3" encoding "^0.1.13" eth-sig-util "^3.0.1" @@ -304,10 +304,10 @@ web3 "^1.3.4" xhr2-cookies "^1.1.0" -"@polyjuice-provider/godwoken@^0.0.1-rc7": - version "0.0.1-rc7" - resolved "https://registry.yarnpkg.com/@polyjuice-provider/godwoken/-/godwoken-0.0.1-rc7.tgz#1c7ff81d92061adbbd1da51721d1545d20e4a2a1" - integrity sha512-jBQ1Ivf1TbcX935bUh+QPWwCdueQEsEmLzU6gU3/luTVNwBHHrNElwMDtoqrTVi0akA9CRMWoNMKzSTjNnTNNw== +"@polyjuice-provider/godwoken@^0.0.1-rc10": + version "0.0.1-rc10" + resolved "https://registry.yarnpkg.com/@polyjuice-provider/godwoken/-/godwoken-0.0.1-rc10.tgz#ca927aac16d80046440b278cdba7f08b85c5c832" + integrity sha512-8Em+jlKqaoDqFM3Ab+fax3dV6gZbMB0oAdsHgxyuiP0XmNBccZRA1PnHwgTzB1hcauqAz4J9IbO92tDY717Vfg== dependencies: "@ckb-lumos/base" "^0.16.0" ckb-js-toolkit "^0.9.3" From f5dd932aa8b8182008053960c73d4c5acc75a5a6 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Tue, 7 Sep 2021 13:30:46 +0800 Subject: [PATCH 13/13] chore: release 0.6.0-rc7 --- package.json | 2 +- packages/api-server/package.json | 4 ++-- packages/godwoken/package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a5606a85..d5e32fb0 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,6 @@ "eslint": "^7.16.0", "prettier": "^2.2.1" }, - "version": "0.6.0-rc6", + "version": "0.6.0-rc7", "author": "hupeng " } diff --git a/packages/api-server/package.json b/packages/api-server/package.json index f215f592..63637252 100644 --- a/packages/api-server/package.json +++ b/packages/api-server/package.json @@ -1,6 +1,6 @@ { "name": "@godwoken-web3/api-server", - "version": "0.6.0-rc6", + "version": "0.6.0-rc7", "private": true, "scripts": { "start": "concurrently \"tsc -w\" \"DEBUG=godwoken-web3-api:server nodemon ./bin/www\"", @@ -16,7 +16,7 @@ }, "dependencies": { "@ckb-lumos/base": "^0.16.0", - "@godwoken-web3/godwoken": "0.6.0-rc6", + "@godwoken-web3/godwoken": "0.6.0-rc7", "@newrelic/native-metrics": "^7.0.1", "@polyjuice-provider/base": "^0.0.1-rc10", "@sentry/node": "^6.11.0", diff --git a/packages/godwoken/package.json b/packages/godwoken/package.json index 44c5f590..620dc100 100644 --- a/packages/godwoken/package.json +++ b/packages/godwoken/package.json @@ -1,6 +1,6 @@ { "name": "@godwoken-web3/godwoken", - "version": "0.6.0-rc6", + "version": "0.6.0-rc7", "private": true, "main": "lib/index.js", "scripts": {