From 1290913c544768b649b145bfe436f4e53cb3063a Mon Sep 17 00:00:00 2001 From: Andy Haynes Date: Mon, 19 Aug 2024 17:26:52 -0700 Subject: [PATCH] --wip-- [skip ci] --- .../client/src/interfaces/dependencies.ts | 8 +- packages/client/src/transactions/actions.ts | 70 +++---- packages/client/src/transactions/composer.ts | 176 +++++++++--------- .../client/src/transactions/sign_and_send.ts | 82 ++++---- 4 files changed, 168 insertions(+), 168 deletions(-) diff --git a/packages/client/src/interfaces/dependencies.ts b/packages/client/src/interfaces/dependencies.ts index dd408dfbc..96e5f66ef 100644 --- a/packages/client/src/interfaces/dependencies.ts +++ b/packages/client/src/interfaces/dependencies.ts @@ -1,8 +1,8 @@ import type { - BlockReference, - BlockResult, - FinalExecutionOutcome, - QueryResponseKind, + BlockReference, + BlockResult, + FinalExecutionOutcome, + QueryResponseKind, } from '@near-js/types'; import type { SignedTransaction } from '@near-js/transactions'; import type { PublicKey } from '@near-js/crypto'; diff --git a/packages/client/src/transactions/actions.ts b/packages/client/src/transactions/actions.ts index 78804dd4d..fe01d32e9 100644 --- a/packages/client/src/transactions/actions.ts +++ b/packages/client/src/transactions/actions.ts @@ -4,52 +4,52 @@ import { signAndSendFromComposer } from './sign_and_send'; import { AddFunctionCallAccessKeyParams, SignAndSendNonceParams } from '../interfaces'; async function getComposerResult({ composer, nonce, blockReference, deps }: { composer: TransactionComposer } & SignAndSendNonceParams) { - const { result } = await signAndSendFromComposer({ - composer, - nonce, - blockReference, - deps, - }); + const { result } = await signAndSendFromComposer({ + composer, + nonce, + blockReference, + deps, + }); - return result; + return result; } export async function functionCall({ sender, receiver, method, args, gas, deposit, nonce, blockReference, deps }: FunctionCallParams) { - return getComposerResult({ - composer: TransactionComposer.init({ sender, receiver }) - .functionCall(method, args, gas, deposit), - nonce, - blockReference, - deps, - }); + return getComposerResult({ + composer: TransactionComposer.init({ sender, receiver }) + .functionCall(method, args, gas, deposit), + nonce, + blockReference, + deps, + }); } export async function transfer({ sender, receiver, amount, nonce, blockReference, deps }: TransferParams) { - return getComposerResult({ - composer: TransactionComposer.init({ sender, receiver }) - .transfer(amount), - nonce, - blockReference, - deps, - }); + return getComposerResult({ + composer: TransactionComposer.init({ sender, receiver }) + .transfer(amount), + nonce, + blockReference, + deps, + }); } export async function addFullAccessKey({ account, publicKey, nonce, blockReference, deps }: AddFullAccessKeyParams) { - return getComposerResult({ - composer: TransactionComposer.init({ sender: account, receiver: account }) - .addFullAccessKey(publicKey), - nonce, - blockReference, - deps, - }); + return getComposerResult({ + composer: TransactionComposer.init({ sender: account, receiver: account }) + .addFullAccessKey(publicKey), + nonce, + blockReference, + deps, + }); } export async function addFunctionCallAccessKey({ account, publicKey, contract, methodNames, allowance, nonce, blockReference, deps }: AddFunctionCallAccessKeyParams) { - return getComposerResult({ - composer: TransactionComposer.init({ sender: account, receiver: account }) - .addFunctionCallAccessKey(publicKey, contract, methodNames, allowance), - nonce, - blockReference, - deps, - }); + return getComposerResult({ + composer: TransactionComposer.init({ sender: account, receiver: account }) + .addFunctionCallAccessKey(publicKey, contract, methodNames, allowance), + nonce, + blockReference, + deps, + }); } diff --git a/packages/client/src/transactions/composer.ts b/packages/client/src/transactions/composer.ts index 2bd368821..d5e110fea 100644 --- a/packages/client/src/transactions/composer.ts +++ b/packages/client/src/transactions/composer.ts @@ -1,11 +1,11 @@ import { PublicKey } from '@near-js/crypto'; import { - Action, - actionCreators, - buildDelegateAction, - DelegateAction, - Signature, - Transaction, + Action, + actionCreators, + buildDelegateAction, + DelegateAction, + Signature, + Transaction, } from '@near-js/transactions'; import { baseDecode, DEFAULT_FUNCTION_CALL_GAS } from '@near-js/utils'; import { DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL } from '../constants'; @@ -20,102 +20,102 @@ interface TransactionOptions { } export class TransactionComposer { - private actions: Action[] = []; - receiver: string | undefined; - sender: string | undefined; - blockHeader: BlockHeader | undefined; - nonce: bigint | undefined; - publicKey: PublicKey | undefined; + private actions: Action[] = []; + receiver: string | undefined; + sender: string | undefined; + blockHeader: BlockHeader | undefined; + nonce: bigint | undefined; + publicKey: PublicKey | undefined; - static init(transaction: TransactionOptions) { - const composer = new TransactionComposer(); - composer.receiver = transaction.receiver; - composer.sender = transaction.sender; - composer.blockHeader = transaction.blockHeader; - composer.nonce = transaction.nonce; - composer.publicKey = transaction.publicKey; + static init(transaction: TransactionOptions) { + const composer = new TransactionComposer(); + composer.receiver = transaction.receiver; + composer.sender = transaction.sender; + composer.blockHeader = transaction.blockHeader; + composer.nonce = transaction.nonce; + composer.publicKey = transaction.publicKey; - return composer; - } + return composer; + } - private buildTransactionObject(transaction?: TransactionOptions) { - const hash = (transaction?.blockHeader?.hash || this.blockHeader?.hash)!; - return { - actions: this.actions, - blockHash: baseDecode(hash), - nonce: (transaction?.nonce || this.nonce)!, - publicKey: (transaction?.publicKey || this.publicKey)!, - receiverId: (transaction?.receiver || this.receiver)!, - signerId: (transaction?.sender || this.sender)!, - }; - } + private buildTransactionObject(transaction?: TransactionOptions) { + const hash = (transaction?.blockHeader?.hash || this.blockHeader?.hash)!; + return { + actions: this.actions, + blockHash: baseDecode(hash), + nonce: (transaction?.nonce || this.nonce)!, + publicKey: (transaction?.publicKey || this.publicKey)!, + receiverId: (transaction?.receiver || this.receiver)!, + signerId: (transaction?.sender || this.sender)!, + }; + } - toTransaction(transaction?: TransactionOptions): Transaction { - return new Transaction(this.buildTransactionObject(transaction)); - } + toTransaction(transaction?: TransactionOptions): Transaction { + return new Transaction(this.buildTransactionObject(transaction)); + } - toDelegateAction(transaction?: TransactionOptions): DelegateAction { - const { actions, nonce, publicKey, receiverId, signerId } = this.buildTransactionObject(transaction); - const blockHeader = transaction?.blockHeader || this.blockHeader; + toDelegateAction(transaction?: TransactionOptions): DelegateAction { + const { actions, nonce, publicKey, receiverId, signerId } = this.buildTransactionObject(transaction); + const blockHeader = transaction?.blockHeader || this.blockHeader; - return buildDelegateAction({ - actions, - maxBlockHeight: BigInt(blockHeader!.height) + DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL, - nonce: BigInt(nonce) + BigInt(1), - publicKey, - receiverId, - senderId: signerId, - }); - } + return buildDelegateAction({ + actions, + maxBlockHeight: BigInt(blockHeader!.height) + DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL, + nonce: BigInt(nonce) + BigInt(1), + publicKey, + receiverId, + senderId: signerId, + }); + } - addFullAccessKey(publicKey: PublicKey) { - this.actions.push(actionCreators.addKey(publicKey, actionCreators.fullAccessKey())); - return this; - } + addFullAccessKey(publicKey: PublicKey) { + this.actions.push(actionCreators.addKey(publicKey, actionCreators.fullAccessKey())); + return this; + } - addFunctionCallAccessKey(publicKey: PublicKey, contractId: string, methodNames: string[], allowance?: bigint) { - const accessKey = actionCreators.functionCallAccessKey(contractId, methodNames, allowance) - this.actions.push(actionCreators.addKey(publicKey, accessKey)); - return this; - } + addFunctionCallAccessKey(publicKey: PublicKey, contractId: string, methodNames: string[], allowance?: bigint) { + const accessKey = actionCreators.functionCallAccessKey(contractId, methodNames, allowance); + this.actions.push(actionCreators.addKey(publicKey, accessKey)); + return this; + } - createAccount() { - this.actions.push(actionCreators.createAccount()); - return this; - } + createAccount() { + this.actions.push(actionCreators.createAccount()); + return this; + } - deleteAccount(beneficiaryId: string) { - this.actions.push(actionCreators.deleteAccount(beneficiaryId)); - return this; - } + deleteAccount(beneficiaryId: string) { + this.actions.push(actionCreators.deleteAccount(beneficiaryId)); + return this; + } - deleteKey(publicKey: PublicKey) { - this.actions.push(actionCreators.deleteKey(publicKey)); - return this; - } + deleteKey(publicKey: PublicKey) { + this.actions.push(actionCreators.deleteKey(publicKey)); + return this; + } - deployContract(code: Uint8Array) { - this.actions.push(actionCreators.deployContract(code)); - return this; - } + deployContract(code: Uint8Array) { + this.actions.push(actionCreators.deployContract(code)); + return this; + } - functionCall(method: string, args: object, gas: bigint = DEFAULT_FUNCTION_CALL_GAS * BigInt(10), deposit = BigInt(0)) { - this.actions.push(actionCreators.functionCall(method, args, gas, deposit)); - return this; - } + functionCall(method: string, args: object, gas: bigint = DEFAULT_FUNCTION_CALL_GAS * BigInt(10), deposit = BigInt(0)) { + this.actions.push(actionCreators.functionCall(method, args, gas, deposit)); + return this; + } - signedDelegate(delegateAction: DelegateAction, signature: Signature) { - this.actions.push(actionCreators.signedDelegate({ delegateAction, signature })); - return this; - } + signedDelegate(delegateAction: DelegateAction, signature: Signature) { + this.actions.push(actionCreators.signedDelegate({ delegateAction, signature })); + return this; + } - stake(stake: bigint, publicKey: PublicKey) { - this.actions.push(actionCreators.stake(stake, publicKey)); - return this; - } + stake(stake: bigint, publicKey: PublicKey) { + this.actions.push(actionCreators.stake(stake, publicKey)); + return this; + } - transfer(deposit: bigint) { - this.actions.push(actionCreators.transfer(deposit)); - return this; - } + transfer(deposit: bigint) { + this.actions.push(actionCreators.transfer(deposit)); + return this; + } } diff --git a/packages/client/src/transactions/sign_and_send.ts b/packages/client/src/transactions/sign_and_send.ts index cfc7f9f05..818b74a29 100644 --- a/packages/client/src/transactions/sign_and_send.ts +++ b/packages/client/src/transactions/sign_and_send.ts @@ -8,55 +8,55 @@ import { getNonce } from '../view'; import { SignAndSendComposerParams } from '../interfaces'; export async function signTransaction({ transaction, deps: { signer } }: SignTransactionParams) { - const encodedTx = transaction.encode(); - const signedTransaction = new SignedTransaction({ - transaction, - signature: new Signature({ - keyType: transaction.publicKey.keyType, - data: await signer.signMessage(encodedTx), - }), - }); - - return { - encodedTransactionHash: new Uint8Array(sha256(encodedTx)), - signedTransaction, - }; + const encodedTx = transaction.encode(); + const signedTransaction = new SignedTransaction({ + transaction, + signature: new Signature({ + keyType: transaction.publicKey.keyType, + data: await signer.signMessage(encodedTx), + }), + }); + + return { + encodedTransactionHash: new Uint8Array(sha256(encodedTx)), + signedTransaction, + }; } export async function signAndSendTransaction({ transaction, deps: { rpcProvider, signer } }: SignAndSendTransactionParams) { - const { signedTransaction } = await signTransaction({ transaction, deps: { signer } }); - const outcome = await rpcProvider.sendTransaction(signedTransaction); - return { - outcome, - result: getTransactionLastResult(outcome), - }; + const { signedTransaction } = await signTransaction({ transaction, deps: { signer } }); + const outcome = await rpcProvider.sendTransaction(signedTransaction); + return { + outcome, + result: getTransactionLastResult(outcome), + }; } export async function getSignerNonce({ account, blockReference, deps: { rpcProvider, signer } }) { - return getNonce({ - account, - publicKey: (await signer.getPublicKey()).toString(), - blockReference, - deps: { rpcProvider }, - }); + return getNonce({ + account, + publicKey: (await signer.getPublicKey()).toString(), + blockReference, + deps: { rpcProvider }, + }); } // this might be more natural as a method on TransactionComposer but would be a major increase in scope export async function signAndSendFromComposer({ composer, nonce, blockReference, deps }: SignAndSendComposerParams) { - const { rpcProvider, signer } = deps; - const block = await getBlock({ blockReference, deps: { rpcProvider } }); - - let signerNonce = nonce; - if (!signerNonce) { - signerNonce = await getSignerNonce({ account: composer.sender, blockReference, deps }); - signerNonce += 1n; - } - - const transaction = composer.toTransaction({ - nonce: signerNonce, - publicKey: await signer.getPublicKey(), - blockHeader: block?.header, - }); - - return signAndSendTransaction({ transaction, deps }); + const { rpcProvider, signer } = deps; + const block = await getBlock({ blockReference, deps: { rpcProvider } }); + + let signerNonce = nonce; + if (!signerNonce) { + signerNonce = await getSignerNonce({ account: composer.sender, blockReference, deps }); + signerNonce += 1n; + } + + const transaction = composer.toTransaction({ + nonce: signerNonce, + publicKey: await signer.getPublicKey(), + blockHeader: block?.header, + }); + + return signAndSendTransaction({ transaction, deps }); }