UNPKG

axelar-cgp-ton

Version:

Utilities for Axelar Cross-Chain Gateway Protocol (CGP) on TON blockchain

327 lines (308 loc) 19.5 kB
import * as _ton_core from '@ton/core'; import { Cell, Address, Contract, ContractProvider, Sender } from '@ton/core'; import nacl from 'tweetnacl'; interface WeightedSigner { signer: bigint; weight: bigint; signature: string; } interface WeightedSigners { signers: WeightedSigner[]; threshold: bigint; nonce: bigint; } interface MessageToBeApproved { message: string; sourceChain: string; sourceAddress: string; payloadBuffer: Buffer; executableAddress: Address; destinationChain: string; } interface MessagesToBeApproved { messages: string[]; sourceChains: string[]; sourceAddresses: string[]; payloadBuffers: Buffer[]; executableAddresses: Address[]; destinationChains: string[]; } interface GasInfo { gasEstimationType: bigint; l1FeeScalar: bigint; axelarBaseFee: bigint; relativeGasPrice: bigint; relativeBlobBaseFee: bigint; expressFee: bigint; } type GasInfos = Map<bigint, GasInfo>; declare enum GasEstimationType { Default = 0, OptimismEcotone = 1, OptimismBedrock = 2, Arbitrum = 3, Scroll = 4 } interface OProposal { targetAddress: bigint; nativeTon: bigint; proposalHash: bigint; proposal: Cell; } interface TProposal { targetAddress: bigint; nativeTon: bigint; proposalHash: bigint; proposal: Cell; timelock: number; expectedTargetAddress: Address; } declare function buildMessageIdCell(id: string): Cell; declare function buildCompleteMessageCell(id: string, sourceChainString: string, sourceContractAddressString: string, payloadBuffer: Buffer, contractAddress: Address, destinationChain: string): Cell; declare function buildApproveMessagesMessage(proof: Cell, messages: Cell): Cell; declare function buildSendMessage(destinationChainString: string, destinationContractAddressString: string, payloadBuffer: Buffer): Cell; declare function buildStartSignerRotationMessage(signerRotationHash: Cell, proof: Cell): Cell; declare function buildContractCallMessage(destinationChainString: string, destinationContractAddressString: string, payloadBuffer: Buffer): Cell; declare function buildContractCallMessageChained(destinationChainString: string, destinationContractAddressString: string, payloadBuffer: Buffer): Cell; declare function buildRotateSignersMessage(signers: Cell): Cell; declare function buildRelayerExecuteMessage(messageString: string, relayerAddress: Address, sourceChainString: string, sourceAddressString: string, payloadBuffer: Buffer, contractAddress: Address, destinationChainString: string, payloadHashOverride?: bigint): Cell; declare function buildInitializeSignersMessage(signers: Cell): Cell; declare function buildPayNativeGasForContractCallMessage(sender: Address, destinationChain: string, destinationAddress: string, payloadHexString: string, refundAddress: Address): Cell; declare function buildPayJettonGasForContractCallMessage(jettonMinter: Address, jettonAmount: bigint, destinationChain: string, destinationAddress: string, payloadHexString: string, sender: Address, refundAddress: Address): Cell; declare function buildCollectGasMessage(receiver: Address, amountToCollect: bigint): Cell; declare function buildCollectJettonsMessage(receiver: Address, jettonToSend: bigint, jettonMinter: Address): Cell; declare function buildAddNativeGasMessage(txHash: bigint, refundReceiver: Address): Cell; declare function buildAddJettonGasMessage(txHash: bigint, jettonMinter: Address, jettonToSend: bigint, refundReceiver: Address): Cell; declare function buildNativeRefundMessage(txHash: bigint, refundAddress: Address, refundAmount: bigint): Cell; declare function buildJettonRefundMessage(txHash: bigint, refundAddress: Address, jettonAmount: bigint, jettonMinter: Address): Cell; declare function buildUpdateGasInfoMessage(gasInfos: GasInfos): Cell; declare function buildPayGasMessage(sender: Address, destinationChain: string, destinationAddress: string, payloadHexString: string, refundAddress: Address, estimateOnChain: number, executionGasLimit: bigint): Cell; declare function buildTransferOperatorshipMessage(newOperator: Address): Cell; declare function buildExecuteTimelockProposalPayload(proposalConfig: TProposal, actualTimelock: bigint): Cell; declare function buildExecuteOperatorProposal(proposalConfig: OProposal): Cell; declare function bufferToCellWith4Refs(buffer: Buffer, bytesPerCell?: number): Cell; declare function bufferToCell(buffer: Buffer, bytesPerCell?: number): Cell; type JettonWalletConfig = {}; declare function jettonWalletConfigToCell(config: JettonWalletConfig): Cell; declare class JettonWallet implements Contract { readonly address: Address; readonly init?: { code: Cell; data: Cell; } | undefined; constructor(address: Address, init?: { code: Cell; data: Cell; } | undefined); static createFromAddress(address: Address): JettonWallet; static createFromConfig(config: JettonWalletConfig, code: Cell, workchain?: number): JettonWallet; sendDeploy(provider: ContractProvider, via: Sender, value: bigint): Promise<void>; getJettonBalance(provider: ContractProvider): Promise<bigint>; static transferMessage(jetton_amount: bigint, to: Address, responseAddress: Address, customPayload: Cell | null, forward_ton_amount: bigint, forwardPayload: Cell | null): Cell; sendTransfer(provider: ContractProvider, via: Sender, value: bigint, jetton_amount: bigint, to: Address, responseAddress: Address, customPayload: Cell, forward_ton_amount: bigint, forwardPayload: Cell): Promise<void>; static burnMessage(jetton_amount: bigint, responseAddress: Address, customPayload: Cell | null): Cell; sendBurn(provider: ContractProvider, via: Sender, value: bigint, jetton_amount: bigint, responseAddress: Address, customPayload: Cell): Promise<void>; static withdrawTonsMessage(): Cell; sendWithdrawTons(provider: ContractProvider, via: Sender): Promise<void>; static withdrawJettonsMessage(from: Address, amount: bigint): Cell; sendWithdrawJettons(provider: ContractProvider, via: Sender, from: Address, amount: bigint): Promise<void>; } type JettonMinterContent = { type: 0 | 1; uri: string; }; type JettonMinterConfig = { admin: Address; content: Cell; wallet_code: Cell; }; declare function jettonMinterConfigToCell(config: JettonMinterConfig): Cell; declare function jettonContentToCell(content: JettonMinterContent): Cell; declare class JettonMinter implements Contract { readonly address: Address; readonly init?: { code: Cell; data: Cell; } | undefined; constructor(address: Address, init?: { code: Cell; data: Cell; } | undefined); static createFromAddress(address: Address): JettonMinter; static createFromConfig(config: JettonMinterConfig, code: Cell, workchain?: number): JettonMinter; sendDeploy(provider: ContractProvider, via: Sender, value: bigint): Promise<void>; protected static jettonInternalTransfer(jetton_amount: bigint, forward_ton_amount: bigint, response_addr?: Address, query_id?: number | bigint): Cell; static mintMessage(from: Address, to: Address, jetton_amount: bigint, forward_ton_amount: bigint, total_ton_amount: bigint, query_id?: number | bigint): Cell; sendMint(provider: ContractProvider, via: Sender, to: Address, jetton_amount: bigint, forward_ton_amount: bigint, total_ton_amount: bigint): Promise<void>; static discoveryMessage(owner: Address, include_address: boolean): Cell; sendDiscovery(provider: ContractProvider, via: Sender, owner: Address, include_address: boolean, value?: bigint): Promise<void>; static changeAdminMessage(newOwner: Address): Cell; sendChangeAdmin(provider: ContractProvider, via: Sender, newOwner: Address): Promise<void>; static changeContentMessage(content: Cell): Cell; sendChangeContent(provider: ContractProvider, via: Sender, content: Cell): Promise<void>; getWalletAddress(provider: ContractProvider, owner: Address): Promise<Address>; getJettonData(provider: ContractProvider): Promise<{ totalSupply: bigint; mintable: boolean; adminAddress: Address; content: Cell; walletCode: Cell; }>; getTotalSupply(provider: ContractProvider): Promise<bigint>; getAdminAddress(provider: ContractProvider): Promise<Address>; getContent(provider: ContractProvider): Promise<Cell>; } declare function sendTransfer(provider: ContractProvider, via: Sender, value: bigint, jetton_amount: bigint, to: Address, responseAddress: Address, customPayload: Cell, forward_ton_amount: bigint, forwardPayload: Cell): Promise<void>; /** * Operation codes for the Axelar Gateway contract */ declare const OP_RELAYER_EXECUTE = 8; declare const OP_GATEWAY_EXECUTE = 12; declare const OP_DEPLOY = 0; declare const OP_APPROVE_MESSAGE = 7; declare const OP_APPROVE_MESSAGES = 40; declare const OP_SEND_MESSAGE = 23; declare const OP_MESSAGE_APPROVED = 13; declare const OP_NULLIFIED_SUCESSFULLY = 5; declare const OP_UNNULIFY_MESSAGE = 75; declare const OP_MESSAGE_APPROVED_LOG = 15; declare const OP_MESSAGE_EXECUTED_LOG = 16; declare const OP_CONTRACT_CALL_LOG = 22; declare const OP_JETTON_GAS_PAID_LOG = 38; declare const OP_NATIVE_GAS_PAID_LOG = 33; declare const OP_OPERATORSHIP_TRANSFERED_LOG = 49; declare const OP_INITIALIZE_SIGNERS = 18; declare const OP_VERIFY_PROOF = 19; declare const OP_START_SIGNER_ROTATION = 20; declare const OP_ROTATE_SIGNERS = 21; declare const OP_CALL_CONTRACT = 9; declare const OP_SCHEDULE_TIMELOCK_PROPOSAL = 28; declare const OP_CANCEL_TIMELOCK_PROPOSAL = 29; declare const OP_EXECUTE_TIMELOCK_PROPOSAL = 30; declare const OP_APPROVE_OPERATOR_PROPOSAL = 60; declare const OP_CANCEL_OPERATOR_APPROVAL = 61; declare const OP_EXECUTE_OPERATOR_PROPOSAL = 62; declare const OP_OPERATOR_PROPOSAL_SCHEDULED_LOG = 63; declare const OP_OPERATOR_PROPOSAL_CANCELED_LOG = 64; declare const OP_OPERATOR_PROPOSAL_EXECUTED_LOG = 65; declare const OP_PROPOSAL_SCHEDULED_LOG = 51; declare const OP_PROPOSAL_CANCELED_LOG = 52; declare const OP_PROPOSAL_EXECUTED_LOG = 53; declare const OP_ADD_BALANCE = 34; declare const OP_SUBTRACT = 35; declare const OP_NULLIFY_IF_ALLOWED = 25; declare const OP_COLLECT_GAS = 32; declare const OP_COLLECT_JETTONS = 41; declare const OP_PAY_JETTON_GAS_FOR_CONTRACT_CALL = 39; declare const OP_PAY_NATIVE_GAS_FOR_CONTRACT_CALL = 31; declare const OP_ADD_GAS = 54; declare const OP_ADD_JETTON_GAS = 55; declare const OP_NATIVE_GAS_ADDED_LOG = 56; declare const OP_JETTON_GAS_ADDED_LOG = 57; declare const OP_NATIVE_GAS_COLLECTED_LOG = 58; declare const OP_JETTON_GAS_COLLECTED_LOG = 59; declare const OP_UPDATE_GAS_INFO = 67; declare const OP_GAS_INFO_UPDATED_LOG = 68; declare const OP_PAY_GAS = 69; declare const OP_NATIVE_REFUND = 70; declare const OP_JETTON_REFUND = 71; declare const OP_NATIVE_GAS_REFUNDED_LOG = 72; declare const OP_JETTON_GAS_REFUNDED_LOG = 73; declare const OP_TRANSFER_OWNERSHIP = 49; declare const INIT_SIGNERS_COST: bigint; declare const APPROVE_MESAGE_COST: bigint; declare const APPROVE_MESSAGES_COST: bigint; declare const START_SIGNER_ROTATION_COST: bigint; declare const ROTATE_SIGNERS_COST: bigint; declare const EXECUTABLE_NATIVE_COST: bigint; declare const RELAYER_EXECUTE_COST: bigint; declare const PAY_JETTON_GAS_FOR_CONTRACT_CALL_COST: bigint; declare const EXECUTE_PROPOSAL_COST: bigint; declare const ERR_UNAUTHORIZED = 101; declare const ERR_MALFORMED_SIGNATURE = 102; declare const ERR_INVALID_SIGNER_CONFIG = 109; declare const ERR_SIGNERS_UNINITIALIZED = 103; declare const ERR_INVALID_NONCE = 104; declare const ERR_INVALID_PAYLOAD = 105; declare const ERR_INSUFFICIENT_GAS = 106; declare const ERR_INSUFFICIENT_PROPOSAL_GAS = 107; declare const ERR_INVALID_WORKCHAIN = 333; declare const ERR_INVALID_AMOUNT = 107; declare const ERR_INVALID_SIGNATURE = 108; declare const ERR_INSUFFICIENT_BALANCE = 110; declare const ERR_UNAUTHORIZED_SOURCE_ADDRESS = 111; declare const ERR_UNAUTHORIZED_SOURCE_CHAIN = 112; declare const ERR_INSUFFICIENT_WEIGHT = 113; declare const ERR_DUPLICATE_SIGNER = 114; declare const ERR_PROOF_NOT_SORTED = 115; declare const ERR_SIGNER_CONFIG_NOT_SORTED = 116; declare const ERR_SIGNER_CONFIG_DUPLICATE_SIGNER = 117; declare const ERR_SIGNERS_ALREADY_INITIALIZED = 118; declare const ERR_REDUNDANT_SIGNATURES_PROVIDED = 130; declare const ERR_INVALID_OP_CODE = 131; declare const ERR_INVALID_EXECUTABLE_GAS = 134; declare const GAS_INFO_BITS = 640; declare const GAS_INFO_BYTES: number; declare const CHAIN_HASH_BITS = 256; declare const EPOCH_KEY_LENGTH = 256; declare const NONCE_KEY_LENGTH = 256; declare const SIGNER_CONFIG_HASH_LENGTH = 256; declare const TIMELOCK_PROPOSAL_DISABLED = 0; declare const TIMELOCK_PROPOSAL_SCHEDULED = 1; declare const TIMELOCK_PROPOSAL_EXECUTED = 2; declare const TIMELOCK_PROPOSAL_CANCELED = 3; declare const OPERATOR_PROPOSAL_DISABLED = 0; declare const OPERATOR_PROPOSAL_SCHEDULED = 1; declare const OPERATOR_PROPOSAL_EXECUTED = 2; declare const OPERATOR_PROPOSAL_CANCELED = 3; declare function uint8ArrayToBigInt(arr: Uint8Array): bigint; declare function isHexString(str: string): boolean; declare function hexToUint8Array(hex: string): Uint8Array; declare function isBase64String(str: string): boolean; declare function base64ToUint8Array(base64: string): Uint8Array; /** * Converts a bigint to a Buffer of a fixed byte length. * @param value The bigint to convert. * @param byteLength The target length in bytes. * @returns A Buffer representing the bigint in big-endian order. */ declare function bigintToBuffer(value: bigint, byteLength: number): Buffer; declare function bigIntToUtf8String(value: bigint): string; declare function generateRandomBuffer(bytes: number): Buffer<ArrayBuffer>; declare function keccak256Single(inputBuffer: Buffer): Buffer; declare function computePayloadHash(payloadBuffer: Buffer): bigint; declare function getDeterministicSigners(count: number, sorted: boolean, duplicate: boolean): nacl.SignKeyPair[]; /** * Verifies a single signature. * @param message - The original message string. * @param signatureBase64 - The signature in base64 format. * @param publicKeyBigInt - The public key stored as a BigInt. * @returns true if the signature is valid. */ declare function verifySignature(message: string, signatureBase64: string, publicKeyBigInt: bigint): boolean; /** * Verifies all signatures in the weighted signers structure. * @param weightedSigners - The object returned by getProofForMessage. * @param message - The message that was signed. * @returns true if all signatures are valid. */ declare function verifyProofWeightedSigners(weightedSigners: any, message: string): boolean; declare function getSignatureBase64(message: string, keyPair: nacl.SignKeyPair): string; declare function getEmptySignature(): string; declare function getSignatureData(message: string, keyPair: nacl.SignKeyPair): { signatureSlice: _ton_core.Slice; signerPublicKey: bigint; }; declare function serializeMessagesToBeApproved(messagesToBeApproved: Array<{ message: string; sourceChain: string; sourceAddress: string; payloadBuffer: Buffer; executableAddress: Address; destinationChain: string; }>): Cell; declare function serializeWeightedSigners(data: WeightedSigners): Cell; declare function serializeWeightedSigner(signer: WeightedSigner): Buffer; declare function serializeGasInfo(gasInfo: GasInfo): Buffer; declare function serializeGasInfos(data: GasInfos): Cell; export { APPROVE_MESAGE_COST, APPROVE_MESSAGES_COST, CHAIN_HASH_BITS, EPOCH_KEY_LENGTH, ERR_DUPLICATE_SIGNER, ERR_INSUFFICIENT_BALANCE, ERR_INSUFFICIENT_GAS, ERR_INSUFFICIENT_PROPOSAL_GAS, ERR_INSUFFICIENT_WEIGHT, ERR_INVALID_AMOUNT, ERR_INVALID_EXECUTABLE_GAS, ERR_INVALID_NONCE, ERR_INVALID_OP_CODE, ERR_INVALID_PAYLOAD, ERR_INVALID_SIGNATURE, ERR_INVALID_SIGNER_CONFIG, ERR_INVALID_WORKCHAIN, ERR_MALFORMED_SIGNATURE, ERR_PROOF_NOT_SORTED, ERR_REDUNDANT_SIGNATURES_PROVIDED, ERR_SIGNERS_ALREADY_INITIALIZED, ERR_SIGNERS_UNINITIALIZED, ERR_SIGNER_CONFIG_DUPLICATE_SIGNER, ERR_SIGNER_CONFIG_NOT_SORTED, ERR_UNAUTHORIZED, ERR_UNAUTHORIZED_SOURCE_ADDRESS, ERR_UNAUTHORIZED_SOURCE_CHAIN, EXECUTABLE_NATIVE_COST, EXECUTE_PROPOSAL_COST, GAS_INFO_BITS, GAS_INFO_BYTES, GasEstimationType, type GasInfo, type GasInfos, INIT_SIGNERS_COST, JettonMinter, type JettonMinterConfig, type JettonMinterContent, JettonWallet, type JettonWalletConfig, type MessageToBeApproved, type MessagesToBeApproved, NONCE_KEY_LENGTH, OPERATOR_PROPOSAL_CANCELED, OPERATOR_PROPOSAL_DISABLED, OPERATOR_PROPOSAL_EXECUTED, OPERATOR_PROPOSAL_SCHEDULED, OP_ADD_BALANCE, OP_ADD_GAS, OP_ADD_JETTON_GAS, OP_APPROVE_MESSAGE, OP_APPROVE_MESSAGES, OP_APPROVE_OPERATOR_PROPOSAL, OP_CALL_CONTRACT, OP_CANCEL_OPERATOR_APPROVAL, OP_CANCEL_TIMELOCK_PROPOSAL, OP_COLLECT_GAS, OP_COLLECT_JETTONS, OP_CONTRACT_CALL_LOG, OP_DEPLOY, OP_EXECUTE_OPERATOR_PROPOSAL, OP_EXECUTE_TIMELOCK_PROPOSAL, OP_GAS_INFO_UPDATED_LOG, OP_GATEWAY_EXECUTE, OP_INITIALIZE_SIGNERS, OP_JETTON_GAS_ADDED_LOG, OP_JETTON_GAS_COLLECTED_LOG, OP_JETTON_GAS_PAID_LOG, OP_JETTON_GAS_REFUNDED_LOG, OP_JETTON_REFUND, OP_MESSAGE_APPROVED, OP_MESSAGE_APPROVED_LOG, OP_MESSAGE_EXECUTED_LOG, OP_NATIVE_GAS_ADDED_LOG, OP_NATIVE_GAS_COLLECTED_LOG, OP_NATIVE_GAS_PAID_LOG, OP_NATIVE_GAS_REFUNDED_LOG, OP_NATIVE_REFUND, OP_NULLIFIED_SUCESSFULLY, OP_NULLIFY_IF_ALLOWED, OP_OPERATORSHIP_TRANSFERED_LOG, OP_OPERATOR_PROPOSAL_CANCELED_LOG, OP_OPERATOR_PROPOSAL_EXECUTED_LOG, OP_OPERATOR_PROPOSAL_SCHEDULED_LOG, OP_PAY_GAS, OP_PAY_JETTON_GAS_FOR_CONTRACT_CALL, OP_PAY_NATIVE_GAS_FOR_CONTRACT_CALL, OP_PROPOSAL_CANCELED_LOG, OP_PROPOSAL_EXECUTED_LOG, OP_PROPOSAL_SCHEDULED_LOG, OP_RELAYER_EXECUTE, OP_ROTATE_SIGNERS, OP_SCHEDULE_TIMELOCK_PROPOSAL, OP_SEND_MESSAGE, OP_START_SIGNER_ROTATION, OP_SUBTRACT, OP_TRANSFER_OWNERSHIP, OP_UNNULIFY_MESSAGE, OP_UPDATE_GAS_INFO, OP_VERIFY_PROOF, type OProposal, PAY_JETTON_GAS_FOR_CONTRACT_CALL_COST, RELAYER_EXECUTE_COST, ROTATE_SIGNERS_COST, SIGNER_CONFIG_HASH_LENGTH, START_SIGNER_ROTATION_COST, TIMELOCK_PROPOSAL_CANCELED, TIMELOCK_PROPOSAL_DISABLED, TIMELOCK_PROPOSAL_EXECUTED, TIMELOCK_PROPOSAL_SCHEDULED, type TProposal, type WeightedSigner, type WeightedSigners, base64ToUint8Array, bigIntToUtf8String, bigintToBuffer, bufferToCell, bufferToCellWith4Refs, buildAddJettonGasMessage, buildAddNativeGasMessage, buildApproveMessagesMessage, buildCollectGasMessage, buildCollectJettonsMessage, buildCompleteMessageCell, buildContractCallMessage, buildContractCallMessageChained, buildExecuteOperatorProposal, buildExecuteTimelockProposalPayload, buildInitializeSignersMessage, buildJettonRefundMessage, buildMessageIdCell, buildNativeRefundMessage, buildPayGasMessage, buildPayJettonGasForContractCallMessage, buildPayNativeGasForContractCallMessage, buildRelayerExecuteMessage, buildRotateSignersMessage, buildSendMessage, buildStartSignerRotationMessage, buildTransferOperatorshipMessage, buildUpdateGasInfoMessage, computePayloadHash, generateRandomBuffer, getDeterministicSigners, getEmptySignature, getSignatureBase64, getSignatureData, hexToUint8Array, isBase64String, isHexString, jettonContentToCell, jettonMinterConfigToCell, jettonWalletConfigToCell, keccak256Single, sendTransfer, serializeGasInfo, serializeGasInfos, serializeMessagesToBeApproved, serializeWeightedSigner, serializeWeightedSigners, uint8ArrayToBigInt, verifyProofWeightedSigners, verifySignature };