|
| 1 | +import { |
| 2 | + AccountAddress as AptosAccountAddress, |
| 3 | + Aptos, |
| 4 | + InputGenerateTransactionPayloadData, |
| 5 | + InputScriptData, |
| 6 | + MoveVector, |
| 7 | + U32, |
| 8 | + U64, |
| 9 | + UserTransactionResponse, |
| 10 | +} from "@aptos-labs/ts-sdk"; |
| 11 | +import { |
| 12 | + AptosPlatform, |
| 13 | + AptosUnsignedTransaction, |
| 14 | + type AptosChains, |
| 15 | +} from "@wormhole-foundation/sdk-aptos"; |
| 16 | +import type { |
| 17 | + AccountAddress, |
| 18 | + ChainAddress, |
| 19 | + ChainsConfig, |
| 20 | + Network, |
| 21 | + Platform, |
| 22 | +} from "@wormhole-foundation/sdk-connect"; |
| 23 | +import { |
| 24 | + CircleBridge, |
| 25 | + CircleTransferMessage, |
| 26 | + circle, |
| 27 | + Contracts, |
| 28 | + encoding, |
| 29 | + keccak256, |
| 30 | +} from "@wormhole-foundation/sdk-connect"; |
| 31 | +import { AptosCCTPMoveScripts, aptosCCTPMoveScripts } from "./moveScripts.js"; |
| 32 | + |
| 33 | +export class AptosCircleBridge<N extends Network, C extends AptosChains> |
| 34 | + implements CircleBridge<N, C> |
| 35 | +{ |
| 36 | + readonly usdcId: string; |
| 37 | + readonly tokenMessengerId: string; |
| 38 | + readonly messageTransmitterId: string; |
| 39 | + readonly moveScripts: AptosCCTPMoveScripts; |
| 40 | + |
| 41 | + constructor( |
| 42 | + readonly network: N, |
| 43 | + readonly chain: C, |
| 44 | + readonly provider: Aptos, |
| 45 | + readonly contracts: Contracts, |
| 46 | + ) { |
| 47 | + if (network === "Devnet") throw new Error("CircleBridge not supported on Devnet"); |
| 48 | + |
| 49 | + const usdcId = circle.usdcContract.get(this.network, this.chain); |
| 50 | + if (!usdcId) { |
| 51 | + throw new Error( |
| 52 | + `No USDC contract configured for network=${this.network} chain=${this.chain}`, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + if (!contracts.cctp?.tokenMessenger) |
| 57 | + throw new Error(`Circle Token Messenger contract for domain ${chain} not found`); |
| 58 | + |
| 59 | + if (!contracts.cctp?.messageTransmitter) |
| 60 | + throw new Error(`Circle Message Transmitter contract for domain ${chain} not found`); |
| 61 | + |
| 62 | + if (!aptosCCTPMoveScripts.has(network)) throw new Error("No Aptos CCTP move scripts found"); |
| 63 | + |
| 64 | + this.usdcId = usdcId; |
| 65 | + this.tokenMessengerId = contracts.cctp?.tokenMessenger; |
| 66 | + this.messageTransmitterId = contracts.cctp.messageTransmitter; |
| 67 | + this.moveScripts = aptosCCTPMoveScripts.get(network)!; |
| 68 | + } |
| 69 | + |
| 70 | + async *transfer( |
| 71 | + sender: AccountAddress<C>, |
| 72 | + recipient: ChainAddress, |
| 73 | + amount: bigint, |
| 74 | + ): AsyncGenerator<AptosUnsignedTransaction<N, C>> { |
| 75 | + const destinationDomain = new U32(circle.circleChainId.get(this.network, recipient.chain)!); |
| 76 | + const burnToken = AptosAccountAddress.from(this.usdcId); |
| 77 | + const mintRecipient = AptosAccountAddress.from( |
| 78 | + recipient.address.toUniversalAddress().toUint8Array(), |
| 79 | + ); |
| 80 | + |
| 81 | + const functionArguments = [new U64(amount), destinationDomain, mintRecipient, burnToken]; |
| 82 | + |
| 83 | + const txData: InputScriptData = { |
| 84 | + bytecode: this.moveScripts.depositForBurn, |
| 85 | + functionArguments, |
| 86 | + }; |
| 87 | + |
| 88 | + yield this.createUnsignedTx(txData, "Aptos.CircleBridge.Transfer"); |
| 89 | + } |
| 90 | + |
| 91 | + async isTransferCompleted(message: CircleBridge.Message): Promise<boolean> { |
| 92 | + const sourceBytes = new U32(message.sourceDomain).bcsToBytes(); |
| 93 | + const nonceBytes = new U64(message.nonce).bcsToBytes(); |
| 94 | + const hash = keccak256(new Uint8Array([...sourceBytes, "-".charCodeAt(0), ...nonceBytes])); |
| 95 | + const hashStr = encoding.hex.encode(hash); |
| 96 | + |
| 97 | + const isNonceUsed = await this.provider.view<[boolean]>({ |
| 98 | + payload: { |
| 99 | + function: `${this.messageTransmitterId}::message_transmitter::is_nonce_used`, |
| 100 | + functionArguments: [hashStr], |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + return isNonceUsed[0]; |
| 105 | + } |
| 106 | + |
| 107 | + async *redeem( |
| 108 | + sender: AccountAddress<C>, |
| 109 | + message: CircleBridge.Message, |
| 110 | + attestation: string, |
| 111 | + ): AsyncGenerator<AptosUnsignedTransaction<N, C>> { |
| 112 | + const functionArguments = [ |
| 113 | + MoveVector.U8(CircleBridge.serialize(message)), |
| 114 | + MoveVector.U8(encoding.hex.decode(attestation)), |
| 115 | + ]; |
| 116 | + |
| 117 | + const txData: InputScriptData = { |
| 118 | + bytecode: this.moveScripts.handleReceiveMessage, |
| 119 | + functionArguments, |
| 120 | + }; |
| 121 | + |
| 122 | + yield this.createUnsignedTx(txData, "Aptos.CircleBridge.Redeem"); |
| 123 | + } |
| 124 | + |
| 125 | + async parseTransactionDetails(digest: string): Promise<CircleTransferMessage> { |
| 126 | + const tx = await this.provider.getTransactionByHash({ transactionHash: digest }); |
| 127 | + |
| 128 | + const messageTransmitterId = this.messageTransmitterId.replace(/^0x0+/, "0x"); // remove any leading zeros to match event |
| 129 | + const circleMessageSentEvent = (tx as UserTransactionResponse).events?.find( |
| 130 | + (e) => e.type === `${messageTransmitterId}::message_transmitter::MessageSent`, |
| 131 | + ); |
| 132 | + |
| 133 | + if (!circleMessageSentEvent) { |
| 134 | + throw new Error("No MessageSent event found"); |
| 135 | + } |
| 136 | + |
| 137 | + const circleMessage = encoding.hex.decode(circleMessageSentEvent.data.message); |
| 138 | + |
| 139 | + const [msg, hash] = CircleBridge.deserialize(circleMessage); |
| 140 | + const { payload } = msg; |
| 141 | + |
| 142 | + const xferSender = payload.messageSender; |
| 143 | + const xferReceiver = payload.mintRecipient; |
| 144 | + |
| 145 | + const sendChain = circle.toCircleChain(this.network, msg.sourceDomain); |
| 146 | + const rcvChain = circle.toCircleChain(this.network, msg.destinationDomain); |
| 147 | + |
| 148 | + const token = { chain: sendChain, address: payload.burnToken }; |
| 149 | + |
| 150 | + return { |
| 151 | + from: { chain: sendChain, address: xferSender }, |
| 152 | + to: { chain: rcvChain, address: xferReceiver }, |
| 153 | + token: token, |
| 154 | + amount: payload.amount, |
| 155 | + message: msg, |
| 156 | + id: { hash }, |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + static async fromRpc<N extends Network>( |
| 161 | + provider: Aptos, |
| 162 | + config: ChainsConfig<N, Platform>, |
| 163 | + ): Promise<AptosCircleBridge<N, AptosChains>> { |
| 164 | + const [network, chain] = await AptosPlatform.chainFromRpc(provider); |
| 165 | + const conf = config[chain]!; |
| 166 | + if (conf.network !== network) { |
| 167 | + throw new Error(`Network mismatch: ${conf.network} != ${network}`); |
| 168 | + } |
| 169 | + |
| 170 | + return new AptosCircleBridge(network as N, chain, provider, conf.contracts); |
| 171 | + } |
| 172 | + |
| 173 | + private createUnsignedTx( |
| 174 | + txReq: InputGenerateTransactionPayloadData, |
| 175 | + description: string, |
| 176 | + parallelizable: boolean = false, |
| 177 | + ): AptosUnsignedTransaction<N, C> { |
| 178 | + return new AptosUnsignedTransaction( |
| 179 | + txReq, |
| 180 | + this.network, |
| 181 | + this.chain, |
| 182 | + description, |
| 183 | + parallelizable, |
| 184 | + ); |
| 185 | + } |
| 186 | +} |
0 commit comments