From de00573c09ed0b99e9703ffc0348e325d8671834 Mon Sep 17 00:00:00 2001 From: jackkru69 Date: Mon, 24 Jun 2024 12:18:22 +0300 Subject: [PATCH] fix: added seq to other set methods --- packages/cli/src/commands/contract/deploy.ts | 4 + packages/tssdk/src/libs/evm-api/evm-api.ts | 26 +- packages/tssdk/src/libs/evm-api/evmCore.ts | 51 ++-- packages/tssdk/src/libs/lstore.ts | 15 +- packages/tssdk/src/libs/transactions.ts | 250 ++++++++++++------- packages/tssdk/src/libs/wallet.ts | 18 +- 6 files changed, 236 insertions(+), 128 deletions(-) diff --git a/packages/cli/src/commands/contract/deploy.ts b/packages/cli/src/commands/contract/deploy.ts index 543fe01..57dc1f9 100644 --- a/packages/cli/src/commands/contract/deploy.ts +++ b/packages/cli/src/commands/contract/deploy.ts @@ -51,11 +51,15 @@ export default class ContractDeploy extends Command { // Format initialization parameters const formattedInitParams = initParams.map((param) => (AddressApi.isTextAddressValid(param) ? AddressApi.textAddressToEvmAddress(param) : param)); + const sequence = await networkApi.getWalletSequence(importedWallet.address); + const newSequence = sequence + 1; + // Compose the deployment transaction const deployTX = TransactionsApi.composeDeployTX({ abi, address: importedWallet.address, code, + seq: newSequence, feeSettings: networkApi.feeSettings, gasSettings: networkApi.gasSettings, gasToken, diff --git a/packages/tssdk/src/libs/evm-api/evm-api.ts b/packages/tssdk/src/libs/evm-api/evm-api.ts index 518ae67..0ea41f2 100644 --- a/packages/tssdk/src/libs/evm-api/evm-api.ts +++ b/packages/tssdk/src/libs/evm-api/evm-api.ts @@ -93,17 +93,23 @@ export class EvmApi { const data = hexToBytes(encodedFunction); const dataBuffer = Buffer.from(data); + const sequence = await this.network.getWalletSequence(this.scAddress); + const newSequence = sequence + 1; + const tx = await TransactionsApi.composeSCMethodCallTX( - key.address, - this.scAddress, - ['0x0', [dataBuffer]], - '', - 0, - key.wif, - 'SK', - amount, - this.network.feeSettings, - this.network.gasSettings, + { + address: key.address, + sc: this.scAddress, + toCall: ['0x0', [dataBuffer]], + gasToken: '', + gasValue: 0, + wif: key.wif, + amountToken: 'SK', + amountValue: amount, + seq: newSequence, + feeSettings: this.network.feeSettings, + gasSettings: this.network.gasSettings, + }, ); return this.network.sendTxAndWaitForResponse(tx); diff --git a/packages/tssdk/src/libs/evm-api/evmCore.ts b/packages/tssdk/src/libs/evm-api/evmCore.ts index 3ed0f19..dfa3fd8 100755 --- a/packages/tssdk/src/libs/evm-api/evmCore.ts +++ b/packages/tssdk/src/libs/evm-api/evmCore.ts @@ -63,31 +63,40 @@ export class EvmContract { const data = hexToBytes(encodedFunction); const dataBuffer = Buffer.from(data); + const sequence = await this.evm.network.getWalletSequence(key.address); + const newSequence = sequence + 1; + const tx = sponsor === '' ? await TransactionsApi.composeSCMethodCallTX( - key.address, - this.address, - ['0x0', [dataBuffer]], - '', - 0, - key.wif, - 'SK', - amount, - this.evm.network.feeSettings, - this.evm.network.gasSettings, + { + address: key.address, + sc: this.address, + toCall: ['0x0', [dataBuffer]], + gasToken: '', + gasValue: 0, + wif: key.wif, + amountToken: 'SK', + amountValue: amount, + seq: newSequence, + feeSettings: this.evm.network.feeSettings, + gasSettings: this.evm.network.gasSettings, + }, ) : await TransactionsApi.composeSponsorSCMethodCallTX( - key.address, - this.address, - ['0x0', [dataBuffer]], - '', - 0, - key.wif, - 'SK', - amount, - this.evm.network.feeSettings, - this.evm.network.gasSettings, - sponsor, + { + address: key.address, + sc: this.address, + toCall: ['0x0', [dataBuffer]], + gasToken: '', + gasValue: 0, + wif: key.wif, + amountToken: 'SK', + amountValue: amount, + seq: newSequence, + feeSettings: this.evm.network.feeSettings, + gasSettings: this.evm.network.gasSettings, + sponsor, + }, ); const res = await this.evm.network.sendTxAndWaitForResponse(tx); diff --git a/packages/tssdk/src/libs/lstore.ts b/packages/tssdk/src/libs/lstore.ts index e338a74..b6c7eea 100755 --- a/packages/tssdk/src/libs/lstore.ts +++ b/packages/tssdk/src/libs/lstore.ts @@ -69,15 +69,22 @@ const getLStore = async (address: string, path: string, key: string, network: Ne const setLStore = async (account: AccountKey, patches: [string, string, string][], network: NetworkApi) => { const addressChain = await network.getAddressChain(account.address); const addressChainNumber = addressChain?.chain; + if (network.getChain() !== addressChainNumber) { await network.changeChain(addressChainNumber); } + const sequence = await network.getWalletSequence(account.address); + const newSequence = sequence + 1; + const tx = await TransactionsApi.composeStoreTX( - account.address, - patches, - account.wif, - network.feeSettings, + { + address: account.address, + patches, + wif: account.wif, + seq: newSequence, + feeSettings: network.feeSettings, + }, ); return tx; }; diff --git a/packages/tssdk/src/libs/transactions.ts b/packages/tssdk/src/libs/transactions.ts index 8542d65..8581333 100644 --- a/packages/tssdk/src/libs/transactions.ts +++ b/packages/tssdk/src/libs/transactions.ts @@ -46,15 +46,23 @@ const getRegisterTxBody = async ({ return body; }; -const getSimpleTransferTxBody = ( - from: Buffer, - to: Buffer, - token: string, - amount: number, - msg: string, - timestamp: number, - seq: number, -) => { +const getSimpleTransferTxBody = ({ + from, + to, + token, + amount, + msg, + timestamp, + seq, +}: { + from: Buffer; + to: Buffer; + token: string; + amount: number; + msg: string; + timestamp: number; + seq: number; +}) => { const body = { k: KIND_GENERIC, t: timestamp, @@ -117,32 +125,46 @@ export const TransactionsApi = { return keyPair.verify(hash, ecsig); }, - composeSimpleTransferTX( - feeSettings: any, - wif: string, - from: string, - to: string, - token: string, - amount: number, - message: string, - seq: number, - fee?: number, - feeToken?: string, - ) { + composeSimpleTransferTX({ + feeSettings, + wif, + from, + to, + token, + amount, + message, + seq, + fee, + feeToken, + }: { + feeSettings: any; + wif: string; + from: string; + to: string; + token: string; + amount: number; + message: string; + seq: number; + fee?: number; + feeToken?: string; + }) { const keyPair = Bitcoin.ECPair.fromWIF(wif); const publicKey = keyPair.getPublicKeyBuffer(); const timestamp = +new Date(); const bufferFrom = Buffer.from(AddressApi.parseTextAddress(from)); const bufferTo = Buffer.from(AddressApi.parseTextAddress(to)); + let body = getSimpleTransferTxBody( - bufferFrom, - bufferTo, - token, - amount, - message, - timestamp, - seq, + { + from: bufferFrom, + to: bufferTo, + token, + amount, + msg: message, + timestamp, + seq, + }, ); if (feeToken && fee) { body.p.push([PURPOSE_SRCFEE, feeToken, fee]); @@ -183,6 +205,7 @@ export const TransactionsApi = { gasValue, wif, abi, + seq, feeSettings, gasSettings, fee, @@ -195,6 +218,7 @@ export const TransactionsApi = { gasValue: number; wif: string; abi: any; + seq: number; feeSettings: any; gasSettings: any; fee?: number; @@ -209,7 +233,7 @@ export const TransactionsApi = { t: +new Date(), f: Buffer.from(AddressApi.parseTextAddress(address)), to: Buffer.from(AddressApi.parseTextAddress(address)), - s: +new Date(), + s: seq, p: [] as any, c: [] as any, e: { code: Buffer.from(scCode), vm: 'evm', view: [] }, @@ -275,21 +299,36 @@ export const TransactionsApi = { return bsig.subarray(index + 2, index + 2 + bsig[index + 1]); }, - composeSCMethodCallTX( - address: string, - sc: string, - toCall: [string, [any]], - gasToken: string, - gasValue: number, - wif: string, - amountToken: string, - amountValue: number, - feeSettings: any, - gasSettings: any, - fee?: number, - feeToken?: string, - ) { - const body = this.composeSCMethodCallTxBody( + composeSCMethodCallTX({ + address, + sc, + toCall, + gasToken, + gasValue, + wif, + amountToken, + amountValue, + seq, + feeSettings, + gasSettings, + fee, + feeToken, + }: { + address: string; + sc: string; + toCall: [string, [any]]; + gasToken: string; + gasValue: number; + wif: string; + amountToken: string; + amountValue: number; + seq: number; + feeSettings: any; + gasSettings: any; + fee?: number; + feeToken?: string; + }) { + const body = this.composeSCMethodCallTxBody({ address, sc, toCall, @@ -297,27 +336,37 @@ export const TransactionsApi = { gasValue, amountToken, amountValue, + seq, feeSettings, gasSettings, fee, feeToken, - ); + }); return this.packAndSignTX(body, wif); }, - composeStoreTX( - address: string, - patches: any, - wif: string, - feeSettings: any, - fee?: number, - feeToken?: string, - ) { + composeStoreTX({ + address, + patches, + wif, + seq, + feeSettings, + fee, + feeToken, + }: { + address: string; + patches: any; + wif: string; + seq: number; + feeSettings: any; + fee?: number; + feeToken?: string; + }) { let body = { k: KIND_LSTORE, t: +new Date(), f: Buffer.from(AddressApi.parseTextAddress(address)), - s: +new Date(), + s: seq, p: [] as any, pa: msgPack.encode(patches.map((i: any) => msgPack.encode(i))), }; @@ -359,19 +408,33 @@ export const TransactionsApi = { return body; }, - composeSCMethodCallTxBody( - address: string, - sc: string, - toCall: [string, [any]], - gasToken: string, - gasValue: number, - amountToken: string, - amountValue: number, - feeSettings: any, - gasSettings: any, - fee?: number, - feeToken?: string, - ) { + composeSCMethodCallTxBody({ + address, + sc, + toCall, + gasToken, + gasValue, + amountToken, + amountValue, + seq, + feeSettings, + gasSettings, + fee, + feeToken, + }: { + address: string; + sc: string; + toCall: [string, [any]]; + gasToken: string; + gasValue: number; + amountToken: string; + amountValue: number; + seq: number; + feeSettings: any; + gasSettings: any; + fee?: number; + feeToken?: string; + }) { const PURPOSE: any[] = []; if (amountValue) { PURPOSE.push([PURPOSE_TRANSFER, amountToken, amountValue]); @@ -381,7 +444,7 @@ export const TransactionsApi = { t: +new Date(), f: Buffer.from(AddressApi.parseTextAddress(address)), to: Buffer.from(AddressApi.parseTextAddress(sc)), - s: +new Date(), + s: seq, p: PURPOSE, c: toCall, }; @@ -400,22 +463,38 @@ export const TransactionsApi = { }, // sponsor - composeSponsorSCMethodCallTX( - address: string, - sc: string, - toCall: [string, [any]], - gasToken: string, - gasValue: number, - wif: string, - amountToken: string, - amountValue: number, - feeSettings: any, - gasSettings: any, - sponsor: string, - fee?: number, - feeToken?: string, - ) { - const body = this.composeSCMethodCallTxBody( + composeSponsorSCMethodCallTX({ + address, + sc, + toCall, + gasToken, + gasValue, + wif, + amountToken, + amountValue, + seq, + feeSettings, + gasSettings, + sponsor, + fee, + feeToken, + }: { + address: string; + sc: string; + toCall: [string, [any]]; + gasToken: string; + gasValue: number; + wif: string; + amountToken: string; + amountValue: number; + seq: number; + feeSettings: any; + gasSettings: any; + sponsor: string; + fee?: number; + feeToken?: string; + }) { + const body = this.composeSCMethodCallTxBody({ address, sc, toCall, @@ -423,11 +502,12 @@ export const TransactionsApi = { gasValue, amountToken, amountValue, + seq, feeSettings, gasSettings, fee, feeToken, - ); + }); body.p.forEach((item) => { if (item[0] === PURPOSE_SRCFEE) { diff --git a/packages/tssdk/src/libs/wallet.ts b/packages/tssdk/src/libs/wallet.ts index ba4b3f9..ea58bd9 100644 --- a/packages/tssdk/src/libs/wallet.ts +++ b/packages/tssdk/src/libs/wallet.ts @@ -164,14 +164,16 @@ export class WalletApi { const sequence = await this.getWalletSequence(from); const newSequence = sequence + 1; const transmission = TransactionsApi.composeSimpleTransferTX( - feeSettings, - wif, - from, - to, - token, - amount, - message, - newSequence, + { + feeSettings, + wif, + from, + to, + token, + amount, + message, + seq: newSequence, + }, ); return this.networkApi.sendPreparedTX(transmission);