Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 2x 2x 1x 8x 8x 8x 7x 1x 8x 1x 7x 8x 8x 1x 6x 8x 8x 8x 8x 8x 9x 8x 7x 7x 7x 7x 6x 6x 6x 1x 1x 1x 1x 1x 3x 6x 3x 1x 1x | import { StacksTransaction } from '../transaction'; import { StacksMainnet } from 'micro-stacks/network'; import { AddressHashMode, PostConditionMode } from '../common/constants'; import { createContractCallPayload } from '../payload'; import { ClarityAbi } from 'micro-stacks/clarity'; import { validateContractCall } from '../contract-abi'; import { createMultiSigSpendingCondition, createSingleSigSpendingCondition, createSponsoredAuth, createStandardAuth, } from '../authorization'; import { PostCondition } from '../postcondition'; import { createLPList } from '../types'; import { bytesToHex, hexToBytes, omit, TransactionVersion } from 'micro-stacks/common'; import { c32address, StacksNetworkVersion } from 'micro-stacks/crypto'; import { createStacksPrivateKey, getPublicKeyFromStacksPrivateKey, pubKeyfromPrivKey, publicKeyFromBuffer, publicKeyToString, } from '../keys'; import { TransactionSigner } from '../signer'; import { SignedContractCallOptions, SignedMultiSigContractCallOptions, UnsignedContractCallOptions, UnsignedMultiSigContractCallOptions, } from './types'; import { getNonce } from '../fetchers/get-nonce'; import { getAbi } from '../fetchers/get-abi'; import { estimateContractFunctionCall } from '../fetchers/estimate-contract-function-call'; /** * Generates an unsigned Clarity smart contract function call transaction * * @param {UnsignedContractCallOptions | UnsignedMultiSigContractCallOptions} txOptions - an options object for the contract call * * @returns {Promise<StacksTransaction>} */ export async function makeUnsignedContractCall( txOptions: UnsignedContractCallOptions | UnsignedMultiSigContractCallOptions ): Promise<StacksTransaction> { const defaultOptions = { fee: BigInt(0), nonce: BigInt(0), network: new StacksMainnet(), postConditionMode: PostConditionMode.Deny, sponsored: false, }; const options = Object.assign(defaultOptions, txOptions); const payload = createContractCallPayload( options.contractAddress, options.contractName, options.functionName, options.functionArgs ); if (options?.validateWithAbi) { let abi: ClarityAbi; if (typeof options.validateWithAbi === 'boolean') { if (options?.network) { abi = await getAbi(options.contractAddress, options.contractName, options.network); } else E{ throw new Error('Network option must be provided in order to validate with ABI'); } } else { abi = options.validateWithAbi; } validateContractCall(payload, abi); } let spendingCondition = null; let authorization = null; if ('publicKey' in options) { // single-sig spendingCondition = createSingleSigSpendingCondition( AddressHashMode.SerializeP2PKH, options.publicKey, options.nonce, options.fee ); } else { // multi-sig spendingCondition = createMultiSigSpendingCondition( AddressHashMode.SerializeP2SH, options.numSignatures, options.publicKeys, options.nonce, options.fee ); } if (options.sponsored) { authorization = createSponsoredAuth(spendingCondition); } else { authorization = createStandardAuth(spendingCondition); } const postConditions: PostCondition[] = []; if (options.postConditions && options.postConditions.length > 0) { options.postConditions.forEach(postCondition => { postConditions.push(postCondition); }); } const lpPostConditions = createLPList(postConditions); const transaction = new StacksTransaction( options.network.version, authorization, payload, lpPostConditions, options.postConditionMode, options.anchorMode, options.network.chainId ); Iif (txOptions.fee === undefined || txOptions.fee === null) { const txFee = await estimateContractFunctionCall(transaction, options.network); transaction.setFee(txFee); } Iif (txOptions.nonce === undefined || txOptions.nonce === null) { const addressVersion = options.network.version === TransactionVersion.Mainnet ? StacksNetworkVersion.mainnetP2PKH : StacksNetworkVersion.testnetP2PKH; const senderAddress = c32address( addressVersion, hexToBytes(transaction.auth.spendingCondition!.signer) ); const txNonce = await getNonce(senderAddress, options.network); transaction.setNonce(txNonce); } return transaction; } /** * Generates a Clarity smart contract function call transaction * * @param {SignedContractCallOptions | SignedMultiSigContractCallOptions} txOptions - an options object for the contract function call * * Returns a signed Stacks smart contract function call transaction. * * @return {StacksTransaction} */ export async function makeContractCall( txOptions: SignedContractCallOptions | SignedMultiSigContractCallOptions ): Promise<StacksTransaction> { if ('senderKey' in txOptions) { const privKey = createStacksPrivateKey(txOptions.senderKey); const publicKey = publicKeyToString(getPublicKeyFromStacksPrivateKey(privKey)); const options = omit(txOptions, 'senderKey'); const transaction = await makeUnsignedContractCall({ publicKey, ...options }); const signer = new TransactionSigner(transaction); await signer.signOrigin(privKey); return transaction; } else { const options = omit(txOptions, 'signerKeys'); const transaction = await makeUnsignedContractCall(options); const signer = new TransactionSigner(transaction); let pubKeys = txOptions.publicKeys; for (const key of txOptions.signerKeys) { const pubKey = pubKeyfromPrivKey(key); pubKeys = pubKeys.filter(pk => pk !== bytesToHex(pubKey.data)); await signer.signOrigin(createStacksPrivateKey(key)); } for (const key of pubKeys) { signer.appendOrigin(publicKeyFromBuffer(hexToBytes(key))); } return transaction; } } |