Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cancel zero address as the default from address #1073

Merged
merged 4 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 2 additions & 36 deletions web3/packages/api-server/src/base/gw-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils, HexNumber, Script, HexString, Hash } from "@ckb-lumos/base";
import { utils, HexNumber, Script, HexString } from "@ckb-lumos/base";
import {
BackendInfo,
EoaScript,
Expand All @@ -15,11 +15,10 @@ import {
GodwokenClient,
NodeInfo as GwNodeInfo,
} from "@godwoken-web3/godwoken";
import { CKB_SUDT_ID, ZERO_ETH_ADDRESS } from "../methods/constant";
import { CKB_SUDT_ID } from "../methods/constant";
import { Uint32 } from "./types/uint";
import { snakeToCamel } from "../util";
import { EntryPointContract } from "../gasless/entrypoint";
import { BaseEthRegistryAddress } from "./base-eth-registry-address";
import { logger } from "./logger";

// source: https://github.com/nervosnetwork/godwoken/commit/d6c98d8f8a199b6ec29bc77c5065c1108220bb0a#diff-c56fda2ca3b1366049c88e633389d9b6faa8366151369fd7314c81f6e389e5c7R5
Expand Down Expand Up @@ -232,16 +231,10 @@ export class GwConfig {
return new Account(regIdHex, scriptHash);
}

// Using zero address firstly
// we search the first account id = 3, if it is eoa account, use it, otherwise continue with id + 1;
private async fetchDefaultFromAccount(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment // Using zero address firstly could be deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

registryId: number
): Promise<AccountWithAddress> {
const zeroAddressInfo = await zeroAddressAccount(this.rpc, registryId);
if (zeroAddressInfo != null) {
return zeroAddressInfo;
}

const ethAccountLockTypeHash = this.nodeInfo.eoaScripts.find(
(s) => s.eoaType === EoaScriptType.Eth
)?.typeHash;
Expand Down Expand Up @@ -437,33 +430,6 @@ function toApiNodeInfo(nodeInfo: GwNodeInfo): NodeInfo {
return snakeToCamel(nodeInfo, ["code_hash", "hash_type"]);
}

async function zeroAddressAccount(
rpc: GodwokenClient,
registryId: number
): Promise<AccountWithAddress | undefined> {
const registryAddress = new BaseEthRegistryAddress(
ZERO_ETH_ADDRESS,
registryId
);
const scriptHash: Hash | undefined = await rpc.getScriptHashByRegistryAddress(
registryAddress.serialize()
);
if (scriptHash == null) {
return undefined;
}

const id = await rpc.getAccountIdByScriptHash(scriptHash);
if (id == null) {
return undefined;
}

return {
id: "0x" + id.toString(16),
scriptHash,
address: ZERO_ETH_ADDRESS,
};
}

async function findFirstEoaAccountId(
rpc: GodwokenClient,
ethAccountLockTypeHash: HexString,
Expand Down
21 changes: 19 additions & 2 deletions web3/packages/api-server/tests/utils/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ mockRpc.getAccountIdByScriptHash = async (scriptHash: HexString) => {
}
};

mockRpc.getRegistryAddressByScriptHash = async (
scriptHash: HexString,
registryId: number
) => {
switch (scriptHash) {
case "0x5df8df09ec23819836b888f575ca4154a2af1f1d4720bca91a5fc9f5f7d9921f":
return {
registry_id: "0x" + registryId.toString(16),
address: "0xFb2C72d3ffe10Ef7c9960272859a23D24db9e04A",
};

default:
throw new Error(
`getRegistryAddressByScriptHash not mock for script hash ${scriptHash}`
);
}
};

mockRpc.getScriptHash = async (accountId: number) => {
switch (accountId) {
case 4:
Expand Down Expand Up @@ -211,8 +229,7 @@ test("init gw config", async (t) => {
},
});
t.is(config.accounts.polyjuiceCreator.id, "0x4");
// Using zero address as default address
t.is(config.accounts.defaultFrom.id, "0x5");
t.is(config.accounts.defaultFrom.id, "0x3");
t.is(config.accounts.ethAddrReg.id, "0x2");
t.is(config.nodeMode, NodeMode.FullNode);
t.is(config.web3ChainId, "0x116e8");
Expand Down