-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web3):
GwConfig
is not a constructor (#1019)
Fix `EthRegistryAddress` and `GwConfig` circular references > TypeError: gw_config_1.GwConfig is not a constructor
- Loading branch information
1 parent
001b6fe
commit 7e8e0be
Showing
4 changed files
with
68 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
web3/packages/api-server/src/base/base-eth-registry-address.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HexString } from "@ckb-lumos/base"; | ||
import { Uint32 } from "./types/uint"; | ||
|
||
// NOTE: Not to import `gwConfig` | ||
export class BaseEthRegistryAddress { | ||
private registryId: number; | ||
private addressByteSize: number = 20; | ||
public readonly address: HexString; | ||
|
||
// Using optional registryId when gwConfig not initialized | ||
constructor(address: HexString, registryId: number) { | ||
if (!address.startsWith("0x") || address.length !== 42) { | ||
throw new Error(`Eth address format error: ${address}`); | ||
} | ||
this.address = address.toLowerCase(); | ||
|
||
this.registryId = registryId; | ||
} | ||
|
||
public serialize(): HexString { | ||
return ( | ||
"0x" + | ||
new Uint32(this.registryId).toLittleEndian().slice(2) + | ||
new Uint32(this.addressByteSize).toLittleEndian().slice(2) + | ||
this.address.slice(2) | ||
); | ||
} | ||
|
||
public static Deserialize(hex: HexString): BaseEthRegistryAddress { | ||
const hexWithoutPrefix = hex.slice(2); | ||
const registryId: number = Uint32.fromLittleEndian( | ||
hexWithoutPrefix.slice(0, 8) | ||
).getValue(); | ||
const addressByteSize: number = Uint32.fromLittleEndian( | ||
hexWithoutPrefix.slice(8, 16) | ||
).getValue(); | ||
const address: HexString = hexWithoutPrefix.slice(16); | ||
if (addressByteSize !== 20 || address.length !== 40) { | ||
throw new Error(`Eth address deserialize error: ${hex}`); | ||
} | ||
return new BaseEthRegistryAddress("0x" + address, registryId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters