Skip to content

Commit e740faa

Browse files
authored
fix sui address type for original asset (#601)
1 parent a8a5914 commit e740faa

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

core/definitions/src/universalAddress.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class UniversalAddress implements Address {
2626
}
2727

2828
toNative<T extends Parameters<typeof toNative>[0]>(chainOrPlatform: T): NativeAddress<T> {
29-
return toNative(chainOrPlatform, this);
29+
return toNative(chainOrPlatform, this.toUint8Array());
3030
}
3131

3232
unwrap(): Uint8Array {
@@ -54,7 +54,7 @@ export class UniversalAddress implements Address {
5454
return (
5555
typeof address === "object" &&
5656
"constructor" in address &&
57-
address.constructor.type === "Universal"
57+
address.constructor.type === UniversalAddress.type
5858
);
5959
}
6060

platforms/evm/src/address.ts

+27-18
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,11 @@ export class EvmAddress implements Address {
3333

3434
this.address = getAddress(address);
3535
} else if (address instanceof Uint8Array) {
36-
if (address.length !== EvmAddress.byteSize)
37-
throw new Error(
38-
`Invalid EVM address, expected ${EvmAddress.byteSize} bytes but got ${address.length}`,
39-
);
40-
36+
address = this.trimUniversalAddress(address);
4137
this.address = getAddress(encoding.hex.encode(address));
4238
} else if (UniversalAddress.instanceof(address)) {
43-
// If its a universal address and we want it to be an ethereum address,
44-
// we need to chop off the first 12 bytes of padding
45-
const addressBytes = address.toUint8Array();
46-
// double check to make sure there are no non zero bytes
47-
if (
48-
addressBytes.slice(0, 12).some((v) => {
49-
v !== 0;
50-
})
51-
)
52-
throw new Error(`Invalid EVM address ${address}`);
53-
54-
const suffix = encoding.hex.encode(addressBytes.slice(12));
55-
this.address = getAddress(suffix);
39+
const addressBytes = this.trimUniversalAddress(address.toUint8Array());
40+
this.address = getAddress(encoding.hex.encode(addressBytes));
5641
} else throw new Error(`Invalid EVM address ${address}`);
5742
}
5843

@@ -71,6 +56,30 @@ export class EvmAddress implements Address {
7156
toUniversalAddress() {
7257
return new UniversalAddress(this.address, 'hex');
7358
}
59+
60+
private trimUniversalAddress(address: Uint8Array): Uint8Array {
61+
if (address.length === EvmAddress.byteSize) return address;
62+
63+
if (address.length < EvmAddress.byteSize)
64+
throw new Error(
65+
`Invalid evm address, expected ${EvmAddress.byteSize} bytes`,
66+
);
67+
68+
if (address.length !== UniversalAddress.byteSize)
69+
throw new Error(
70+
`Invalid universal address, expected ${UniversalAddress.byteSize} bytes`,
71+
);
72+
73+
// If the address is longer than 20 bytes, it is a universal address
74+
// that has been padded with 12 bytes of zeros
75+
if (encoding.bignum.decode(address.slice(0, 12)) !== 0n) {
76+
throw new Error(
77+
`Invalid EVM address ${address} expected first 12 bytes to be 0s`,
78+
);
79+
}
80+
81+
return address.slice(12);
82+
}
7483
static isValidAddress(address: string) {
7584
return isAddress(address);
7685
}

platforms/sui/protocols/tokenBridge/src/tokenBridge.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ export class SuiTokenBridge<N extends Network, C extends SuiChains> implements T
138138

139139
if (!isMoveStructStruct(addressVal)) throw new Error("Expected fields to be a MoveStruct");
140140

141+
const universalAddress = new Uint8Array(addressVal.fields["data"]! as Array<number>);
141142
return {
142143
chain: toChain(Number(info.fields["token_chain"])),
143-
// @ts-ignore TODO
144-
address: new UniversalAddress(addressVal.fields["data"]!),
144+
address: new UniversalAddress(universalAddress),
145145
};
146146
}
147147

0 commit comments

Comments
 (0)