From 43be8106f26f63b90a98f78f0ab93d9335a21816 Mon Sep 17 00:00:00 2001 From: Ben Guidarelli Date: Thu, 21 Dec 2023 11:15:04 -0500 Subject: [PATCH] refactor addresses (#178) --- connect/src/protocols/cctpTransfer.ts | 11 ++-- connect/src/protocols/gatewayTransfer.ts | 5 +- connect/src/protocols/tokenTransfer.ts | 36 ++++++++----- connect/src/tasks.ts | 3 +- connect/src/wormhole.ts | 8 +-- core/base/src/constants/index.ts | 1 + core/definitions/__tests__/circleMessage.ts | 15 ++---- core/definitions/src/address.ts | 45 +++++++--------- core/definitions/src/layout-items/circle.ts | 4 +- core/definitions/src/protocol.ts | 9 ++-- core/definitions/src/protocols/core.ts | 4 +- core/definitions/src/protocols/ibc.ts | 52 ++++++------------- core/definitions/src/relayer.ts | 16 +++--- .../src/testing/mocks/tokenBridge.ts | 3 +- core/definitions/src/testing/utils/address.ts | 5 +- core/definitions/src/types.ts | 44 +--------------- examples/src/cosmos.ts | 11 ++-- examples/src/helpers/helpers.ts | 6 +-- package-lock.json | 18 +++---- package.json | 5 +- platforms/aptos/package.json | 4 +- platforms/aptos/protocols/core/package.json | 6 +-- .../aptos/protocols/tokenBridge/package.json | 8 +-- .../protocols/tokenBridge/src/tokenBridge.ts | 7 ++- platforms/aptos/src/address.ts | 18 +++---- platforms/aptos/src/platform.ts | 5 +- platforms/aptos/src/types.ts | 3 +- .../cosmwasm/protocols/core/src/index.ts | 3 +- platforms/cosmwasm/protocols/ibc/src/index.ts | 3 +- .../protocols/tokenBridge/src/index.ts | 3 +- .../protocols/tokenBridge/src/tokenBridge.ts | 2 +- platforms/cosmwasm/src/address.ts | 18 +++---- platforms/cosmwasm/src/gateway.ts | 2 +- platforms/cosmwasm/src/platform.ts | 10 ++-- platforms/cosmwasm/src/types.ts | 2 +- .../__tests__/integration/tokenBridge.test.ts | 28 ++++------ .../cctp/src/automaticCircleBridge.ts | 3 +- .../evm/protocols/cctp/src/circleBridge.ts | 7 ++- platforms/evm/protocols/cctp/src/index.ts | 5 +- platforms/evm/protocols/core/src/index.ts | 3 +- .../tokenBridge/src/automaticTokenBridge.ts | 6 +-- .../evm/protocols/tokenBridge/src/index.ts | 5 +- .../protocols/tokenBridge/src/tokenBridge.ts | 15 +++--- platforms/evm/src/address.ts | 2 +- platforms/evm/src/platform.ts | 4 +- platforms/evm/src/types.ts | 2 +- .../solana/protocols/cctp/src/circleBridge.ts | 7 ++- platforms/solana/protocols/cctp/src/index.ts | 3 +- .../src/utils/instructions/receiveMessage.ts | 7 +-- platforms/solana/protocols/core/src/core.ts | 21 ++++---- platforms/solana/protocols/core/src/index.ts | 3 +- .../core/src/utils/instructions/governance.ts | 4 +- .../tokenBridge/src/automaticTokenBridge.ts | 13 +++-- .../solana/protocols/tokenBridge/src/index.ts | 5 +- .../protocols/tokenBridge/src/tokenBridge.ts | 16 +++--- .../utils/tokenBridge/accounts/endpoint.ts | 9 ++-- .../src/utils/tokenBridge/accounts/wrapped.ts | 16 ++---- platforms/solana/src/address.ts | 22 ++++---- platforms/solana/src/chain.ts | 2 +- platforms/solana/src/platform.ts | 4 +- platforms/solana/src/types.ts | 4 +- tokenRegistry/src/scripts/foreignAssets.ts | 5 +- 62 files changed, 264 insertions(+), 352 deletions(-) diff --git a/connect/src/protocols/cctpTransfer.ts b/connect/src/protocols/cctpTransfer.ts index 481991704..f9def2672 100644 --- a/connect/src/protocols/cctpTransfer.ts +++ b/connect/src/protocols/cctpTransfer.ts @@ -17,7 +17,6 @@ import { isCircleTransferDetails, isTransactionIdentifier, isWormholeMessageId, - nativeChainAddress, } from "@wormhole-foundation/sdk-definitions"; import { signSendWait } from "../common"; @@ -129,7 +128,7 @@ export class CircleTransfer let automatic = false; if (wormholeRelayer) { - const relayerAddress = nativeChainAddress( + const relayerAddress = Wormhole.chainAddress( chain, wormholeRelayer, ).address.toUniversalAddress(); @@ -137,8 +136,8 @@ export class CircleTransfer } const details: CircleTransferDetails = { - from: nativeChainAddress(from.chain, vaa.payload.caller), - to: nativeChainAddress(rcvChain, rcvAddress), + from: { chain: from.chain, address: vaa.payload.caller }, + to: { chain: rcvChain, address: rcvAddress }, amount: vaa.payload.token.amount, automatic, }; @@ -165,8 +164,8 @@ export class CircleTransfer const rcvChain = circle.toCircleChain(msg.destinationDomain); const details: CircleTransferDetails = { - from: nativeChainAddress(sendChain, xferSender), - to: nativeChainAddress(rcvChain, xferReceiver), + from: { chain: sendChain, address: xferSender }, + to: { chain: rcvChain, address: xferReceiver }, amount: burnMessage.amount, automatic: false, }; diff --git a/connect/src/protocols/gatewayTransfer.ts b/connect/src/protocols/gatewayTransfer.ts index 1ba295855..826156260 100644 --- a/connect/src/protocols/gatewayTransfer.ts +++ b/connect/src/protocols/gatewayTransfer.ts @@ -4,8 +4,8 @@ import { chainToPlatform, encoding, toChain, + PlatformToChains, } from "@wormhole-foundation/sdk-base"; -import { PlatformToChains } from "@wormhole-foundation/sdk-base/src"; import { ChainAddress, ChainContext, @@ -25,7 +25,6 @@ import { isGatewayTransferDetails, isTransactionIdentifier, isWormholeMessageId, - nativeChainAddress, toGatewayMsg, toNative, AttestationId, @@ -200,7 +199,7 @@ export class GatewayTransfer implements WormholeTra const recipientAddress = encoding.bytes.decode( encoding.b64.decode(maybeWithPayload.recipient), ); - to = nativeChainAddress(destChain, recipientAddress); + to = Wormhole.chainAddress(destChain, recipientAddress); } catch { /*Ignoring, throws if not the payload isnt JSON*/ } diff --git a/connect/src/protocols/tokenTransfer.ts b/connect/src/protocols/tokenTransfer.ts index 56c799ea1..f48999a6a 100644 --- a/connect/src/protocols/tokenTransfer.ts +++ b/connect/src/protocols/tokenTransfer.ts @@ -1,7 +1,14 @@ -import { Chain, Network, PlatformToChains, encoding, toChain } from "@wormhole-foundation/sdk-base"; -import { Platform } from "@wormhole-foundation/sdk-base/dist/cjs"; -import { ChainToPlatform } from "@wormhole-foundation/sdk-base/src"; import { + ChainToPlatform, + Platform, + Chain, + Network, + PlatformToChains, + encoding, + toChain, +} from "@wormhole-foundation/sdk-base"; +import { + AttestationId, ChainContext, Signer, TokenAddress, @@ -16,16 +23,15 @@ import { isTokenTransferDetails, isTransactionIdentifier, isWormholeMessageId, - nativeChainAddress, toNative, - AttestationId, + toUniversal, } from "@wormhole-foundation/sdk-definitions"; import { signSendWait } from "../common"; import { DEFAULT_TASK_TIMEOUT } from "../config"; import { Wormhole } from "../wormhole"; import { - TransferReceipt, TransferQuote, + TransferReceipt, TransferState, WormholeTransfer, } from "../wormholeTransfer"; @@ -285,7 +291,8 @@ export class TokenTransfer const receipt = TokenTransfer.getReceipt(xfer); if (receipt.state === TransferState.SourceInitiated) { - if (receipt.originTxs.length === 0) throw "Invalid state transition"; + if (receipt.originTxs.length === 0) + throw "Invalid state transition: no originating transactions"; if (!receipt.attestation || !receipt.attestation?.emitter) { const initTx = receipt.originTxs[receipt.originTxs.length - 1]!; @@ -302,7 +309,8 @@ export class TokenTransfer let vaa = undefined; if (receipt.state == TransferState.SourceFinalized) { - if (!receipt.attestation || !receipt.attestation.emitter) throw "Invalid state transition"; + if (!receipt.attestation || !receipt.attestation.emitter) + throw "Invalid state transition: no attestation id"; vaa = xfer.vaas && xfer.vaas.length > 0 && xfer.vaas[0]!.vaa ? xfer.vaas[0].vaa : undefined; // we need to get the attestation so we can deliver it @@ -463,8 +471,7 @@ export class TokenTransfer // if the token id is actually native to the destination, return it if (lookup.chain === dstChain.chain) { - const { chain, address } = lookup; - return { chain, address: address.toNative(chain) } as TokenId; + return lookup as TokenId; } // otherwise, figure out what the token address representing the wormhole-wrapped token we're transferring @@ -478,9 +485,7 @@ export class TokenTransfer const { chain, address } = vaa.payload.to; const { tokenBridgeRelayer } = wh.config.chains[chain]!.contracts; - const relayerAddress = tokenBridgeRelayer - ? nativeChainAddress(chain, tokenBridgeRelayer).address.toUniversalAddress() - : null; + const relayerAddress = tokenBridgeRelayer ? toUniversal(chain, tokenBridgeRelayer) : null; return ( vaa.payloadName === "TransferWithPayload" && @@ -613,7 +618,10 @@ export class TokenTransfer }, }), ); - _transfer.to = nativeChainAddress(_transfer.to.chain, dstChain.config.contracts!.translator!); + _transfer.to = Wormhole.chainAddress( + _transfer.to.chain, + dstChain.config.contracts!.translator!, + ); } return _transfer; diff --git a/connect/src/tasks.ts b/connect/src/tasks.ts index 75b82cc11..0154e7e0f 100644 --- a/connect/src/tasks.ts +++ b/connect/src/tasks.ts @@ -1,4 +1,4 @@ -import { Chain, Network, Platform } from "@wormhole-foundation/sdk-base"; +import { PlatformToChains, Chain, Network, Platform } from "@wormhole-foundation/sdk-base"; import { GatewayTransferMsg, GatewayTransferWithPayloadMsg, @@ -14,7 +14,6 @@ import { isTransactionIdentifier, } from "@wormhole-foundation/sdk-definitions"; import { DEFAULT_TASK_TIMEOUT } from "./config"; -import { PlatformToChains } from "@wormhole-foundation/sdk-base/src"; // A task is a retryable function, it should return a Thing or null for a failure case // It should throw on a permanent failure instead of retrying diff --git a/connect/src/wormhole.ts b/connect/src/wormhole.ts index 47ee181b6..ce286a15f 100644 --- a/connect/src/wormhole.ts +++ b/connect/src/wormhole.ts @@ -5,6 +5,7 @@ import { PlatformToChains, chainToPlatform, circle, + ChainToPlatform, } from "@wormhole-foundation/sdk-base"; import { ChainAddress, @@ -22,10 +23,10 @@ import { deserialize, toNative, } from "@wormhole-foundation/sdk-definitions"; -import { WormholeConfig, CONFIG, DEFAULT_TASK_TIMEOUT } from "./config"; +import { getCircleAttestationWithRetry } from "./circle-api"; +import { CONFIG, DEFAULT_TASK_TIMEOUT, WormholeConfig } from "./config"; import { CircleTransfer } from "./protocols/cctpTransfer"; import { TokenTransfer } from "./protocols/tokenTransfer"; -import { getCircleAttestationWithRetry } from "./circle-api"; import { retry } from "./tasks"; import { TransactionStatus, @@ -34,7 +35,6 @@ import { getVaaBytesWithRetry, getVaaWithRetry, } from "./whscan-api"; -import { ChainToPlatform } from "@wormhole-foundation/sdk-base/src"; type PlatformMap = Map>; type ChainMap = Map< @@ -356,7 +356,7 @@ export class Wormhole { * @returns The ChainAddress */ static chainAddress(chain: C, address: string): ChainAddress { - return { chain, address: toNative(chain, address) }; + return { chain, address: Wormhole.parseAddress(chain, address) }; } /** diff --git a/core/base/src/constants/index.ts b/core/base/src/constants/index.ts index 8e31a3cd1..64c119894 100644 --- a/core/base/src/constants/index.ts +++ b/core/base/src/constants/index.ts @@ -18,6 +18,7 @@ export { isPlatform, platformToChains, chainToPlatform, + platformToAddressFormat, } from "./platforms"; export * as platform from "./platforms"; diff --git a/core/definitions/__tests__/circleMessage.ts b/core/definitions/__tests__/circleMessage.ts index 8a2da75fe..6eb8579ed 100644 --- a/core/definitions/__tests__/circleMessage.ts +++ b/core/definitions/__tests__/circleMessage.ts @@ -1,7 +1,6 @@ -import { deserializeLayout, circle, encoding } from "@wormhole-foundation/sdk-base"; +import { deserializeLayout, circle, encoding, contracts } from "@wormhole-foundation/sdk-base"; import { circleMessageLayout } from "../src/protocols/cctp"; import { UniversalAddress } from "../src"; -import { circleContracts } from "@wormhole-foundation/sdk-base/src/constants/contracts"; const ethAddressToUniversal = (address: string) => { return new UniversalAddress("00".repeat(12) + address.slice(2)); @@ -18,20 +17,16 @@ describe("Circle Message tests", function () { const toChain = "Avalanche"; // same sender and receiver - const accountSender = ethAddressToUniversal( - "0x6603b4a7e29dfbdb6159c395a915e74757c1fb13", - ); + const accountSender = ethAddressToUniversal("0x6603b4a7e29dfbdb6159c395a915e74757c1fb13"); const actualSender = ethAddressToUniversal( - circleContracts("Testnet", fromChain).tokenMessenger, + contracts.circleContracts("Testnet", fromChain).tokenMessenger, ); const actualReceiver = ethAddressToUniversal( - circleContracts("Testnet", toChain).tokenMessenger, + contracts.circleContracts("Testnet", toChain).tokenMessenger, ); - const tokenAddress = ethAddressToUniversal( - circle.usdcContract("Testnet", fromChain), - ); + const tokenAddress = ethAddressToUniversal(circle.usdcContract("Testnet", fromChain)); const decoded = deserializeLayout(circleMessageLayout, orig); expect(decoded.sourceDomain).toEqual(fromChain); diff --git a/core/definitions/src/address.ts b/core/definitions/src/address.ts index 4ed78407b..8aa8f6674 100644 --- a/core/definitions/src/address.ts +++ b/core/definitions/src/address.ts @@ -1,9 +1,9 @@ import { Chain, - isChain, + ChainToPlatform, Platform, chainToPlatform, - ChainToPlatform, + platformToAddressFormat, } from "@wormhole-foundation/sdk-base"; //TODO BRRRR circular include!! @@ -42,22 +42,16 @@ declare global { export type MappedPlatforms = keyof WormholeNamespace.PlatformToNativeAddressMapping; -type ChainOrPlatformToPlatform = T extends Chain - ? ChainToPlatform - : T; - type GetNativeAddress = T extends MappedPlatforms ? WormholeNamespace.PlatformToNativeAddressMapping[T] : never; -export type NativeAddress = GetNativeAddress< - ChainOrPlatformToPlatform ->; +export type NativeAddress = GetNativeAddress>; -export type UniversalOrNative = UniversalAddress | NativeAddress; +export type UniversalOrNative = UniversalAddress | NativeAddress; -export type AccountAddress = UniversalOrNative; -export type TokenAddress = UniversalOrNative | "native"; +export type AccountAddress = UniversalOrNative; +export type TokenAddress = UniversalOrNative | "native"; export type ChainAddress = { readonly chain: C; @@ -78,24 +72,25 @@ export function registerNative

(platform: P, ctr: NativeAddre nativeFactory.set(platform, ctr); } -export function nativeIsRegistered(chainOrPlatform: T): boolean { - const platform: Platform = isChain(chainOrPlatform) - ? chainToPlatform.get(chainOrPlatform)! - : chainOrPlatform; - +export function nativeIsRegistered(chain: C): boolean { + const platform: Platform = chainToPlatform.get(chain); return nativeFactory.has(platform); } -export function toNative( - chainOrPlatform: T, +export function toNative( + chain: C, ua: UniversalAddress | string | Uint8Array, -): NativeAddress { - const platform: Platform = isChain(chainOrPlatform) - ? chainToPlatform.get(chainOrPlatform)! - : chainOrPlatform; - +): NativeAddress { + const platform: Platform = chainToPlatform.get(chain); const nativeCtr = nativeFactory.get(platform); if (!nativeCtr) throw new Error(`No native address type registered for platform ${platform}`); + return new nativeCtr(ua) as unknown as NativeAddress; +} - return new nativeCtr(ua) as unknown as NativeAddress; +export function toUniversal( + chain: C, + address: string | Uint8Array, +): UniversalAddress { + const platform: Platform = chainToPlatform.get(chain); + return new UniversalAddress(address, platformToAddressFormat.get(platform)); } diff --git a/core/definitions/src/layout-items/circle.ts b/core/definitions/src/layout-items/circle.ts index e37d7f2eb..d1c59a908 100644 --- a/core/definitions/src/layout-items/circle.ts +++ b/core/definitions/src/layout-items/circle.ts @@ -1,6 +1,4 @@ -import { circle } from "@wormhole-foundation/sdk-base"; -import { UintLayoutItem } from "@wormhole-foundation/sdk-base/dist/cjs"; -import { CustomConversion } from "@wormhole-foundation/sdk-base/src"; +import { UintLayoutItem, CustomConversion, circle } from "@wormhole-foundation/sdk-base"; export const circleDomainItem = { binary: "uint", diff --git a/core/definitions/src/protocol.ts b/core/definitions/src/protocol.ts index 70b31d0ea..0ab176388 100644 --- a/core/definitions/src/protocol.ts +++ b/core/definitions/src/protocol.ts @@ -28,7 +28,10 @@ export type ProtocolImplementation< : never; export interface ProtocolInitializer

{ - fromRpc(rpc: RpcConnection

, config: ChainsConfig): ProtocolImplementation; + fromRpc( + rpc: RpcConnection

, + config: ChainsConfig, + ): Promise>; } const protocolFactory = new Map< @@ -74,7 +77,7 @@ export function getProtocolInitializer

; + return pctr; } export const create = ( @@ -84,5 +87,5 @@ export const create = , ): Promise => { const pctr = getProtocolInitializer(platform, protocol); - return pctr.fromRpc(rpc, config) as Promise; + return pctr.fromRpc(rpc, config); }; diff --git a/core/definitions/src/protocols/core.ts b/core/definitions/src/protocols/core.ts index 9755e9b38..f0723574b 100644 --- a/core/definitions/src/protocols/core.ts +++ b/core/definitions/src/protocols/core.ts @@ -1,10 +1,10 @@ -import { Network, Platform } from "@wormhole-foundation/sdk-base"; -import { PlatformToChains } from "@wormhole-foundation/sdk-base/src"; +import { PlatformToChains, Network, Platform } from "@wormhole-foundation/sdk-base"; import { AccountAddress } from "../address"; import { WormholeMessageId } from "../attestation"; import { TxHash } from "../types"; import { UnsignedTransaction } from "../unsignedTransaction"; import { VAA } from "../vaa"; + export interface WormholeCore< N extends Network, P extends Platform, diff --git a/core/definitions/src/protocols/ibc.ts b/core/definitions/src/protocols/ibc.ts index 83512fa17..308104313 100644 --- a/core/definitions/src/protocols/ibc.ts +++ b/core/definitions/src/protocols/ibc.ts @@ -1,13 +1,12 @@ import { Chain, ChainId, - ChainToPlatform, Network, Platform, PlatformToChains, encoding, toChain, - toChainId + toChainId, } from "@wormhole-foundation/sdk-base"; import { AccountAddress, ChainAddress, NativeAddress, TokenAddress } from "../address"; import { IbcMessageId, WormholeMessageId } from "../attestation"; @@ -51,33 +50,23 @@ export interface GatewayTransferWithPayloadMsg { // GatewayIBCTransferMsg is the message sent in the memo of an IBC transfer // to be decoded and executed by the Gateway contract. export interface GatewayIbcTransferMsg { - gateway_ibc_token_bridge_payload: - | GatewayTransferMsg - | GatewayTransferWithPayloadMsg; + gateway_ibc_token_bridge_payload: GatewayTransferMsg | GatewayTransferWithPayloadMsg; } -export function isGatewayTransferMsg( - thing: GatewayTransferMsg | any, -): thing is GatewayTransferMsg { +export function isGatewayTransferMsg(thing: GatewayTransferMsg | any): thing is GatewayTransferMsg { return (thing).gateway_transfer !== undefined; } export function isGatewayTransferWithPayloadMsg( thing: GatewayTransferWithPayloadMsg | any, ): thing is GatewayTransferWithPayloadMsg { - return ( - (thing).gateway_transfer_with_payload !== - undefined - ); + return (thing).gateway_transfer_with_payload !== undefined; } export function isGatewayIbcTransferMsg( thing: GatewayIbcTransferMsg | any, ): thing is GatewayIbcTransferMsg { - return ( - (thing).gateway_ibc_token_bridge_payload !== - undefined - ); + return (thing).gateway_ibc_token_bridge_payload !== undefined; } export function isGatewayTransferDetails( @@ -94,18 +83,13 @@ export function isGatewayTransferDetails( // Get the underlying payload from a gateway message // without prefix export function toGatewayMsg( - msg: - | GatewayTransferMsg - | GatewayTransferWithPayloadMsg - | GatewayIbcTransferMsg - | string, + msg: GatewayTransferMsg | GatewayTransferWithPayloadMsg | GatewayIbcTransferMsg | string, ): GatewayMsg { if (typeof msg === "string") msg = JSON.parse(msg); if (isGatewayIbcTransferMsg(msg)) msg = msg.gateway_ibc_token_bridge_payload; if (isGatewayTransferMsg(msg)) return msg.gateway_transfer; - if (isGatewayTransferWithPayloadMsg(msg)) - return msg.gateway_transfer_with_payload; + if (isGatewayTransferWithPayloadMsg(msg)) return msg.gateway_transfer_with_payload; throw new Error(`Unrecognized payload: ${msg}`); } @@ -115,9 +99,7 @@ export function gatewayTransferMsg( ): GatewayTransferMsg | GatewayTransferWithPayloadMsg { if (isGatewayTransferDetails(gtd)) { // If we've already got a payload, b64 encode it so it works in json - const _payload = gtd.payload - ? encoding.b64.encode(gtd.payload) - : undefined; + const _payload = gtd.payload ? encoding.b64.encode(gtd.payload) : undefined; // Encode the payload so the gateway contract knows where to forward the // newly minted tokens @@ -143,7 +125,7 @@ export function gatewayTransferMsg( export function makeGatewayTransferMsg( chain: CN, - recipient: NativeAddress> | string, + recipient: NativeAddress | string, fee: bigint = 0n, nonce: number, payload?: string, @@ -153,8 +135,8 @@ export function makeGatewayTransferMsg( const address = typeof recipient === "string" ? recipient - // @ts-ignore - : encoding.b64.encode(recipient.toString()); + : // @ts-ignore + encoding.b64.encode(recipient.toString()); const common = { chain: toChainId(chain), @@ -165,8 +147,8 @@ export function makeGatewayTransferMsg( const msg: GatewayTransferWithPayloadMsg | GatewayTransferMsg = payload ? ({ - gateway_transfer_with_payload: { ...common, payload: payload }, - } as GatewayTransferWithPayloadMsg) + gateway_transfer_with_payload: { ...common, payload: payload }, + } as GatewayTransferWithPayloadMsg) : ({ gateway_transfer: { ...common } } as GatewayTransferMsg); return msg; @@ -181,9 +163,7 @@ export interface IbcTransferInfo { pending: boolean; } -export function isIbcTransferInfo( - thing: IbcTransferInfo | any, -): thing is IbcTransferInfo { +export function isIbcTransferInfo(thing: IbcTransferInfo | any): thing is IbcTransferInfo { return ( (thing).id !== undefined && (thing).pending !== undefined && @@ -218,9 +198,7 @@ export interface IbcBridge; + lookupMessageFromIbcMsgId(msg: IbcMessageId): Promise; // Get IbcTransferInfo // TODO: overload diff --git a/core/definitions/src/relayer.ts b/core/definitions/src/relayer.ts index fb1ffbcc0..337cdd695 100644 --- a/core/definitions/src/relayer.ts +++ b/core/definitions/src/relayer.ts @@ -1,15 +1,19 @@ -import { Chain, Platform } from "@wormhole-foundation/sdk-base"; +import { Chain } from "@wormhole-foundation/sdk-base"; import { TokenAddress } from "./address"; -export interface Relayer

{ +export interface Relayer { relaySupported(chain: Chain): boolean; - getRelayerFee(sourceChain: Chain, destChain: Chain, tokenId: TokenAddress

): Promise; + getRelayerFee( + sourceChain: Chain, + destChain: Chain, + tokenId: TokenAddress, + ): Promise; // TODO: What should this be named? // I don't think it should return an UnisgnedTransaction // rather it should take some signing callbacks and // a ref to track the progress startTransferWithRelay( - token: TokenAddress

, + token: TokenAddress, amount: bigint, toNativeToken: string, sendingChain: Chain, @@ -20,13 +24,13 @@ export interface Relayer

{ ): Promise; calculateNativeTokenAmt( destChain: Chain, - tokenId: TokenAddress

, + tokenId: TokenAddress, amount: bigint, walletAddress: string, ): Promise; calculateMaxSwapAmount( destChain: Chain, - tokenId: TokenAddress

, + tokenId: TokenAddress, walletAddress: string, ): Promise; } diff --git a/core/definitions/src/testing/mocks/tokenBridge.ts b/core/definitions/src/testing/mocks/tokenBridge.ts index 93b4de31f..9bd7264e6 100644 --- a/core/definitions/src/testing/mocks/tokenBridge.ts +++ b/core/definitions/src/testing/mocks/tokenBridge.ts @@ -1,4 +1,4 @@ -import { Network, Platform } from "@wormhole-foundation/sdk-base"; +import { PlatformToChains, Network, Platform } from "@wormhole-foundation/sdk-base"; import { TokenAddress, ChainAddress, @@ -7,7 +7,6 @@ import { TokenBridge, UnsignedTransaction, } from "../.."; -import { PlatformToChains } from "@wormhole-foundation/sdk-base/src"; //export function mockTokenBridgeFactory( // p: Platform, diff --git a/core/definitions/src/testing/utils/address.ts b/core/definitions/src/testing/utils/address.ts index c906f594b..3efc3d6e1 100644 --- a/core/definitions/src/testing/utils/address.ts +++ b/core/definitions/src/testing/utils/address.ts @@ -1,12 +1,11 @@ -import crypto from "crypto"; import { Chain, - Platform, chainToPlatform, encoding, isPlatform, platformToChains, } from "@wormhole-foundation/sdk-base"; +import crypto from "crypto"; import { ChainAddress, NativeAddress, UniversalAddress, toNative } from "../../"; // return a random buffer of length n @@ -57,7 +56,7 @@ export function makeUniversalAddress(chain: Chain): UniversalAddress { return new UniversalAddress("0x" + nativeAddress.padStart(64, "0")); } // make a random NativeAddress for a given chain -export function makeNativeAddress(chain: T): NativeAddress { +export function makeNativeAddress(chain: T): NativeAddress { let cn: Chain; if (isPlatform(chain)) { // just grab the first one diff --git a/core/definitions/src/types.ts b/core/definitions/src/types.ts index 72d741bfb..c1b4a3048 100644 --- a/core/definitions/src/types.ts +++ b/core/definitions/src/types.ts @@ -14,11 +14,8 @@ import { rpc, toChainId, } from "@wormhole-foundation/sdk-base"; -import { ChainAddress, NativeAddress, toNative } from "./address"; +import { ChainAddress } from "./address"; import { Contracts, getContracts } from "./contracts"; -import { Signer, isSigner } from "./signer"; - -import { UniversalAddress } from "./universalAddress"; export type TxHash = string; export type SequenceId = bigint; @@ -37,45 +34,6 @@ export type Balances = { [key: string]: bigint | null; }; -export function nativeChainAddress( - chain: C, - address: UniversalAddress | Uint8Array | string, -): ChainAddress; - -export function nativeChainAddress( - s: Signer | TokenId | C, - a?: UniversalAddress | Uint8Array | string, -): ChainAddress { - let chain: C; - let address: NativeAddress; - - // its a chain address - if (a) { - // We might be passed a universal address as a string - // First try to decode it as native, otherwise try - // to decode it as universal and convert it to native - chain = s as C; - try { - address = toNative(chain, a); - } catch { - address = UniversalAddress.instanceof(a) - ? a.toNative(chain) - : new UniversalAddress(a).toNative(chain); - } - } else if (isSigner(s)) { - chain = s.chain(); - address = toNative(s.chain(), s.address()); - } else if (isTokenId(s)) { - // otherwise TokenId - chain = s.chain; - address = s.address.toNative(s.chain) as NativeAddress; - } else { - throw new Error("Invalid nativeChainAddress parameters"); - } - - return { chain, address }; -} - // Fully qualifier Transaction ID export type TransactionId = { chain: C; txid: TxHash }; export function isTransactionIdentifier(thing: TransactionId | any): thing is TransactionId { diff --git a/examples/src/cosmos.ts b/examples/src/cosmos.ts index 50f40a44d..802ebbac8 100644 --- a/examples/src/cosmos.ts +++ b/examples/src/cosmos.ts @@ -1,11 +1,11 @@ import { + Network, GatewayTransfer, GatewayTransferDetails, Platform, TokenId, Wormhole, normalizeAmount, - toNative, } from "@wormhole-foundation/connect-sdk"; // Import the platform specific packages import { CosmwasmPlatform, CosmwasmPlatformType } from "@wormhole-foundation/connect-sdk-cosmwasm"; @@ -13,12 +13,11 @@ import { EvmPlatform } from "@wormhole-foundation/connect-sdk-evm"; import { TransferStuff, getStuff } from "./helpers"; -import "@wormhole-foundation/connect-sdk-evm-core"; -import "@wormhole-foundation/connect-sdk-evm-tokenbridge"; import "@wormhole-foundation/connect-sdk-cosmwasm-core"; -import "@wormhole-foundation/connect-sdk-cosmwasm-tokenbridge"; import "@wormhole-foundation/connect-sdk-cosmwasm-ibc"; -import { Network } from "@wormhole-foundation/sdk-base/src"; +import "@wormhole-foundation/connect-sdk-cosmwasm-tokenbridge"; +import "@wormhole-foundation/connect-sdk-evm-core"; +import "@wormhole-foundation/connect-sdk-evm-tokenbridge"; // We're going to transfer into, around, and out of the Cosmos ecosystem // First on Avalanche, transparently through gateway and over IBC to Cosmoshub @@ -73,7 +72,7 @@ import { Network } from "@wormhole-foundation/sdk-base/src"; const { denom } = route1.ibcTransfers![0]!.data; // Lookup the Gateway representation of the wrappd token - const cosmosTokenAddress = toNative("Wormchain", denom); + const cosmosTokenAddress = Wormhole.parseAddress("Wormchain", denom); //console.log("Wrapped Token: ", cosmosTokenAddress.toString()); // Transfer Gateway factory tokens over IBC through gateway to another Cosmos chain diff --git a/examples/src/helpers/helpers.ts b/examples/src/helpers/helpers.ts index fb4d4427f..59471f067 100644 --- a/examples/src/helpers/helpers.ts +++ b/examples/src/helpers/helpers.ts @@ -1,4 +1,5 @@ import { + Network, ChainAddress, ChainContext, Platform, @@ -11,12 +12,11 @@ import { api, tasks, } from "@wormhole-foundation/connect-sdk"; -import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing"; -//import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing"; +// Importing from src so we dont have to rebuild to see debug stuff in signer +import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing"; import { getEvmSigner } from "@wormhole-foundation/connect-sdk-evm/src/testing"; import { getSolanaSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing"; -import { Network } from "@wormhole-foundation/sdk-base/src"; // read in from `.env` require("dotenv").config(); diff --git a/package-lock.json b/package-lock.json index cd00b46ba..0f4f6553f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8788,11 +8788,11 @@ }, "platforms/aptos": { "name": "@wormhole-foundation/connect-sdk-aptos", - "version": "0.2.6", + "version": "0.3.0-beta.3", "extraneous": true, "license": "Apache-2.0", "dependencies": { - "@wormhole-foundation/connect-sdk": "^0.2.6", + "@wormhole-foundation/connect-sdk": "^0.3.0-beta.3", "aptos": "1.5.0" }, "engines": { @@ -8801,12 +8801,12 @@ }, "platforms/aptos/protocols/core": { "name": "@wormhole-foundation/connect-sdk-aptos-core", - "version": "0.2.6", + "version": "0.3.0-beta.3", "extraneous": true, "license": "Apache-2.0", "dependencies": { - "@wormhole-foundation/connect-sdk": "^0.2.6", - "@wormhole-foundation/connect-sdk-aptos": "^0.2.6" + "@wormhole-foundation/connect-sdk": "^0.3.0-beta.3", + "@wormhole-foundation/connect-sdk-aptos": "^0.3.0-beta.3" }, "devDependencies": { "nock": "^13.3.3" @@ -8817,13 +8817,13 @@ }, "platforms/aptos/protocols/tokenBridge": { "name": "@wormhole-foundation/connect-sdk-aptos-tokenbridge", - "version": "0.2.6", + "version": "0.3.0-beta.3", "extraneous": true, "license": "Apache-2.0", "dependencies": { - "@wormhole-foundation/connect-sdk": "^0.2.6", - "@wormhole-foundation/connect-sdk-solana": "^0.2.6", - "@wormhole-foundation/connect-sdk-solana-core": "^0.2.6" + "@wormhole-foundation/connect-sdk": "^0.3.0-beta.3", + "@wormhole-foundation/connect-sdk-solana": "^0.3.0-beta.3", + "@wormhole-foundation/connect-sdk-solana-core": "^0.3.0-beta.3" }, "devDependencies": { "nock": "^13.3.3" diff --git a/package.json b/package.json index 659b5fd4f..12bf3c8d0 100644 --- a/package.json +++ b/package.json @@ -57,10 +57,9 @@ "examples" ], "unreleased": [ - "tokenRegistry", - "examples", "platforms/aptos", "platforms/aptos/protocols/core", - "platforms/aptos/protocols/tokenBridge" + "platforms/aptos/protocols/tokenBridge", + "tokenRegistry" ] } \ No newline at end of file diff --git a/platforms/aptos/package.json b/platforms/aptos/package.json index cce4f083a..dc0ddc1e1 100644 --- a/platforms/aptos/package.json +++ b/platforms/aptos/package.json @@ -1,6 +1,6 @@ { "name": "@wormhole-foundation/connect-sdk-aptos", - "version": "0.2.6", + "version": "0.3.0-beta.3", "repository": { "type": "git", "url": "git+https://github.com/wormhole-foundation/connect-sdk.git" @@ -43,7 +43,7 @@ "prettier": "prettier --write ./src" }, "dependencies": { - "@wormhole-foundation/connect-sdk": "^0.2.6", + "@wormhole-foundation/connect-sdk": "^0.3.0-beta.3", "aptos": "1.5.0" } } \ No newline at end of file diff --git a/platforms/aptos/protocols/core/package.json b/platforms/aptos/protocols/core/package.json index 41eeaa74a..d07f00b15 100644 --- a/platforms/aptos/protocols/core/package.json +++ b/platforms/aptos/protocols/core/package.json @@ -1,6 +1,6 @@ { "name": "@wormhole-foundation/connect-sdk-aptos-core", - "version": "0.2.6", + "version": "0.3.0-beta.3", "repository": { "type": "git", "url": "git+https://github.com/wormhole-foundation/connect-sdk.git" @@ -46,7 +46,7 @@ "nock": "^13.3.3" }, "dependencies": { - "@wormhole-foundation/connect-sdk": "^0.2.6", - "@wormhole-foundation/connect-sdk-aptos": "^0.2.6" + "@wormhole-foundation/connect-sdk": "^0.3.0-beta.3", + "@wormhole-foundation/connect-sdk-aptos": "^0.3.0-beta.3" } } \ No newline at end of file diff --git a/platforms/aptos/protocols/tokenBridge/package.json b/platforms/aptos/protocols/tokenBridge/package.json index 39152659f..4ea8d9982 100644 --- a/platforms/aptos/protocols/tokenBridge/package.json +++ b/platforms/aptos/protocols/tokenBridge/package.json @@ -1,6 +1,6 @@ { "name": "@wormhole-foundation/connect-sdk-aptos-tokenbridge", - "version": "0.2.6", + "version": "0.3.0-beta.3", "repository": { "type": "git", "url": "git+https://github.com/wormhole-foundation/connect-sdk.git" @@ -46,8 +46,8 @@ "nock": "^13.3.3" }, "dependencies": { - "@wormhole-foundation/connect-sdk": "^0.2.6", - "@wormhole-foundation/connect-sdk-solana": "^0.2.6", - "@wormhole-foundation/connect-sdk-solana-core": "^0.2.6" + "@wormhole-foundation/connect-sdk": "^0.3.0-beta.3", + "@wormhole-foundation/connect-sdk-solana": "^0.3.0-beta.3", + "@wormhole-foundation/connect-sdk-solana-core": "^0.3.0-beta.3" } } \ No newline at end of file diff --git a/platforms/aptos/protocols/tokenBridge/src/tokenBridge.ts b/platforms/aptos/protocols/tokenBridge/src/tokenBridge.ts index b0e6f2fa7..3368c8f87 100644 --- a/platforms/aptos/protocols/tokenBridge/src/tokenBridge.ts +++ b/platforms/aptos/protocols/tokenBridge/src/tokenBridge.ts @@ -4,7 +4,6 @@ import { ChainsConfig, Contracts, ErrNotWrapped, - NativeAddress, Network, TokenBridge, TokenId, @@ -109,7 +108,7 @@ export class AptosTokenBridge return false; } - async getWrappedAsset(token: TokenId): Promise> { + async getWrappedAsset(token: TokenId) { const assetFullyQualifiedType = await this.getAssetFullyQualifiedType(token); // check to see if we can get origin info from asset address @@ -148,7 +147,7 @@ export class AptosTokenBridge } } - async getWrappedNative(): Promise> { + async getWrappedNative() { return toNative(this.chain, APTOS_COIN); } @@ -323,7 +322,7 @@ export class AptosTokenBridge ): string { const data = serializeForeignAddressSeeds({ chain: tokenId.chain, - tokenBridgeAddress: toNative(chain, tokenBridgeAddress).toUniversalAddress(), + tokenBridgeAddress: new AptosAddress(tokenBridgeAddress).toUniversalAddress(), tokenId: tokenId.address.toUniversalAddress(), }); return encoding.hex.encode(sha3_256(data), true); diff --git a/platforms/aptos/src/address.ts b/platforms/aptos/src/address.ts index 55a2e8517..e077a4540 100644 --- a/platforms/aptos/src/address.ts +++ b/platforms/aptos/src/address.ts @@ -10,15 +10,6 @@ import { APTOS_SEPARATOR } from "./constants"; import { AptosPlatform } from "./platform"; import { AnyAptosAddress, isValidAptosType } from "./types"; -declare global { - namespace WormholeNamespace { - interface PlatformToNativeAddressMapping { - // @ts-ignore - Aptos: AptosAddress; - } - } -} - export const AptosZeroAddress = "0x"; // Sometimes Aptos addresses will be trimmed of leading 0s @@ -96,4 +87,13 @@ export class AptosAddress implements Address { } } +declare global { + namespace WormholeNamespace { + interface PlatformToNativeAddressMapping { + // @ts-ignore + Aptos: AptosAddress; + } + } +} + registerNative("Aptos", AptosAddress); diff --git a/platforms/aptos/src/platform.ts b/platforms/aptos/src/platform.ts index ff4ffe714..754a6c52d 100644 --- a/platforms/aptos/src/platform.ts +++ b/platforms/aptos/src/platform.ts @@ -1,4 +1,4 @@ -import { Chain, Network, PlatformContext } from "@wormhole-foundation/connect-sdk"; +import { Chain, Network, PlatformContext, Wormhole } from "@wormhole-foundation/connect-sdk"; import { AptosClient } from "aptos"; import { AptosChain } from "./chain"; import { AptosChains, AptosPlatformType, _platform } from "./types"; @@ -9,7 +9,6 @@ import { TokenId, TxHash, chainToPlatform, - nativeChainAddress, nativeChainIds, decimals as nativeDecimals, } from "@wormhole-foundation/connect-sdk"; @@ -35,7 +34,7 @@ export class AptosPlatform extends PlatformContext(network: N, chain: C): TokenId { if (!this.isSupportedChain(chain)) throw new Error(`invalid chain: ${chain}`); - return nativeChainAddress(chain, APTOS_COIN); + return Wormhole.chainAddress(chain, APTOS_COIN); } static isNativeTokenId( diff --git a/platforms/aptos/src/types.ts b/platforms/aptos/src/types.ts index 6dc49d66d..6f23d8bf2 100644 --- a/platforms/aptos/src/types.ts +++ b/platforms/aptos/src/types.ts @@ -6,8 +6,7 @@ export const unusedArbiterFee = 0n; export const _platform: "Aptos" = "Aptos"; export type AptosPlatformType = typeof _platform; export type AptosChains = PlatformToChains; - -export type UniversalOrAptos = UniversalOrNative; +export type UniversalOrAptos = UniversalOrNative; export type AnyAptosAddress = UniversalOrAptos | string | Uint8Array; export type CurrentCoinBalancesResponse = { diff --git a/platforms/cosmwasm/protocols/core/src/index.ts b/platforms/cosmwasm/protocols/core/src/index.ts index 80e6488d6..4ddb693ea 100644 --- a/platforms/cosmwasm/protocols/core/src/index.ts +++ b/platforms/cosmwasm/protocols/core/src/index.ts @@ -1,4 +1,5 @@ import { registerProtocol } from "@wormhole-foundation/connect-sdk"; +import { _platform } from "@wormhole-foundation/connect-sdk-cosmwasm"; import { CosmwasmWormholeCore } from "./wormholeCore"; declare global { @@ -9,6 +10,6 @@ declare global { } } -registerProtocol("Cosmwasm", "WormholeCore", CosmwasmWormholeCore); +registerProtocol(_platform, "WormholeCore", CosmwasmWormholeCore); export * from "./wormholeCore"; diff --git a/platforms/cosmwasm/protocols/ibc/src/index.ts b/platforms/cosmwasm/protocols/ibc/src/index.ts index 30b35cfa2..9e7311676 100644 --- a/platforms/cosmwasm/protocols/ibc/src/index.ts +++ b/platforms/cosmwasm/protocols/ibc/src/index.ts @@ -1,4 +1,5 @@ import { registerProtocol } from "@wormhole-foundation/connect-sdk"; +import { _platform } from "@wormhole-foundation/connect-sdk-cosmwasm"; import { CosmwasmIbcBridge } from "./ibc"; declare global { @@ -9,6 +10,6 @@ declare global { } } -registerProtocol("Cosmwasm", "IbcBridge", CosmwasmIbcBridge); +registerProtocol(_platform, "IbcBridge", CosmwasmIbcBridge); export * from "./ibc"; diff --git a/platforms/cosmwasm/protocols/tokenBridge/src/index.ts b/platforms/cosmwasm/protocols/tokenBridge/src/index.ts index ee0ae2131..e66418c02 100644 --- a/platforms/cosmwasm/protocols/tokenBridge/src/index.ts +++ b/platforms/cosmwasm/protocols/tokenBridge/src/index.ts @@ -1,4 +1,5 @@ import { registerProtocol } from "@wormhole-foundation/connect-sdk"; +import { _platform } from "@wormhole-foundation/connect-sdk-cosmwasm"; import { CosmwasmTokenBridge } from "./tokenBridge"; declare global { @@ -9,6 +10,6 @@ declare global { } } -registerProtocol("Cosmwasm", "TokenBridge", CosmwasmTokenBridge); +registerProtocol(_platform, "TokenBridge", CosmwasmTokenBridge); export * from "./tokenBridge"; diff --git a/platforms/cosmwasm/protocols/tokenBridge/src/tokenBridge.ts b/platforms/cosmwasm/protocols/tokenBridge/src/tokenBridge.ts index ca76f9069..d90040391 100644 --- a/platforms/cosmwasm/protocols/tokenBridge/src/tokenBridge.ts +++ b/platforms/cosmwasm/protocols/tokenBridge/src/tokenBridge.ts @@ -289,7 +289,7 @@ export class CosmwasmTokenBridge const toTranslator = this.translator && - toNative(this.chain, this.translator).toUniversalAddress().equals(vaa.payload.to.address); + new CosmwasmAddress(this.translator).toUniversalAddress().equals(vaa.payload.to.address); const msg = toTranslator ? buildExecuteMsg(senderAddress, this.translator!, { diff --git a/platforms/cosmwasm/src/address.ts b/platforms/cosmwasm/src/address.ts index 953bb57a8..7278bd2d1 100644 --- a/platforms/cosmwasm/src/address.ts +++ b/platforms/cosmwasm/src/address.ts @@ -8,15 +8,6 @@ import { import { CosmwasmPlatform } from "./platform"; import { AnyCosmwasmAddress, _platform } from "./types"; -declare global { - namespace WormholeNamespace { - interface PlatformToNativeAddressMapping { - // @ts-ignore - Cosmwasm: CosmwasmAddress; - } - } -} - /* Categories: @@ -243,4 +234,13 @@ export class CosmwasmAddress implements Address { } } +declare global { + namespace WormholeNamespace { + interface PlatformToNativeAddressMapping { + // @ts-ignore + Cosmwasm: CosmwasmAddress; + } + } +} + registerNative(_platform, CosmwasmAddress); diff --git a/platforms/cosmwasm/src/gateway.ts b/platforms/cosmwasm/src/gateway.ts index 06f478a54..694fdf2b2 100644 --- a/platforms/cosmwasm/src/gateway.ts +++ b/platforms/cosmwasm/src/gateway.ts @@ -34,7 +34,7 @@ export class Gateway extends ChainContext< // for a given chain async getWrappedAsset(token: TokenId): Promise { const tb = await this.getTokenBridge(); - const wrappedAsset = await tb.getWrappedAsset(token); + const wrappedAsset = new CosmwasmAddress(await tb.getWrappedAsset(token)); // Encode the original address to base58 and add it // to the factory address for cw20 style factory token address diff --git a/platforms/cosmwasm/src/platform.ts b/platforms/cosmwasm/src/platform.ts index 5f61b8395..708b8d241 100644 --- a/platforms/cosmwasm/src/platform.ts +++ b/platforms/cosmwasm/src/platform.ts @@ -15,6 +15,7 @@ import { PlatformContext, SignedTx, TxHash, + Wormhole, decimals, nativeChainIds, networkPlatformConfigs, @@ -24,12 +25,7 @@ import { CosmwasmChain } from "./chain"; import { IbcChannels, chainToNativeDenoms, networkChainToChannels } from "./constants"; import { CosmwasmChains, CosmwasmPlatformType, _platform } from "./types"; -import { - Balances, - TokenId, - chainToPlatform, - nativeChainAddress, -} from "@wormhole-foundation/connect-sdk"; +import { Balances, TokenId, chainToPlatform } from "@wormhole-foundation/connect-sdk"; import { CosmwasmAddress } from "./address"; import { IBC_TRANSFER_PORT } from "./constants"; import { AnyCosmwasmAddress } from "./types"; @@ -79,7 +75,7 @@ export class CosmwasmPlatform extends PlatformContext(network: Network, chain: C): TokenId { if (!this.isSupportedChain(chain)) throw new Error(`invalid chain for CosmWasm: ${chain}`); - return nativeChainAddress(chain, this.getNativeDenom(network, chain)); + return Wormhole.chainAddress(chain, this.getNativeDenom(network, chain)); } static isSupportedChain(chain: Chain): boolean { diff --git a/platforms/cosmwasm/src/types.ts b/platforms/cosmwasm/src/types.ts index 8a3a11e28..f03b295e2 100644 --- a/platforms/cosmwasm/src/types.ts +++ b/platforms/cosmwasm/src/types.ts @@ -5,7 +5,7 @@ export const _platform: "Cosmwasm" = "Cosmwasm"; export type CosmwasmPlatformType = typeof _platform; export type CosmwasmChains = PlatformToChains; -export type UniversalOrCosmwasm = UniversalOrNative; +export type UniversalOrCosmwasm = UniversalOrNative; export type AnyCosmwasmAddress = UniversalOrCosmwasm | string | Uint8Array; export interface WrappedRegistryResponse { diff --git a/platforms/evm/__tests__/integration/tokenBridge.test.ts b/platforms/evm/__tests__/integration/tokenBridge.test.ts index a7fdecd6d..4cbe8a67e 100644 --- a/platforms/evm/__tests__/integration/tokenBridge.test.ts +++ b/platforms/evm/__tests__/integration/tokenBridge.test.ts @@ -9,17 +9,13 @@ import { encoding, nativeChainIds, testing, - toNative + toNative, } from '@wormhole-foundation/connect-sdk'; import '@wormhole-foundation/connect-sdk-evm-core'; import '@wormhole-foundation/connect-sdk-evm-tokenbridge'; -import { - EvmChains, - EvmPlatform -} from '../../src'; - +import { EvmChains, EvmPlatform } from '../../src'; import { describe, expect, test } from '@jest/globals'; @@ -95,22 +91,22 @@ const TOKEN_ADDRESSES = { }; const bogusAddress = toNative( - 'Evm', + 'Ethereum', '0x0000c581f595b53c5cb19bd0b3f8da6c935e2ca0', ); const realNativeAddress = toNative( - 'Evm', + 'Ethereum', TOKEN_ADDRESSES['Mainnet']['Ethereum']['wsteth'], ); const realWrappedAddress = toNative( - 'Evm', + 'Ethereum', TOKEN_ADDRESSES['Mainnet']['Ethereum']['wavax'], ); const chain = 'Ethereum'; const destChain = 'Avalanche'; -const sender = toNative('Evm', new Uint8Array(20)); +const sender = toNative('Ethereum', new Uint8Array(20)); const recipient: ChainAddress = { chain: destChain, address: new UniversalAddress(new Uint8Array(32)), @@ -122,7 +118,7 @@ describe('TokenBridge Tests', () => { test('Create TokenBridge', async () => { const rpc = p.getRpc('Ethereum'); - tb = await p.getProtocol("TokenBridge", rpc) + tb = await p.getProtocol('TokenBridge', rpc); expect(tb).toBeTruthy(); }); @@ -228,10 +224,7 @@ describe('TokenBridge Tests', () => { const { transaction } = attestTx; expect(transaction.chainId).toEqual( - nativeChainIds.networkChainToNativeChainId.get( - network, - chain - ), + nativeChainIds.networkChainToNativeChainId.get(network, chain), ); }); @@ -325,10 +318,7 @@ describe('TokenBridge Tests', () => { const { transaction: xferTransaction } = xferTx; expect(xferTransaction.to).toEqual(tbAddress.toString()); expect(xferTransaction.chainId).toEqual( - nativeChainIds.networkChainToNativeChainId.get( - network, - chain - ), + nativeChainIds.networkChainToNativeChainId.get(network, chain), ); }); }); diff --git a/platforms/evm/protocols/cctp/src/automaticCircleBridge.ts b/platforms/evm/protocols/cctp/src/automaticCircleBridge.ts index 97f3a6120..b8109ca0f 100644 --- a/platforms/evm/protocols/cctp/src/automaticCircleBridge.ts +++ b/platforms/evm/protocols/cctp/src/automaticCircleBridge.ts @@ -18,6 +18,7 @@ import { CircleRelayer } from './ethers-contracts'; import { ethers_contracts } from '.'; import { + EvmAddress, EvmChains, EvmPlatform, EvmPlatformType, @@ -92,7 +93,7 @@ export class EvmAutomaticCircleBridge amount: bigint, nativeGas?: bigint, ): AsyncGenerator> { - const senderAddr = sender.toNative(this.chain).toString(); + const senderAddr = new EvmAddress(sender).toString(); const recipientChainId = chainToChainId(recipient.chain); const recipientAddress = recipient.address .toUniversalAddress() diff --git a/platforms/evm/protocols/cctp/src/circleBridge.ts b/platforms/evm/protocols/cctp/src/circleBridge.ts index ca0b4a0a0..6fecab2c8 100644 --- a/platforms/evm/protocols/cctp/src/circleBridge.ts +++ b/platforms/evm/protocols/cctp/src/circleBridge.ts @@ -11,7 +11,6 @@ import { circle, deserializeCircleMessage, encoding, - nativeChainAddress, nativeChainIds, serializeCircleMessage, } from '@wormhole-foundation/connect-sdk'; @@ -204,11 +203,11 @@ export class EvmCircleBridge const sendChain = circle.toCircleChain(circleMsg.sourceDomain); const rcvChain = circle.toCircleChain(circleMsg.destinationDomain); - const token = nativeChainAddress(sendChain, body.burnToken); + const token = { chain: sendChain, address: body.burnToken }; return { - from: nativeChainAddress(sendChain, xferSender), - to: nativeChainAddress(rcvChain, xferReceiver), + from: { chain: sendChain, address: xferSender }, + to: { chain: rcvChain, address: xferReceiver }, token: token, amount: body.amount, message: circleMsg, diff --git a/platforms/evm/protocols/cctp/src/index.ts b/platforms/evm/protocols/cctp/src/index.ts index 6c5ee1662..caa8f0b5f 100644 --- a/platforms/evm/protocols/cctp/src/index.ts +++ b/platforms/evm/protocols/cctp/src/index.ts @@ -1,4 +1,5 @@ import { registerProtocol } from '@wormhole-foundation/connect-sdk'; +import { _platform } from '@wormhole-foundation/connect-sdk-evm'; import { EvmCircleBridge } from './circleBridge'; import { EvmAutomaticCircleBridge } from './automaticCircleBridge'; @@ -10,8 +11,8 @@ declare global { } } -registerProtocol('Evm', 'CircleBridge', EvmCircleBridge); -registerProtocol('Evm', 'AutomaticCircleBridge', EvmAutomaticCircleBridge); +registerProtocol(_platform, 'CircleBridge', EvmCircleBridge); +registerProtocol(_platform, 'AutomaticCircleBridge', EvmAutomaticCircleBridge); export * as ethers_contracts from './ethers-contracts'; export * from './circleBridge'; diff --git a/platforms/evm/protocols/core/src/index.ts b/platforms/evm/protocols/core/src/index.ts index 6fa39364a..57bb0d8d8 100644 --- a/platforms/evm/protocols/core/src/index.ts +++ b/platforms/evm/protocols/core/src/index.ts @@ -1,4 +1,5 @@ import { registerProtocol } from '@wormhole-foundation/connect-sdk'; +import { _platform } from '@wormhole-foundation/connect-sdk-evm'; import { EvmWormholeCore } from './wormholeCore'; declare global { @@ -9,7 +10,7 @@ declare global { } } -registerProtocol('Evm', 'WormholeCore', EvmWormholeCore); +registerProtocol(_platform, 'WormholeCore', EvmWormholeCore); export * as ethers_contracts from './ethers-contracts'; export * from './wormholeCore'; diff --git a/platforms/evm/protocols/tokenBridge/src/automaticTokenBridge.ts b/platforms/evm/protocols/tokenBridge/src/automaticTokenBridge.ts index fdd88bd50..e7b5b7c61 100644 --- a/platforms/evm/protocols/tokenBridge/src/automaticTokenBridge.ts +++ b/platforms/evm/protocols/tokenBridge/src/automaticTokenBridge.ts @@ -75,7 +75,7 @@ export class EvmAutomaticTokenBridge sender: AccountAddress, vaa: TokenBridge.VAA<'TransferWithPayload'>, ): AsyncGenerator> { - const senderAddr = sender.toNative(this.chain).toString(); + const senderAddr = new EvmAddress(sender).toString(); const txReq = await this.tokenBridgeRelayer.completeTransferWithRelay.populateTransaction( serialize(vaa), @@ -113,7 +113,7 @@ export class EvmAutomaticTokenBridge amount: bigint, nativeGas?: bigint, ): AsyncGenerator> { - const senderAddr = sender.toNative(this.chain).toString(); + const senderAddr = new EvmAddress(sender).toString(); const recipientChainId = toChainId(recipient.chain); const recipientAddress = recipient.address @@ -138,7 +138,7 @@ export class EvmAutomaticTokenBridge ); } else { //TODO check for ERC-2612 (permit) support on token? - const tokenAddr = token.toNative(this.chain).toString(); + const tokenAddr = new EvmAddress(token).toString(); const tokenContract = EvmPlatform.getTokenImplementation( this.provider, diff --git a/platforms/evm/protocols/tokenBridge/src/index.ts b/platforms/evm/protocols/tokenBridge/src/index.ts index 9a43b72cd..37bbc2609 100644 --- a/platforms/evm/protocols/tokenBridge/src/index.ts +++ b/platforms/evm/protocols/tokenBridge/src/index.ts @@ -1,4 +1,5 @@ import { registerProtocol } from '@wormhole-foundation/connect-sdk'; +import { _platform } from '@wormhole-foundation/connect-sdk-evm'; import { EvmTokenBridge } from './tokenBridge'; import { EvmAutomaticTokenBridge } from './automaticTokenBridge'; @@ -10,8 +11,8 @@ declare global { } } -registerProtocol('Evm', 'TokenBridge', EvmTokenBridge); -registerProtocol('Evm', 'AutomaticTokenBridge', EvmAutomaticTokenBridge); +registerProtocol(_platform, 'TokenBridge', EvmTokenBridge); +registerProtocol(_platform, 'AutomaticTokenBridge', EvmAutomaticTokenBridge); export * as ethers_contracts from './ethers-contracts'; export * from './tokenBridge'; diff --git a/platforms/evm/protocols/tokenBridge/src/tokenBridge.ts b/platforms/evm/protocols/tokenBridge/src/tokenBridge.ts index 2d1466854..e61551bfd 100644 --- a/platforms/evm/protocols/tokenBridge/src/tokenBridge.ts +++ b/platforms/evm/protocols/tokenBridge/src/tokenBridge.ts @@ -17,6 +17,7 @@ import { Chain, Platform, nativeChainIds, + toNative, } from '@wormhole-foundation/connect-sdk'; import { Provider, TransactionRequest } from 'ethers'; @@ -174,7 +175,7 @@ export class EvmTokenBridge amount: bigint, payload?: Uint8Array, ): AsyncGenerator> { - const senderAddr = sender.toNative(this.chain).toString(); + const senderAddr = new EvmAddress(sender).toString(); const recipientChainId = toChainId(recipient.chain); const recipientAddress = recipient.address .toUniversalAddress() @@ -204,7 +205,7 @@ export class EvmTokenBridge ); } else { //TODO check for ERC-2612 (permit) support on token? - const tokenAddr = token.toNative(this.chain).toString(); + const tokenAddr = new EvmAddress(token).toString(); const tokenContract = EvmPlatform.getTokenImplementation( this.provider, tokenAddr, @@ -254,12 +255,12 @@ export class EvmTokenBridge vaa: TokenBridge.VAA<'Transfer' | 'TransferWithPayload'>, unwrapNative: boolean = true, ): AsyncGenerator> { - const senderAddr = sender.toNative(this.chain).toString(); + const senderAddr = new EvmAddress(sender).toString(); if ( vaa.payloadName === 'TransferWithPayload' && vaa.payload.token.chain !== this.chain ) { - const fromAddr = vaa.payload.from.toNative(this.chain).unwrap(); + const fromAddr = new EvmAddress(vaa.payload.from).unwrap(); if (fromAddr !== senderAddr) throw new Error( `VAA.from (${fromAddr}) does not match sender (${senderAddr})`, @@ -267,7 +268,7 @@ export class EvmTokenBridge } const wrappedNativeAddr = await this.tokenBridge.WETH(); - const tokenAddr = vaa.payload.token.address.toNative(this.chain).unwrap(); + const tokenAddr = new EvmAddress(vaa.payload.token.address).unwrap(); if (tokenAddr === wrappedNativeAddr && unwrapNative) { const txReq = await this.tokenBridge.completeTransferAndUnwrapETH.populateTransaction( @@ -288,9 +289,9 @@ export class EvmTokenBridge } } - async getWrappedNative(): Promise> { + async getWrappedNative() { const address = await this.tokenBridge.WETH(); - return new EvmAddress(address) as NativeAddress; + return toNative(this.chain, address); } private createUnsignedTx( diff --git a/platforms/evm/src/address.ts b/platforms/evm/src/address.ts index dd70f3fae..310891452 100644 --- a/platforms/evm/src/address.ts +++ b/platforms/evm/src/address.ts @@ -97,4 +97,4 @@ declare global { } } -registerNative('Evm', EvmAddress); +registerNative(_platform, EvmAddress); diff --git a/platforms/evm/src/platform.ts b/platforms/evm/src/platform.ts index 56e9b2120..4c230893f 100644 --- a/platforms/evm/src/platform.ts +++ b/platforms/evm/src/platform.ts @@ -7,10 +7,10 @@ import { SignedTx, TokenId, TxHash, + Wormhole, chainToPlatform, decimals, encoding, - nativeChainAddress, nativeChainIds, networkPlatformConfigs, } from '@wormhole-foundation/connect-sdk'; @@ -56,7 +56,7 @@ export class EvmPlatform extends PlatformContext< ): TokenId { if (!EvmPlatform.isSupportedChain(chain)) throw new Error(`invalid chain for EVM: ${chain}`); - return nativeChainAddress(chain, EvmZeroAddress); + return Wormhole.chainAddress(chain, EvmZeroAddress); } static isNativeTokenId( diff --git a/platforms/evm/src/types.ts b/platforms/evm/src/types.ts index 68322198f..e62b7d36f 100644 --- a/platforms/evm/src/types.ts +++ b/platforms/evm/src/types.ts @@ -12,7 +12,7 @@ export const _platform: 'Evm' = 'Evm'; export type EvmPlatformType = typeof _platform; export type EvmChains = PlatformToChains; -export type UniversalOrEvm = UniversalOrNative; +export type UniversalOrEvm = UniversalOrNative; export type AnyEvmAddress = UniversalOrEvm | string | Uint8Array; export const addFrom = (txReq: TransactionRequest, from: string) => ({ diff --git a/platforms/solana/protocols/cctp/src/circleBridge.ts b/platforms/solana/protocols/cctp/src/circleBridge.ts index bafeeecbb..081ad0d90 100644 --- a/platforms/solana/protocols/cctp/src/circleBridge.ts +++ b/platforms/solana/protocols/cctp/src/circleBridge.ts @@ -11,7 +11,6 @@ import { Platform, circle, deserializeCircleMessage, - nativeChainAddress, } from '@wormhole-foundation/connect-sdk'; import { EventParser, Program } from '@project-serum/anchor'; @@ -175,11 +174,11 @@ export class SolanaCircleBridge const sendChain = circle.toCircleChain(msg.sourceDomain); const rcvChain = circle.toCircleChain(msg.destinationDomain); - const token = nativeChainAddress(sendChain, body.burnToken); + const token = { chain: sendChain, address: body.burnToken }; return { - from: nativeChainAddress(sendChain, xferSender), - to: nativeChainAddress(rcvChain, xferReceiver), + from: { chain: sendChain, address: xferSender }, + to: { chain: rcvChain, address: xferReceiver }, token: token, amount: body.amount, message: msg, diff --git a/platforms/solana/protocols/cctp/src/index.ts b/platforms/solana/protocols/cctp/src/index.ts index 9b5b5ffcc..4da41371e 100644 --- a/platforms/solana/protocols/cctp/src/index.ts +++ b/platforms/solana/protocols/cctp/src/index.ts @@ -1,5 +1,6 @@ import { registerProtocol } from '@wormhole-foundation/connect-sdk'; import { SolanaCircleBridge } from './circleBridge'; +import { _platform } from '@wormhole-foundation/connect-sdk-solana'; declare global { namespace WormholeNamespace { @@ -9,7 +10,7 @@ declare global { } } -registerProtocol('Solana', 'CircleBridge', SolanaCircleBridge); +registerProtocol(_platform, 'CircleBridge', SolanaCircleBridge); import { TokenMessenger, TokenMessengerIdl } from './anchor-idl/tokenMessenger'; import { diff --git a/platforms/solana/protocols/cctp/src/utils/instructions/receiveMessage.ts b/platforms/solana/protocols/cctp/src/utils/instructions/receiveMessage.ts index f6aa7e150..11fb68cbc 100644 --- a/platforms/solana/protocols/cctp/src/utils/instructions/receiveMessage.ts +++ b/platforms/solana/protocols/cctp/src/utils/instructions/receiveMessage.ts @@ -12,6 +12,7 @@ import { encoding, serializeCircleMessage, } from '@wormhole-foundation/connect-sdk'; +import { SolanaAddress } from '@wormhole-foundation/connect-sdk-solana'; import { findProgramAddress } from '../accounts'; import { createMessageTransmitterProgramInterface } from '../program'; @@ -32,9 +33,9 @@ export async function createReceiveMessageInstruction( circleMessage.payload.burnToken.toUint8Array(), ); - const receiver = circleMessage.payload.mintRecipient - .toNative('Solana') - .unwrap() as PublicKey; + const receiver = new SolanaAddress( + circleMessage.payload.mintRecipient, + ).unwrap(); const payerPubkey = payer ? new PublicKey(payer) : receiver; diff --git a/platforms/solana/protocols/core/src/core.ts b/platforms/solana/protocols/core/src/core.ts index 242970170..4cbc95d37 100644 --- a/platforms/solana/protocols/core/src/core.ts +++ b/platforms/solana/protocols/core/src/core.ts @@ -8,6 +8,14 @@ import { TransactionResponse, VersionedTransactionResponse, } from '@solana/web3.js'; +import { + SolanaAddress, + AnySolanaAddress, + SolanaChains, + SolanaPlatform, + SolanaPlatformType, + SolanaUnsignedTransaction, +} from '@wormhole-foundation/connect-sdk-solana'; import { ChainId, ChainsConfig, @@ -19,16 +27,7 @@ import { WormholeCore, WormholeMessageId, toChainId, - toNative, } from '@wormhole-foundation/connect-sdk'; -import { - AnySolanaAddress, - SolanaAddress, - SolanaChains, - SolanaPlatform, - SolanaPlatformType, - SolanaUnsignedTransaction, -} from '@wormhole-foundation/connect-sdk-solana'; import { Wormhole as WormholeCoreContract } from './types'; import { createPostMessageInstruction, @@ -72,7 +71,7 @@ export class SolanaWormholeCore static async fromRpc( connection: Connection, config: ChainsConfig, - ): Promise> { + ) { const [network, chain] = await SolanaPlatform.chainFromRpc(connection); const conf = config[chain]!; if (conf.network !== network) @@ -296,7 +295,7 @@ export class SolanaWormholeCore const sequence = acctInfo!.data.readBigUInt64LE(49); const emitterAddr = new Uint8Array(acctInfo!.data.subarray(59, 91)); - const emitter = toNative(this.chain, emitterAddr); + const emitter = new SolanaAddress(emitterAddr); return { chain: this.chain, diff --git a/platforms/solana/protocols/core/src/index.ts b/platforms/solana/protocols/core/src/index.ts index f01387765..08866859b 100644 --- a/platforms/solana/protocols/core/src/index.ts +++ b/platforms/solana/protocols/core/src/index.ts @@ -1,3 +1,4 @@ +import { _platform } from '@wormhole-foundation/connect-sdk-solana'; import { registerProtocol } from '@wormhole-foundation/connect-sdk'; import { SolanaWormholeCore } from './core'; @@ -9,7 +10,7 @@ declare global { } } -registerProtocol('Solana', 'WormholeCore', SolanaWormholeCore); +registerProtocol(_platform, 'WormholeCore', SolanaWormholeCore); export * from './core'; export * from './types'; diff --git a/platforms/solana/protocols/core/src/utils/instructions/governance.ts b/platforms/solana/protocols/core/src/utils/instructions/governance.ts index b019bc296..97ddfe228 100644 --- a/platforms/solana/protocols/core/src/utils/instructions/governance.ts +++ b/platforms/solana/protocols/core/src/utils/instructions/governance.ts @@ -17,7 +17,7 @@ import { derivePostedVaaKey, deriveUpgradeAuthorityKey, } from '../accounts'; -import { utils } from '@wormhole-foundation/connect-sdk-solana'; +import { SolanaAddress, utils } from '@wormhole-foundation/connect-sdk-solana'; export function createSetFeesInstruction( connection: Connection, @@ -244,7 +244,7 @@ export function getUpgradeContractAccounts( ), upgradeAuthority: deriveUpgradeAuthorityKey(wormholeProgramId), spill: new PublicKey(spill === undefined ? payer : spill), - implementation: newContract.toNative('Solana').unwrap(), + implementation: new SolanaAddress(newContract).unwrap(), programData: utils.deriveUpgradeableProgramKey(wormholeProgramId), wormholeProgram: new PublicKey(wormholeProgramId), rent: SYSVAR_RENT_PUBKEY, diff --git a/platforms/solana/protocols/tokenBridge/src/automaticTokenBridge.ts b/platforms/solana/protocols/tokenBridge/src/automaticTokenBridge.ts index 0c0303676..488ad0d95 100644 --- a/platforms/solana/protocols/tokenBridge/src/automaticTokenBridge.ts +++ b/platforms/solana/protocols/tokenBridge/src/automaticTokenBridge.ts @@ -6,7 +6,6 @@ import { ChainId, ChainsConfig, Contracts, - NativeAddress, Network, TokenAddress, VAA, @@ -112,7 +111,7 @@ export class SolanaAutomaticTokenBridge< nativeGas?: bigint | undefined, ) { const nonce = 0; - const senderAddress = sender.toNative(this.chain).unwrap(); + const senderAddress = new SolanaAddress(sender).unwrap(); const recipientAddress = recipient.address .toUniversalAddress() .toUint8Array(); @@ -201,7 +200,7 @@ export class SolanaAutomaticTokenBridge< const tokenAddress = token === 'native' ? new PublicKey(NATIVE_MINT) - : token.toNative(this.chain).unwrap(); + : new SolanaAddress(token).unwrap(); const [{ fee }, { swapRate }, { relayerFeePrecision }] = await Promise.all([ this.getForeignContract(recipient.chain), @@ -225,7 +224,7 @@ export class SolanaAutomaticTokenBridge< const mint = token === 'native' ? new PublicKey(NATIVE_MINT) - : token.toNative(this.chain).unwrap(); + : new SolanaAddress(token).unwrap(); const [{ swapRate, maxNativeSwapAmount }, { swapRate: solSwapRate }] = await Promise.all([ @@ -262,7 +261,7 @@ export class SolanaAutomaticTokenBridge< const mint = token === 'native' ? new PublicKey(NATIVE_MINT) - : token.toNative(this.chain).unwrap(); + : new SolanaAddress(token).unwrap(); const decimals = Number( await SolanaPlatform.getDecimals(this.chain, this.connection, token), @@ -289,7 +288,7 @@ export class SolanaAutomaticTokenBridge< const mint = token === 'native' ? new PublicKey(NATIVE_MINT) - : token.toNative(this.chain).unwrap(); + : new SolanaAddress(token).unwrap(); try { await this.getRegisteredToken(mint); @@ -303,7 +302,7 @@ export class SolanaAutomaticTokenBridge< } } - async getRegisteredTokens(): Promise[]> { + async getRegisteredTokens() { return registeredTokens[this.network].map((addr) => toNative(this.chain, addr), ); diff --git a/platforms/solana/protocols/tokenBridge/src/index.ts b/platforms/solana/protocols/tokenBridge/src/index.ts index f74f0ec9d..85fc1c8a2 100644 --- a/platforms/solana/protocols/tokenBridge/src/index.ts +++ b/platforms/solana/protocols/tokenBridge/src/index.ts @@ -1,3 +1,4 @@ +import { _platform } from '@wormhole-foundation/connect-sdk-solana'; import { registerProtocol } from '@wormhole-foundation/connect-sdk'; import { SolanaTokenBridge } from './tokenBridge'; import { SolanaAutomaticTokenBridge } from './automaticTokenBridge'; @@ -10,8 +11,8 @@ declare global { } } -registerProtocol('Solana', 'TokenBridge', SolanaTokenBridge); -registerProtocol('Solana', 'AutomaticTokenBridge', SolanaAutomaticTokenBridge); +registerProtocol(_platform, 'TokenBridge', SolanaTokenBridge); +registerProtocol(_platform, 'AutomaticTokenBridge', SolanaAutomaticTokenBridge); export * from './tokenBridgeType'; export * from './automaticTokenBridgeType'; diff --git a/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts b/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts index cb053a761..acd1ef999 100644 --- a/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts +++ b/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts @@ -5,7 +5,6 @@ import { ChainsConfig, Contracts, ErrNotWrapped, - NativeAddress, Network, TokenBridge, TokenId, @@ -150,7 +149,7 @@ export class SolanaTokenBridge if (meta === null) return { chain: this.chain, - address: toNative(this.chain, mint.toBytes()), + address: new SolanaAddress(mint.toBytes()).toUniversalAddress(), }; return { @@ -170,7 +169,7 @@ export class SolanaTokenBridge return false; } - async getWrappedAsset(token: TokenId): Promise> { + async getWrappedAsset(token: TokenId) { const mint = deriveWrappedMintKey( this.tokenBridge.programId, toChainId(token.chain), @@ -202,7 +201,7 @@ export class SolanaTokenBridge .catch(() => false); } - async getWrappedNative(): Promise> { + async getWrappedNative() { return toNative(this.chain, NATIVE_MINT.toBase58()); } @@ -511,9 +510,7 @@ export class SolanaTokenBridge const payerPublicKey = new SolanaAddress(sender).unwrap(); // (maybe) ATA for this account - const targetPublicKey = vaa.payload.to.address - .toNative(this.chain) - .unwrap(); + const targetPublicKey = new SolanaAddress(vaa.payload.to.address).unwrap(); const targetAmount = await getMint(this.connection, NATIVE_MINT).then( (info) => @@ -614,7 +611,7 @@ export class SolanaTokenBridge const { blockhash } = await this.connection.getLatestBlockhash(); // Find the token address local to this chain - let nativeAddress = + const nativeAddress = vaa.payload.token.chain === this.chain ? vaa.payload.token.address : (await this.getWrappedAsset(vaa.payload.token)).toUniversalAddress(); @@ -626,9 +623,10 @@ export class SolanaTokenBridge yield* this.coreBridge.postVaa(sender, vaa, blockhash); // Check if this is native wrapped sol + const wrappedNative = new SolanaAddress(await this.getWrappedNative()); const isNativeToken = encoding.bytes.equals( nativeAddress.toUint8Array(), - (await this.getWrappedNative()).toUint8Array(), + wrappedNative.toUint8Array(), ); // redeem vaa and unwrap to native sol from wrapped sol diff --git a/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/endpoint.ts b/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/endpoint.ts index ac9fa2f52..228131e42 100644 --- a/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/endpoint.ts +++ b/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/endpoint.ts @@ -1,16 +1,15 @@ import { + Commitment, Connection, PublicKey, - Commitment, PublicKeyInitData, } from '@solana/web3.js'; -import { utils } from '@wormhole-foundation/connect-sdk-solana'; import { ChainId, + UniversalAddress, toChainId, - toChain, - toNative, } from '@wormhole-foundation/connect-sdk'; +import { utils } from '@wormhole-foundation/connect-sdk-solana'; export function deriveEndpointKey( tokenBridgeProgramId: PublicKeyInitData, @@ -24,7 +23,7 @@ export function deriveEndpointKey( } const emitterAddr = typeof emitterAddress === 'string' - ? toNative(toChain(emitterChain), emitterAddress).toUint8Array() + ? new UniversalAddress(emitterAddress).toUint8Array() : emitterAddress; return utils.deriveAddress( diff --git a/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/wrapped.ts b/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/wrapped.ts index 183a6e944..fbeff79f4 100644 --- a/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/wrapped.ts +++ b/platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/accounts/wrapped.ts @@ -1,31 +1,23 @@ import { + Commitment, Connection, PublicKey, - Commitment, PublicKeyInitData, } from '@solana/web3.js'; +import { ChainId, toChainId } from '@wormhole-foundation/connect-sdk'; import { utils } from '@wormhole-foundation/connect-sdk-solana'; -import { - ChainId, - toChainId, - toChain, - toNative, -} from '@wormhole-foundation/connect-sdk'; export function deriveWrappedMintKey( tokenBridgeProgramId: PublicKeyInitData, tokenChain: number | ChainId, - tokenAddress: Buffer | Uint8Array | string, + tokenAddress: Buffer | Uint8Array, ): PublicKey { if (tokenChain == toChainId('Solana')) { throw new Error( 'tokenChain == CHAIN_ID_SOLANA does not have wrapped mint key', ); } - if (typeof tokenAddress == 'string') { - const parsedAddress = toNative(toChain(tokenChain), tokenAddress); - tokenAddress = parsedAddress.toUint8Array(); - } + return utils.deriveAddress( [ Buffer.from('wrapped'), diff --git a/platforms/solana/src/address.ts b/platforms/solana/src/address.ts index e2efb35c5..0171ddc80 100644 --- a/platforms/solana/src/address.ts +++ b/platforms/solana/src/address.ts @@ -9,15 +9,6 @@ import { import { PublicKey } from '@solana/web3.js'; import { AnySolanaAddress, _platform } from './types'; -declare global { - namespace WormholeNamespace { - interface PlatformToNativeAddressMapping { - // @ts-ignore - Solana: SolanaAddress; - } - } -} - export const SolanaZeroAddress = '11111111111111111111111111111111'; export class SolanaAddress implements Address { @@ -70,4 +61,15 @@ export class SolanaAddress implements Address { } } -registerNative('Solana', SolanaAddress); +// This is required to make `type Z = NativeAddress<"Solana">;` resolve to SolanaAddress +// but outside this module it does _not_ resolve correctly +declare global { + namespace WormholeNamespace { + export interface PlatformToNativeAddressMapping { + // @ts-ignore + Solana: SolanaAddress; + } + } +} + +registerNative(_platform, SolanaAddress); diff --git a/platforms/solana/src/chain.ts b/platforms/solana/src/chain.ts index 5a4861779..3764f1934 100644 --- a/platforms/solana/src/chain.ts +++ b/platforms/solana/src/chain.ts @@ -17,7 +17,7 @@ export class SolanaChain< address: UniversalOrNative, token: UniversalOrNative, ): Promise> { - const mint = token.toNative(this.chain).unwrap(); + const mint = new SolanaAddress(token).unwrap(); const owner = new SolanaAddress(address).unwrap(); const ata = await getAssociatedTokenAddress(mint, owner); diff --git a/platforms/solana/src/platform.ts b/platforms/solana/src/platform.ts index 0a8acfda2..10cf93738 100644 --- a/platforms/solana/src/platform.ts +++ b/platforms/solana/src/platform.ts @@ -7,9 +7,9 @@ import { SignedTx, TokenId, TxHash, + Wormhole, chainToPlatform, decimals, - nativeChainAddress, nativeChainIds, networkPlatformConfigs, } from '@wormhole-foundation/connect-sdk'; @@ -70,7 +70,7 @@ export class SolanaPlatform extends PlatformContext< ): TokenId { if (!SolanaPlatform.isSupportedChain(chain)) throw new Error(`invalid chain: ${chain}`); - return nativeChainAddress(chain, SolanaZeroAddress); + return Wormhole.chainAddress(chain, SolanaZeroAddress); } static isNativeTokenId( diff --git a/platforms/solana/src/types.ts b/platforms/solana/src/types.ts index 3885ec2eb..de1f1fb46 100644 --- a/platforms/solana/src/types.ts +++ b/platforms/solana/src/types.ts @@ -1,8 +1,8 @@ +import { PublicKeyInitData } from '@solana/web3.js'; import { PlatformToChains, UniversalOrNative, } from '@wormhole-foundation/connect-sdk'; -import { PublicKeyInitData } from '@solana/web3.js'; export const unusedNonce = 0; export const unusedArbiterFee = 0n; @@ -11,5 +11,5 @@ export const _platform: 'Solana' = 'Solana'; export type SolanaPlatformType = typeof _platform; export type SolanaChains = PlatformToChains; -export type UniversalOrSolana = UniversalOrNative; +export type UniversalOrSolana = UniversalOrNative; export type AnySolanaAddress = UniversalOrSolana | PublicKeyInitData; diff --git a/tokenRegistry/src/scripts/foreignAssets.ts b/tokenRegistry/src/scripts/foreignAssets.ts index 0349a4fef..7cdfea544 100644 --- a/tokenRegistry/src/scripts/foreignAssets.ts +++ b/tokenRegistry/src/scripts/foreignAssets.ts @@ -43,10 +43,7 @@ export const isSupportedChain = (chain: Chain) => { export const createTokenId = (chain: Chain, address: string) => { if (!isSupportedChain(chain)) return undefined; - return { - chain, - address: toNative(chain, address), - }; + return Wormhole.chainAddress(chain, address); }; export const getForeignAddress = async (wh: Wormhole, chain: Chain, tokenId: TokenId) => {