@wormhole-foundation/sdk-definitions 0.1.6-beta.2 → 0.1.7-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/platform.d.ts +2 -1
- package/dist/cjs/platform.d.ts.map +1 -1
- package/dist/cjs/protocols/cctp.js +1 -1
- package/dist/cjs/protocols/cctp.js.map +1 -1
- package/dist/cjs/protocols/ibc.d.ts.map +1 -1
- package/dist/cjs/protocols/ibc.js +2 -2
- package/dist/cjs/protocols/ibc.js.map +1 -1
- package/dist/cjs/protocols/tokenBridge.d.ts.map +1 -1
- package/dist/cjs/relayer.js +0 -13
- package/dist/cjs/relayer.js.map +1 -1
- package/dist/cjs/signer.d.ts +18 -0
- package/dist/cjs/signer.d.ts.map +1 -0
- package/dist/cjs/signer.js +20 -0
- package/dist/cjs/signer.js.map +1 -0
- package/dist/cjs/testing/mocks/platform.d.ts +1 -0
- package/dist/cjs/testing/mocks/platform.d.ts.map +1 -1
- package/dist/cjs/testing/mocks/platform.js +3 -0
- package/dist/cjs/testing/mocks/platform.js.map +1 -1
- package/dist/cjs/testing/utils/address.d.ts.map +1 -1
- package/dist/cjs/testing/utils/address.js +6 -6
- package/dist/cjs/testing/utils/address.js.map +1 -1
- package/dist/cjs/types.d.ts +1 -15
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/types.js +3 -18
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/universalAddress.d.ts.map +1 -1
- package/dist/cjs/universalAddress.js +3 -3
- package/dist/cjs/universalAddress.js.map +1 -1
- package/dist/cjs/vaa.d.ts.map +1 -1
- package/dist/cjs/vaa.js +4 -4
- package/dist/cjs/vaa.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/platform.d.ts +2 -1
- package/dist/esm/platform.d.ts.map +1 -1
- package/dist/esm/protocols/cctp.js +2 -2
- package/dist/esm/protocols/cctp.js.map +1 -1
- package/dist/esm/protocols/ibc.d.ts.map +1 -1
- package/dist/esm/protocols/ibc.js +3 -3
- package/dist/esm/protocols/ibc.js.map +1 -1
- package/dist/esm/protocols/tokenBridge.d.ts.map +1 -1
- package/dist/esm/relayer.js +0 -13
- package/dist/esm/relayer.js.map +1 -1
- package/dist/esm/signer.d.ts +18 -0
- package/dist/esm/signer.d.ts.map +1 -0
- package/dist/esm/signer.js +14 -0
- package/dist/esm/signer.js.map +1 -0
- package/dist/esm/testing/mocks/platform.d.ts +1 -0
- package/dist/esm/testing/mocks/platform.d.ts.map +1 -1
- package/dist/esm/testing/mocks/platform.js +3 -0
- package/dist/esm/testing/mocks/platform.js.map +1 -1
- package/dist/esm/testing/utils/address.d.ts.map +1 -1
- package/dist/esm/testing/utils/address.js +7 -7
- package/dist/esm/testing/utils/address.js.map +1 -1
- package/dist/esm/types.d.ts +1 -15
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +1 -13
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/universalAddress.d.ts.map +1 -1
- package/dist/esm/universalAddress.js +4 -4
- package/dist/esm/universalAddress.js.map +1 -1
- package/dist/esm/vaa.d.ts.map +1 -1
- package/dist/esm/vaa.js +5 -5
- package/dist/esm/vaa.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/platform.ts +17 -3
- package/src/protocols/cctp.ts +2 -2
- package/src/protocols/ibc.ts +7 -6
- package/src/protocols/tokenBridge.ts +10 -7
- package/src/relayer.ts +1 -15
- package/src/signer.ts +49 -0
- package/src/testing/mocks/platform.ts +3 -0
- package/src/testing/utils/address.ts +7 -6
- package/src/types.ts +2 -33
- package/src/universalAddress.ts +5 -9
- package/src/vaa.ts +33 -33
package/src/platform.ts
CHANGED
|
@@ -9,30 +9,35 @@ import { RpcConnection } from "./rpc";
|
|
|
9
9
|
import { AnyAddress, Balances, ChainsConfig, SignedTx, TokenId, TxHash } from "./types";
|
|
10
10
|
|
|
11
11
|
export interface PlatformUtils<P extends PlatformName> {
|
|
12
|
+
// Get the native (gas) token id for a given chain
|
|
12
13
|
nativeTokenId(chain: ChainName): TokenId;
|
|
14
|
+
isNativeTokenId(chain: ChainName, tokenId: TokenId): boolean;
|
|
13
15
|
|
|
16
|
+
// Check if this chain is supported by this platform
|
|
14
17
|
isSupportedChain(chain: ChainName): boolean;
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// Utils for platform specific queries
|
|
19
|
+
// Get the number of decimals for a given token
|
|
19
20
|
getDecimals(
|
|
20
21
|
chain: ChainName,
|
|
21
22
|
rpc: RpcConnection<P>,
|
|
22
23
|
token: AnyAddress,
|
|
23
24
|
): Promise<bigint>;
|
|
25
|
+
// Get the balance of a token for a given wallet address
|
|
24
26
|
getBalance(
|
|
25
27
|
chain: ChainName,
|
|
26
28
|
rpc: RpcConnection<P>,
|
|
27
29
|
walletAddr: string,
|
|
28
30
|
token: AnyAddress,
|
|
29
31
|
): Promise<bigint | null>;
|
|
32
|
+
// Look up the balances for a list of tokens for a given wallet address
|
|
33
|
+
// TODO: this should be batched but isn't currently
|
|
30
34
|
getBalances(
|
|
31
35
|
chain: ChainName,
|
|
32
36
|
rpc: RpcConnection<P>,
|
|
33
37
|
walletAddress: string,
|
|
34
38
|
tokens: AnyAddress[],
|
|
35
39
|
): Promise<Balances>;
|
|
40
|
+
// Look up the latest block
|
|
36
41
|
getCurrentBlock(rpc: RpcConnection<P>): Promise<number>;
|
|
37
42
|
|
|
38
43
|
// Platform interaction utils
|
|
@@ -42,6 +47,13 @@ export interface PlatformUtils<P extends PlatformName> {
|
|
|
42
47
|
stxns: SignedTx[],
|
|
43
48
|
): Promise<TxHash[]>;
|
|
44
49
|
|
|
50
|
+
// Look up a Chain from its native chain ID
|
|
51
|
+
// See implementation for details
|
|
52
|
+
// Note: this is _not_ the same as the Wormhole chain id
|
|
53
|
+
chainFromChainId(chainId: string): [Network, ChainName];
|
|
54
|
+
|
|
55
|
+
// Given an RPC connection, request the native chain id
|
|
56
|
+
// then resolve it to a Wormhole Canonical network and chain name
|
|
45
57
|
chainFromRpc(rpc: RpcConnection<P>): Promise<[Network, ChainName]>;
|
|
46
58
|
}
|
|
47
59
|
|
|
@@ -57,8 +69,10 @@ export interface Platform<P extends PlatformName> extends PlatformUtils<P> {
|
|
|
57
69
|
// Create a new Chain context object
|
|
58
70
|
getChain(chain: ChainName): ChainContext<P>;
|
|
59
71
|
|
|
72
|
+
// Create a _new_ RPC Connection
|
|
60
73
|
getRpc(chain: ChainName): RpcConnection<P>;
|
|
61
74
|
|
|
75
|
+
// Look up transaction logs and parse out Wormhole messages
|
|
62
76
|
parseTransaction(
|
|
63
77
|
chain: ChainName,
|
|
64
78
|
rpc: RpcConnection<P>,
|
package/src/protocols/cctp.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
LayoutToType,
|
|
4
4
|
PlatformName,
|
|
5
5
|
deserializeLayout,
|
|
6
|
-
|
|
6
|
+
encoding,
|
|
7
7
|
} from "@wormhole-foundation/sdk-base";
|
|
8
8
|
import { ChainAddress } from "../address";
|
|
9
9
|
import { CircleMessageId } from "../attestation";
|
|
@@ -40,7 +40,7 @@ export const deserializeCircleMessage = (
|
|
|
40
40
|
data: Uint8Array,
|
|
41
41
|
): [LayoutToType<typeof circleMessageLayout>, string] => {
|
|
42
42
|
const msg = deserializeLayout(circleMessageLayout, data);
|
|
43
|
-
const messsageHash =
|
|
43
|
+
const messsageHash = encoding.hex.encode(keccak256(data));
|
|
44
44
|
return [msg, messsageHash];
|
|
45
45
|
};
|
|
46
46
|
|
package/src/protocols/ibc.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
ChainName,
|
|
4
4
|
PlatformName,
|
|
5
5
|
toChainId,
|
|
6
|
+
encoding,
|
|
6
7
|
} from "@wormhole-foundation/sdk-base";
|
|
7
8
|
import { ChainAddress, NativeAddress } from "../address";
|
|
8
9
|
import { IbcMessageId, WormholeMessageId } from "../attestation";
|
|
@@ -48,8 +49,8 @@ export interface GatewayTransferWithPayloadMsg {
|
|
|
48
49
|
// to be decoded and executed by the Gateway contract.
|
|
49
50
|
export interface GatewayIbcTransferMsg {
|
|
50
51
|
gateway_ibc_token_bridge_payload:
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
| GatewayTransferMsg
|
|
53
|
+
| GatewayTransferWithPayloadMsg;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
export function isGatewayTransferMsg(
|
|
@@ -112,7 +113,7 @@ export function gatewayTransferMsg(
|
|
|
112
113
|
if (isGatewayTransferDetails(gtd)) {
|
|
113
114
|
// If we've already got a payload, b64 encode it so it works in json
|
|
114
115
|
const _payload = gtd.payload
|
|
115
|
-
?
|
|
116
|
+
? encoding.b64.encode(gtd.payload)
|
|
116
117
|
: undefined;
|
|
117
118
|
|
|
118
119
|
// Encode the payload so the gateway contract knows where to forward the
|
|
@@ -150,7 +151,7 @@ export function makeGatewayTransferMsg(
|
|
|
150
151
|
typeof recipient === "string"
|
|
151
152
|
? recipient
|
|
152
153
|
: //@ts-ignore
|
|
153
|
-
|
|
154
|
+
encoding.b64.encode(recipient.toString());
|
|
154
155
|
|
|
155
156
|
const common = {
|
|
156
157
|
chain: toChainId(chain),
|
|
@@ -161,8 +162,8 @@ export function makeGatewayTransferMsg(
|
|
|
161
162
|
|
|
162
163
|
const msg: GatewayTransferWithPayloadMsg | GatewayTransferMsg = payload
|
|
163
164
|
? ({
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
gateway_transfer_with_payload: { ...common, payload: payload },
|
|
166
|
+
} as GatewayTransferWithPayloadMsg)
|
|
166
167
|
: ({ gateway_transfer: { ...common } } as GatewayTransferMsg);
|
|
167
168
|
|
|
168
169
|
return msg;
|
|
@@ -52,26 +52,28 @@ export interface TokenBridge<P extends PlatformName> {
|
|
|
52
52
|
getOriginalAsset(nativeAddress: AnyAddress): Promise<TokenId>;
|
|
53
53
|
// returns the wrapped version of the native asset
|
|
54
54
|
getWrappedNative(): Promise<NativeAddress<P>>;
|
|
55
|
-
|
|
56
55
|
// Check to see if a foreign token has a wrapped version
|
|
57
56
|
hasWrappedAsset(foreignToken: TokenId): Promise<boolean>;
|
|
58
57
|
// Returns the address of the native version of this asset
|
|
59
58
|
getWrappedAsset(foreignToken: TokenId): Promise<NativeAddress<P>>;
|
|
60
|
-
|
|
61
|
-
// TODO: preview (receive amount, fees, gas estimates, estimated blocks/time)
|
|
59
|
+
// Checks if a transfer VAA has been redeemed
|
|
62
60
|
isTransferCompleted(
|
|
63
61
|
vaa: TokenBridge.VAA<"Transfer" | "TransferWithPayload">
|
|
64
62
|
): Promise<boolean>;
|
|
65
|
-
//
|
|
63
|
+
// Create a Token Attestation VAA containing metadata about
|
|
64
|
+
// the token that may be submitted to a Token bridge on another chain
|
|
65
|
+
// to allow it to create a wrapped version of the token
|
|
66
66
|
createAttestation(
|
|
67
67
|
token_to_attest: AnyAddress,
|
|
68
68
|
payer?: AnyAddress
|
|
69
69
|
): AsyncGenerator<UnsignedTransaction>;
|
|
70
|
+
// Submit the Token Attestation VAA to the Token bridge
|
|
71
|
+
// to create the wrapped token represented by the data in the VAA
|
|
70
72
|
submitAttestation(
|
|
71
73
|
vaa: TokenBridge.VAA<"AttestMeta">,
|
|
72
74
|
payer?: AnyAddress
|
|
73
75
|
): AsyncGenerator<UnsignedTransaction>;
|
|
74
|
-
//
|
|
76
|
+
// Initiate a transfer of some token to another chain
|
|
75
77
|
transfer(
|
|
76
78
|
sender: AnyAddress,
|
|
77
79
|
recipient: ChainAddress,
|
|
@@ -79,13 +81,14 @@ export interface TokenBridge<P extends PlatformName> {
|
|
|
79
81
|
amount: bigint,
|
|
80
82
|
payload?: Uint8Array
|
|
81
83
|
): AsyncGenerator<UnsignedTransaction>;
|
|
82
|
-
//
|
|
84
|
+
// Redeem a transfer VAA to receive the tokens on this chain
|
|
83
85
|
redeem(
|
|
84
86
|
sender: AnyAddress,
|
|
85
87
|
vaa: TokenBridge.VAA<"Transfer" | "TransferWithPayload">,
|
|
86
88
|
unwrapNative?: boolean //default: true
|
|
87
89
|
): AsyncGenerator<UnsignedTransaction>;
|
|
88
|
-
|
|
90
|
+
|
|
91
|
+
// TODO: preview (receive amount, fees, gas estimates, estimated blocks/time)
|
|
89
92
|
}
|
|
90
93
|
|
|
91
94
|
export interface AutomaticTokenBridge<P extends PlatformName> {
|
package/src/relayer.ts
CHANGED
|
@@ -33,18 +33,4 @@ export interface Relayer<P extends PlatformName> {
|
|
|
33
33
|
tokenId: AnyAddress,
|
|
34
34
|
walletAddress: string,
|
|
35
35
|
): Promise<bigint>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//function parseRelayerPayload(transferPayload: Buffer): ParsedRelayerPayload {
|
|
39
|
-
// return {
|
|
40
|
-
// relayerPayloadId: transferPayload.readUint8(0),
|
|
41
|
-
// relayerFee: BigNumber.from(
|
|
42
|
-
// "0x" + transferPayload.subarray(1, 33).toString("hex")
|
|
43
|
-
// ),
|
|
44
|
-
// toNativeTokenAmount: BigNumber.from(
|
|
45
|
-
// "0x" + transferPayload.subarray(33, 65).toString("hex")
|
|
46
|
-
// ),
|
|
47
|
-
// to: "0x" + transferPayload.subarray(65, 98).toString("hex"),
|
|
48
|
-
// };
|
|
49
|
-
//}
|
|
50
|
-
//
|
|
36
|
+
}
|
package/src/signer.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ChainName } from "@wormhole-foundation/sdk-base";
|
|
2
|
+
import { UnsignedTransaction } from "./unsignedTransaction";
|
|
3
|
+
import { SignedTx, TxHash } from "./types";
|
|
4
|
+
|
|
5
|
+
// A Signer is an interface that must be provided to certain methods
|
|
6
|
+
// in the SDK to sign transactions. It can be either a SignOnlySigner
|
|
7
|
+
// or a SignAndSendSigner depending on circumstances.
|
|
8
|
+
// A Signer can be implemented by wrapping an existing offline wallet
|
|
9
|
+
// or a web wallet
|
|
10
|
+
export type Signer = SignOnlySigner | SignAndSendSigner;
|
|
11
|
+
|
|
12
|
+
export function isSigner(thing: Signer | any): thing is Signer {
|
|
13
|
+
return isSignOnlySigner(thing) || isSignAndSendSigner(thing)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// A SignOnlySender is for situations where the signer is not
|
|
17
|
+
// connected to the network or does not wish to broadcast the
|
|
18
|
+
// transactions themselves
|
|
19
|
+
export interface SignOnlySigner {
|
|
20
|
+
chain(): ChainName;
|
|
21
|
+
address(): string;
|
|
22
|
+
sign(tx: UnsignedTransaction[]): Promise<SignedTx[]>;
|
|
23
|
+
}
|
|
24
|
+
export function isSignOnlySigner(thing: SignOnlySigner | any): thing is SignOnlySigner {
|
|
25
|
+
return (
|
|
26
|
+
typeof (<SignOnlySigner>thing).chain === "function" &&
|
|
27
|
+
typeof (<SignOnlySigner>thing).address == "function" &&
|
|
28
|
+
typeof (<SignOnlySigner>thing).sign === "function"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// A SignAndSendSigner is for situations where the signer is
|
|
33
|
+
// connected to the network and wishes to broadcast the
|
|
34
|
+
// transactions themselves
|
|
35
|
+
export interface SignAndSendSigner {
|
|
36
|
+
chain(): ChainName;
|
|
37
|
+
address(): string;
|
|
38
|
+
signAndSend(tx: UnsignedTransaction[]): Promise<TxHash[]>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function isSignAndSendSigner(thing: SignAndSendSigner | any): thing is SignAndSendSigner {
|
|
42
|
+
return (
|
|
43
|
+
typeof (<SignAndSendSigner>thing).chain === "function" &&
|
|
44
|
+
typeof (<SignAndSendSigner>thing).address == "function" &&
|
|
45
|
+
typeof (<SignAndSendSigner>thing).signAndSend === "function"
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
@@ -143,6 +143,9 @@ export class MockPlatform<P extends PlatformName> implements Platform<P> {
|
|
|
143
143
|
throw new Error("Method not implemented");
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
chainFromChainId(chainId: string): [Network, ChainName] {
|
|
147
|
+
throw new Error("Not implemented");
|
|
148
|
+
}
|
|
146
149
|
async chainFromRpc(rpc: RpcConnection<P>): Promise<[Network, ChainName]> {
|
|
147
150
|
throw new Error("Not implemented");
|
|
148
151
|
}
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
ChainName,
|
|
4
4
|
PlatformName,
|
|
5
5
|
chainToPlatform,
|
|
6
|
+
encoding,
|
|
6
7
|
isPlatform,
|
|
7
8
|
platformToChains,
|
|
8
9
|
} from "@wormhole-foundation/sdk-base";
|
|
@@ -14,22 +15,22 @@ import {
|
|
|
14
15
|
} from "../../";
|
|
15
16
|
|
|
16
17
|
// return a random buffer of length n
|
|
17
|
-
function
|
|
18
|
+
function randomBytes(n: number): Uint8Array {
|
|
18
19
|
const buff = new Uint8Array(n);
|
|
19
20
|
crypto.getRandomValues(buff);
|
|
20
|
-
return
|
|
21
|
+
return buff;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
// get a random 20 byte address
|
|
24
25
|
function fake20ByteAddress(): string {
|
|
25
|
-
const buff =
|
|
26
|
-
return
|
|
26
|
+
const buff = randomBytes(20);
|
|
27
|
+
return encoding.hex.encode(buff);
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
// get a random 32 byte address
|
|
30
31
|
function fake32ByteAddress(): string {
|
|
31
|
-
const buff =
|
|
32
|
-
return
|
|
32
|
+
const buff = randomBytes(32);
|
|
33
|
+
return encoding.hex.encode(buff);
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
// make a random native address for a given chain
|
package/src/types.ts
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
} from "@wormhole-foundation/sdk-base";
|
|
8
8
|
import { ChainAddress, NativeAddress, toNative } from "./address";
|
|
9
9
|
import { Contracts } from "./contracts";
|
|
10
|
+
import { Signer, isSigner } from './signer'
|
|
11
|
+
|
|
10
12
|
import { UnsignedTransaction } from "./unsignedTransaction";
|
|
11
13
|
import { UniversalAddress } from "./universalAddress";
|
|
12
14
|
|
|
@@ -35,39 +37,6 @@ export type Balances = {
|
|
|
35
37
|
[key: string]: BigInt | null;
|
|
36
38
|
};
|
|
37
39
|
|
|
38
|
-
export interface SignOnlySigner {
|
|
39
|
-
chain(): ChainName;
|
|
40
|
-
address(): string;
|
|
41
|
-
sign(tx: UnsignedTransaction[]): Promise<SignedTx[]>;
|
|
42
|
-
}
|
|
43
|
-
export function isSignOnlySigner(thing: SignOnlySigner | any): thing is SignOnlySigner {
|
|
44
|
-
return (
|
|
45
|
-
typeof (<SignOnlySigner>thing).chain === "function" &&
|
|
46
|
-
typeof (<SignOnlySigner>thing).address == "function" &&
|
|
47
|
-
typeof (<SignOnlySigner>thing).sign === "function"
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface SignAndSendSigner {
|
|
52
|
-
chain(): ChainName;
|
|
53
|
-
address(): string;
|
|
54
|
-
signAndSend(tx: UnsignedTransaction[]): Promise<TxHash[]>;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function isSignAndSendSigner(thing: SignAndSendSigner | any): thing is SignAndSendSigner {
|
|
58
|
-
return (
|
|
59
|
-
typeof (<SignAndSendSigner>thing).chain === "function" &&
|
|
60
|
-
typeof (<SignAndSendSigner>thing).address == "function" &&
|
|
61
|
-
typeof (<SignAndSendSigner>thing).signAndSend === "function"
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export type Signer = SignOnlySigner | SignAndSendSigner;
|
|
66
|
-
|
|
67
|
-
export function isSigner(thing: Signer | any): thing is Signer {
|
|
68
|
-
return isSignOnlySigner(thing) || isSignAndSendSigner(thing)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
40
|
|
|
72
41
|
export function nativeChainAddress(
|
|
73
42
|
s: Signer | TokenId | [ChainName, UniversalAddress | Uint8Array | string],
|
package/src/universalAddress.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
hexByteStringToUint8Array,
|
|
3
|
-
uint8ArrayToHexByteString,
|
|
4
|
-
isHexByteString,
|
|
5
|
-
} from "@wormhole-foundation/sdk-base";
|
|
1
|
+
import { encoding } from "@wormhole-foundation/sdk-base";
|
|
6
2
|
|
|
7
3
|
import { Address, NativeAddress, toNative } from "./address";
|
|
8
4
|
|
|
@@ -17,10 +13,10 @@ export class UniversalAddress implements Address {
|
|
|
17
13
|
if (!UniversalAddress.isValidAddress(address))
|
|
18
14
|
throw new Error(
|
|
19
15
|
`Invalid Wormhole address, expected ${UniversalAddress.byteSize}-byte ` +
|
|
20
|
-
|
|
16
|
+
`hex string but got ${address}`,
|
|
21
17
|
);
|
|
22
18
|
|
|
23
|
-
this.address =
|
|
19
|
+
this.address = encoding.hex.decode(address);
|
|
24
20
|
} else {
|
|
25
21
|
this.address = address;
|
|
26
22
|
}
|
|
@@ -36,7 +32,7 @@ export class UniversalAddress implements Address {
|
|
|
36
32
|
return this.address;
|
|
37
33
|
}
|
|
38
34
|
toString() {
|
|
39
|
-
return
|
|
35
|
+
return encoding.hex.encode(this.address, true);
|
|
40
36
|
}
|
|
41
37
|
toUint8Array() {
|
|
42
38
|
return this.address;
|
|
@@ -53,7 +49,7 @@ export class UniversalAddress implements Address {
|
|
|
53
49
|
}
|
|
54
50
|
|
|
55
51
|
static isValidAddress(address: string) {
|
|
56
|
-
return
|
|
52
|
+
return encoding.hex.valid(address) && encoding.stripPrefix("0x", address).length === UniversalAddress.byteSize * 2;
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
static instanceof(address: any) {
|
package/src/vaa.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
encoding,
|
|
3
3
|
Layout,
|
|
4
4
|
LayoutItem,
|
|
5
5
|
LayoutToType,
|
|
@@ -29,7 +29,7 @@ import { keccak256 } from "./utils";
|
|
|
29
29
|
declare global {
|
|
30
30
|
namespace Wormhole {
|
|
31
31
|
//effective type: Record<string, Layout>
|
|
32
|
-
interface PayloadLiteralToLayoutMapping {}
|
|
32
|
+
interface PayloadLiteralToLayoutMapping { }
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -37,8 +37,8 @@ export type LayoutLiteral = keyof Wormhole.PayloadLiteralToLayoutMapping & strin
|
|
|
37
37
|
type LayoutOf<LL extends LayoutLiteral> =
|
|
38
38
|
//TODO check if this lazy instantiation hack is actually necessary
|
|
39
39
|
LL extends infer V extends LayoutLiteral
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
? Wormhole.PayloadLiteralToLayoutMapping[V]
|
|
41
|
+
: never;
|
|
42
42
|
|
|
43
43
|
type LayoutLiteralToPayloadType<LL extends LayoutLiteral> = LayoutToType<LayoutOf<LL>>;
|
|
44
44
|
|
|
@@ -134,7 +134,7 @@ export type DistributiveVAA<PL extends PayloadLiteral> =
|
|
|
134
134
|
? VAA<PL>
|
|
135
135
|
: never;
|
|
136
136
|
|
|
137
|
-
export type ProtocolVAA<PN extends ProtocolName, PayloadName extends string> =
|
|
137
|
+
export type ProtocolVAA<PN extends ProtocolName, PayloadName extends string> =
|
|
138
138
|
ComposeLiteral<PN, PayloadName, PayloadLiteral> extends infer PL extends PayloadLiteral
|
|
139
139
|
? DistributiveVAA<PL>
|
|
140
140
|
: never;
|
|
@@ -154,10 +154,10 @@ const payloadLiteralToPayloadItem = <PL extends PayloadLiteral>(
|
|
|
154
154
|
(payloadLiteral === "Uint8Array"
|
|
155
155
|
? ({ name: "payload", binary: "bytes" } as const)
|
|
156
156
|
: ({
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
157
|
+
name: "payload",
|
|
158
|
+
binary: "object",
|
|
159
|
+
layout: getPayloadLayout(payloadLiteral),
|
|
160
|
+
} as const)) satisfies LayoutItem;
|
|
161
161
|
|
|
162
162
|
//annoyingly we can't use the return value of payloadLiteralToPayloadItem
|
|
163
163
|
type PayloadLiteralToDynamicItems<PL extends PayloadLiteral> =
|
|
@@ -165,12 +165,12 @@ type PayloadLiteralToDynamicItems<PL extends PayloadLiteral> =
|
|
|
165
165
|
[
|
|
166
166
|
...typeof baseLayout,
|
|
167
167
|
PL extends LayoutLiteral
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
168
|
+
? {
|
|
169
|
+
name: "payload";
|
|
170
|
+
binary: "object";
|
|
171
|
+
layout: DynamicItemsOfLayout<LayoutOf<PL>>;
|
|
172
|
+
}
|
|
173
|
+
: { name: "payload"; binary: "bytes" },
|
|
174
174
|
]
|
|
175
175
|
>;
|
|
176
176
|
|
|
@@ -214,7 +214,7 @@ type AtLeast2<T> = readonly [T, T, ...T[]];
|
|
|
214
214
|
|
|
215
215
|
export type NamedPayloads = AtLeast1<readonly [string, Layout]>;
|
|
216
216
|
export type RegisterPayloadTypes<ProtocolN extends ProtocolName, NP extends NamedPayloads> =
|
|
217
|
-
{ readonly [E in NP[number]
|
|
217
|
+
{ readonly [E in NP[number]as ToLiteralFormat<ProtocolN, E[0]>]: E[1] };
|
|
218
218
|
|
|
219
219
|
export function registerPayloadTypes(protocol: ProtocolName, payloads: NamedPayloads) {
|
|
220
220
|
for (const [name, layout] of payloads)
|
|
@@ -262,7 +262,7 @@ type PayloadGroupsToLayoutLiterals<PGA extends readonly PayloadGroup[]> =
|
|
|
262
262
|
: never;
|
|
263
263
|
|
|
264
264
|
type LLDtoLLs<LLD extends AtLeast2<LayoutLiteral> | readonly [ProtocolName, AtLeast2<string>]
|
|
265
|
-
| AtLeast2<PayloadGroup>> =
|
|
265
|
+
| AtLeast2<PayloadGroup>> =
|
|
266
266
|
LLD extends AtLeast2<LayoutLiteral>
|
|
267
267
|
? LLD[number]
|
|
268
268
|
: LLD extends readonly [ProtocolName, AtLeast2<string>]
|
|
@@ -273,7 +273,7 @@ type LLDtoLLs<LLD extends AtLeast2<LayoutLiteral> | readonly [ProtocolName, AtLe
|
|
|
273
273
|
|
|
274
274
|
export function payloadDiscriminator<
|
|
275
275
|
const LLD extends AtLeast2<LayoutLiteral> | readonly [ProtocolName, AtLeast2<string>]
|
|
276
|
-
|
|
276
|
+
| AtLeast2<PayloadGroup>,
|
|
277
277
|
B extends boolean = false
|
|
278
278
|
>(
|
|
279
279
|
payloadLiterals: LLD,
|
|
@@ -284,15 +284,15 @@ export function payloadDiscriminator<
|
|
|
284
284
|
return (payloadLiterals as AtLeast2<PayloadGroup>).flatMap(([protocol, payloadNames]) =>
|
|
285
285
|
payloadNames.map((name) => composeLiteral(protocol, name))
|
|
286
286
|
);
|
|
287
|
-
|
|
287
|
+
|
|
288
288
|
if (typeof payloadLiterals[1] === "string")
|
|
289
289
|
return payloadLiterals as AtLeast2<LayoutLiteral>;
|
|
290
|
-
|
|
290
|
+
|
|
291
291
|
const [protocol, payloadNames] =
|
|
292
292
|
payloadLiterals as readonly [ProtocolName, AtLeast2<LayoutLiteral>];
|
|
293
293
|
return payloadNames.map((name) => composeLiteral(protocol, name));
|
|
294
294
|
})();
|
|
295
|
-
|
|
295
|
+
|
|
296
296
|
const discriminator = layoutDiscriminator(
|
|
297
297
|
literals.map(literal => getPayloadLayout(literal)),
|
|
298
298
|
!!allowAmbiguous
|
|
@@ -300,7 +300,7 @@ export function payloadDiscriminator<
|
|
|
300
300
|
|
|
301
301
|
return ((data: Byteish) => {
|
|
302
302
|
if (typeof data === "string")
|
|
303
|
-
data =
|
|
303
|
+
data = encoding.hex.decode(data);
|
|
304
304
|
|
|
305
305
|
const cands = discriminator(data);
|
|
306
306
|
return (Array.isArray(cands))
|
|
@@ -315,7 +315,7 @@ export function deserialize<T extends PayloadLiteral | PayloadDiscriminator>(
|
|
|
315
315
|
payloadDet: T,
|
|
316
316
|
data: Byteish,
|
|
317
317
|
): DistributiveVAA<ExtractLiteral<T>> {
|
|
318
|
-
if (typeof data === "string") data =
|
|
318
|
+
if (typeof data === "string") data = encoding.hex.decode(data);
|
|
319
319
|
|
|
320
320
|
const [header, envelopeOffset] =
|
|
321
321
|
deserializeLayout(headerLayout, data, 0, false);
|
|
@@ -339,7 +339,7 @@ export function deserialize<T extends PayloadLiteral | PayloadDiscriminator>(
|
|
|
339
339
|
);
|
|
340
340
|
|
|
341
341
|
const [payloadLiteral, payload] =
|
|
342
|
-
|
|
342
|
+
typeof payloadDet === "string"
|
|
343
343
|
? [
|
|
344
344
|
payloadDet as PayloadLiteral,
|
|
345
345
|
deserializePayload(payloadDet as PayloadLiteral, data, payloadOffset)
|
|
@@ -383,7 +383,7 @@ export function deserializePayload<
|
|
|
383
383
|
): DeserializePayloadReturn<T> {
|
|
384
384
|
//grouped together to have just a single cast on the return type
|
|
385
385
|
return (() => {
|
|
386
|
-
if (typeof data === "string") data =
|
|
386
|
+
if (typeof data === "string") data = encoding.hex.decode(data);
|
|
387
387
|
|
|
388
388
|
if (payloadDet === "Uint8Array") return data.slice(offset);
|
|
389
389
|
|
|
@@ -397,7 +397,7 @@ export function deserializePayload<
|
|
|
397
397
|
throw new Error(
|
|
398
398
|
`Encoded data does not match any of the given payload types - ${data}`,
|
|
399
399
|
);
|
|
400
|
-
|
|
400
|
+
|
|
401
401
|
return [
|
|
402
402
|
candidate,
|
|
403
403
|
deserializeLayout(getPayloadLayout(candidate), data, offset),
|
|
@@ -420,16 +420,16 @@ export const blindDeserializePayload = (() => {
|
|
|
420
420
|
[layoutLiterals, discriminator] = rebuildDiscrimininator();
|
|
421
421
|
|
|
422
422
|
if (typeof data === "string")
|
|
423
|
-
data =
|
|
423
|
+
data = encoding.hex.decode(data);
|
|
424
424
|
|
|
425
425
|
const candidates = discriminator(data).map((c) => layoutLiterals[c]);
|
|
426
426
|
return candidates.reduce((acc, literal) => {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
427
|
+
try {
|
|
428
|
+
acc.push([literal, deserializePayload(literal, data)] as DeserializedPair);
|
|
429
|
+
}
|
|
430
|
+
catch { }
|
|
431
|
+
return acc;
|
|
432
|
+
},
|
|
433
433
|
[] as DeserializedPair[]
|
|
434
434
|
);
|
|
435
435
|
}
|