From dfb32c2887ba16f7685119c728d9f1b8c0f32b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolfo=20Pietro=20Calabr=C3=B2?= <33911400+rodolfopietro97@users.noreply.github.com> Date: Tue, 7 May 2024 10:39:28 +0200 Subject: [PATCH] 821 feat implement signer into contracts module (#849) * fix: remove generics from signer * feat: using only 1 method to sign transaction in order to reduce complexity * fix: missing exmaples * feat: replace private key with signer * fix: docs examples * fix: simplify --- docs/contracts-deposit.md | 4 +- docs/contracts-erc20.md | 13 +- .../contracts/contract-create-ERC20-token.ts | 35 +++-- .../contracts/contract-delegation-ERC20.ts | 33 ++++- docs/examples/contracts/contract-deposit.ts | 33 +++-- .../contracts/contract-event-filter.ts | 32 +++-- .../contract-transfer-ERC20-token.ts | 27 +++- docs/examples/thor-client/contract.ts | 29 ++++- .../full-flow-delegator-private-key.ts | 2 +- .../transactions/full-flow-delegator-url.ts | 2 +- docs/transactions.md | 4 +- .../provider-internal-base-wallet.ts | 6 +- .../provider-internal-wallets/types.d.ts | 9 +- .../vechain-provider/vechain-provider.ts | 2 +- .../eth_sendTransaction.ts | 2 +- .../network/src/signer/signers/types.d.ts | 24 +--- .../vechain-base-signer.ts | 70 +++------- .../thor-client/contracts/contracts-module.ts | 122 +++++------------- .../contracts/model/contract-factory.ts | 30 ++--- .../contracts/model/contract-proxy.ts | 17 +-- .../thor-client/contracts/model/contract.ts | 27 ++-- .../hardhat/hardhat-provider.solo.test.ts | 31 +++-- .../tests/provider/providers/helpers.ts | 20 ++- .../vechain/vechain-provider.solo.test.ts | 35 +++-- .../signers/vechain-base-signer/fixture.ts | 29 +++++ .../vechain-base-signer.solo.test.ts | 21 +-- .../vechain-base-signer.testnet.test.ts | 26 ++-- .../contracts/contract.erc20.solo.test.ts | 33 +++-- .../contracts/contract.erc20.testnet.test.ts | 24 +++- .../contracts/contract.erc721.solo.test.ts | 25 +++- .../contracts/contract.solo.test.ts | 74 +++++++---- .../subscriptions/subscriptions.solo.test.ts | 4 +- 32 files changed, 475 insertions(+), 370 deletions(-) diff --git a/docs/contracts-deposit.md b/docs/contracts-deposit.md index a8e244ed3..231b1f5c4 100644 --- a/docs/contracts-deposit.md +++ b/docs/contracts-deposit.md @@ -19,7 +19,7 @@ The main deployment steps are as follows: const contractFactory = thorSoloClient.contracts.createContractFactory( depositContractAbi, depositContractBytecode, - privateKeyDeployer + signer ); const contract = await ( @@ -28,7 +28,7 @@ const contract = await ( await (await contract.transact.deposit({ value: 1000 })).wait(); -const balance = await contract.read.getBalance(privateKeyAddress); +const balance = await contract.read.getBalance(deployerAccount.address); expect(balance).toEqual([BigInt(1000)]); ``` diff --git a/docs/contracts-erc20.md b/docs/contracts-erc20.md index b297539d0..8a83a2fa7 100644 --- a/docs/contracts-erc20.md +++ b/docs/contracts-erc20.md @@ -34,12 +34,19 @@ Once the contract is compiled, we can deploy it using the vechain SDK. The follo // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount]) +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; // Creating the contract factory const contractFactory = thorSoloClient.contracts.createContractFactory( VIP180_ABI, erc20ContractBytecode, - privateKeyDeployer + signer ); // Deploying the contract @@ -54,9 +61,7 @@ const receipt = contract.deployTransactionReceipt; // Asserting that the contract deployment didn't revert, indicating a successful deployment expect(receipt.reverted).toEqual(false); -const balance = await contract.read.balanceOf( - addressUtils.fromPrivateKey(Buffer.from(privateKeyDeployer, 'hex')) -); +const balance = await contract.read.balanceOf(deployerAccount.address); // Asserting that the initial balance of the deployer is the expected amount (1e24) expect(balance).toEqual([unitsUtils.parseUnits('1', 24)]); diff --git a/docs/examples/contracts/contract-create-ERC20-token.ts b/docs/examples/contracts/contract-create-ERC20-token.ts index 6e85a3a16..3b0ac16f2 100644 --- a/docs/examples/contracts/contract-create-ERC20-token.ts +++ b/docs/examples/contracts/contract-create-ERC20-token.ts @@ -1,25 +1,44 @@ +import { + ProviderInternalBaseWallet, + type ProviderInternalWalletAccount, + ThorClient, + VechainProvider, + type VechainSigner +} from '@vechain/sdk-network'; +import { unitsUtils, VIP180_ABI } from '@vechain/sdk-core'; import { expect } from 'expect'; -import { VIP180_ABI, addressUtils, unitsUtils } from '@vechain/sdk-core'; -import { ThorClient } from '@vechain/sdk-network'; +// ERC20 contract bytecode const erc20ContractBytecode: string = '0x60806040523480156200001157600080fd5b506040518060400160405280600b81526020017f53616d706c65546f6b656e0000000000000000000000000000000000000000008152506040518060400160405280600281526020017f535400000000000000000000000000000000000000000000000000000000000081525081600390816200008f91906200062c565b508060049081620000a191906200062c565b505050620000e633620000b9620000ec60201b60201c565b60ff16600a620000ca919062000896565b620f4240620000da9190620008e7565b620000f560201b60201c565b62000a3a565b60006012905090565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036200016a5760006040517fec442f0500000000000000000000000000000000000000000000000000000000815260040162000161919062000977565b60405180910390fd5b6200017e600083836200018260201b60201c565b5050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001d8578060026000828254620001cb919062000994565b92505081905550620002ae565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490508181101562000267578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016200025e93929190620009e0565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002f9578060026000828254039250508190555062000346565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051620003a5919062000a1d565b60405180910390a3505050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200043457607f821691505b6020821081036200044a5762000449620003ec565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620004b47fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000475565b620004c0868362000475565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006200050d620005076200050184620004d8565b620004e2565b620004d8565b9050919050565b6000819050919050565b6200052983620004ec565b62000541620005388262000514565b84845462000482565b825550505050565b600090565b6200055862000549565b620005658184846200051e565b505050565b5b818110156200058d57620005816000826200054e565b6001810190506200056b565b5050565b601f821115620005dc57620005a68162000450565b620005b18462000465565b81016020851015620005c1578190505b620005d9620005d08562000465565b8301826200056a565b50505b505050565b600082821c905092915050565b60006200060160001984600802620005e1565b1980831691505092915050565b60006200061c8383620005ee565b9150826002028217905092915050565b6200063782620003b2565b67ffffffffffffffff811115620006535762000652620003bd565b5b6200065f82546200041b565b6200066c82828562000591565b600060209050601f831160018114620006a457600084156200068f578287015190505b6200069b85826200060e565b8655506200070b565b601f198416620006b48662000450565b60005b82811015620006de57848901518255600182019150602085019450602081019050620006b7565b86831015620006fe5784890151620006fa601f891682620005ee565b8355505b6001600288020188555050505b505050505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008160011c9050919050565b6000808291508390505b6001851115620007a15780860481111562000779576200077862000713565b5b6001851615620007895780820291505b8081029050620007998562000742565b945062000759565b94509492505050565b600082620007bc57600190506200088f565b81620007cc57600090506200088f565b8160018114620007e55760028114620007f05762000826565b60019150506200088f565b60ff84111562000805576200080462000713565b5b8360020a9150848211156200081f576200081e62000713565b5b506200088f565b5060208310610133831016604e8410600b8410161715620008605782820a9050838111156200085a576200085962000713565b5b6200088f565b6200086f84848460016200074f565b9250905081840481111562000889576200088862000713565b5b81810290505b9392505050565b6000620008a382620004d8565b9150620008b083620004d8565b9250620008df7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8484620007aa565b905092915050565b6000620008f482620004d8565b91506200090183620004d8565b92508282026200091181620004d8565b915082820484148315176200092b576200092a62000713565b5b5092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200095f8262000932565b9050919050565b620009718162000952565b82525050565b60006020820190506200098e600083018462000966565b92915050565b6000620009a182620004d8565b9150620009ae83620004d8565b9250828201905080821115620009c957620009c862000713565b5b92915050565b620009da81620004d8565b82525050565b6000606082019050620009f7600083018662000966565b62000a066020830185620009cf565b62000a156040830184620009cf565b949350505050565b600060208201905062000a346000830184620009cf565b92915050565b610e558062000a4a6000396000f3fe608060405234801561001057600080fd5b50600436106100935760003560e01c8063313ce56711610066578063313ce5671461013457806370a082311461015257806395d89b4114610182578063a9059cbb146101a0578063dd62ed3e146101d057610093565b806306fdde0314610098578063095ea7b3146100b657806318160ddd146100e657806323b872dd14610104575b600080fd5b6100a0610200565b6040516100ad9190610aa9565b60405180910390f35b6100d060048036038101906100cb9190610b64565b610292565b6040516100dd9190610bbf565b60405180910390f35b6100ee6102b5565b6040516100fb9190610be9565b60405180910390f35b61011e60048036038101906101199190610c04565b6102bf565b60405161012b9190610bbf565b60405180910390f35b61013c6102ee565b6040516101499190610c73565b60405180910390f35b61016c60048036038101906101679190610c8e565b6102f7565b6040516101799190610be9565b60405180910390f35b61018a61033f565b6040516101979190610aa9565b60405180910390f35b6101ba60048036038101906101b59190610b64565b6103d1565b6040516101c79190610bbf565b60405180910390f35b6101ea60048036038101906101e59190610cbb565b6103f4565b6040516101f79190610be9565b60405180910390f35b60606003805461020f90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461023b90610d2a565b80156102885780601f1061025d57610100808354040283529160200191610288565b820191906000526020600020905b81548152906001019060200180831161026b57829003601f168201915b5050505050905090565b60008061029d61047b565b90506102aa818585610483565b600191505092915050565b6000600254905090565b6000806102ca61047b565b90506102d7858285610495565b6102e2858585610529565b60019150509392505050565b60006012905090565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60606004805461034e90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461037a90610d2a565b80156103c75780601f1061039c576101008083540402835291602001916103c7565b820191906000526020600020905b8154815290600101906020018083116103aa57829003601f168201915b5050505050905090565b6000806103dc61047b565b90506103e9818585610529565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b610490838383600161061d565b505050565b60006104a184846103f4565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146105235781811015610513578281836040517ffb8f41b200000000000000000000000000000000000000000000000000000000815260040161050a93929190610d6a565b60405180910390fd5b6105228484848403600061061d565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361059b5760006040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105929190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361060d5760006040517fec442f050000000000000000000000000000000000000000000000000000000081526004016106049190610da1565b60405180910390fd5b6106188383836107f4565b505050565b600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff160361068f5760006040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106869190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036107015760006040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106f89190610da1565b60405180910390fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555080156107ee578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107e59190610be9565b60405180910390a35b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361084657806002600082825461083a9190610deb565b92505081905550610919565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050818110156108d2578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108c993929190610d6a565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361096257806002600082825403925050819055506109af565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051610a0c9190610be9565b60405180910390a3505050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610a53578082015181840152602081019050610a38565b60008484015250505050565b6000601f19601f8301169050919050565b6000610a7b82610a19565b610a858185610a24565b9350610a95818560208601610a35565b610a9e81610a5f565b840191505092915050565b60006020820190508181036000830152610ac38184610a70565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610afb82610ad0565b9050919050565b610b0b81610af0565b8114610b1657600080fd5b50565b600081359050610b2881610b02565b92915050565b6000819050919050565b610b4181610b2e565b8114610b4c57600080fd5b50565b600081359050610b5e81610b38565b92915050565b60008060408385031215610b7b57610b7a610acb565b5b6000610b8985828601610b19565b9250506020610b9a85828601610b4f565b9150509250929050565b60008115159050919050565b610bb981610ba4565b82525050565b6000602082019050610bd46000830184610bb0565b92915050565b610be381610b2e565b82525050565b6000602082019050610bfe6000830184610bda565b92915050565b600080600060608486031215610c1d57610c1c610acb565b5b6000610c2b86828701610b19565b9350506020610c3c86828701610b19565b9250506040610c4d86828701610b4f565b9150509250925092565b600060ff82169050919050565b610c6d81610c57565b82525050565b6000602082019050610c886000830184610c64565b92915050565b600060208284031215610ca457610ca3610acb565b5b6000610cb284828501610b19565b91505092915050565b60008060408385031215610cd257610cd1610acb565b5b6000610ce085828601610b19565b9250506020610cf185828601610b19565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610d4257607f821691505b602082108103610d5557610d54610cfb565b5b50919050565b610d6481610af0565b82525050565b6000606082019050610d7f6000830186610d5b565b610d8c6020830185610bda565b610d996040830184610bda565b949350505050565b6000602082019050610db66000830184610d5b565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610df682610b2e565b9150610e0183610b2e565b9250828201905080821115610e1957610e18610dbc565b5b9291505056fea2646970667358221220912f5265edaea44910db734f0d00fccd257c78dba79c126931551eaad1a334f764736f6c63430008170033'; -// Defining the private key for the deployer account, which has VTHO for deployment costs -const privateKeyDeployer = - '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff'; +// Defining the deployer account, which has VTHO for deployment costs +const deployerAccount: ProviderInternalWalletAccount = { + privateKey: Buffer.from( + '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff', + 'hex' + ), + address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' +}; // START_SNIPPET: CreateERC20TokenSnippet // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount]) +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; // Creating the contract factory const contractFactory = thorSoloClient.contracts.createContractFactory( VIP180_ABI, erc20ContractBytecode, - privateKeyDeployer + signer ); // Deploying the contract @@ -34,9 +53,7 @@ const receipt = contract.deployTransactionReceipt; // Asserting that the contract deployment didn't revert, indicating a successful deployment expect(receipt.reverted).toEqual(false); -const balance = await contract.read.balanceOf( - addressUtils.fromPrivateKey(Buffer.from(privateKeyDeployer, 'hex')) -); +const balance = await contract.read.balanceOf(deployerAccount.address); // Asserting that the initial balance of the deployer is the expected amount (1e24) expect(balance).toEqual([unitsUtils.parseUnits('1', 24)]); diff --git a/docs/examples/contracts/contract-delegation-ERC20.ts b/docs/examples/contracts/contract-delegation-ERC20.ts index f799aa49e..2184c64a5 100644 --- a/docs/examples/contracts/contract-delegation-ERC20.ts +++ b/docs/examples/contracts/contract-delegation-ERC20.ts @@ -1,18 +1,29 @@ import { VIP180_ABI } from '@vechain/sdk-core'; import { type Contract, + ProviderInternalBaseWallet, + type ProviderInternalWalletAccount, ThorClient, - type TransactionReceipt + type TransactionReceipt, + VechainProvider, + type VechainSigner } from '@vechain/sdk-network'; import { expect } from 'expect'; +// ERC20 contract bytecode const erc20ContractBytecode: string = '0x60806040523480156200001157600080fd5b506040518060400160405280600b81526020017f53616d706c65546f6b656e0000000000000000000000000000000000000000008152506040518060400160405280600281526020017f535400000000000000000000000000000000000000000000000000000000000081525081600390816200008f91906200062c565b508060049081620000a191906200062c565b505050620000e633620000b9620000ec60201b60201c565b60ff16600a620000ca919062000896565b620f4240620000da9190620008e7565b620000f560201b60201c565b62000a3a565b60006012905090565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036200016a5760006040517fec442f0500000000000000000000000000000000000000000000000000000000815260040162000161919062000977565b60405180910390fd5b6200017e600083836200018260201b60201c565b5050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001d8578060026000828254620001cb919062000994565b92505081905550620002ae565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490508181101562000267578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016200025e93929190620009e0565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002f9578060026000828254039250508190555062000346565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051620003a5919062000a1d565b60405180910390a3505050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200043457607f821691505b6020821081036200044a5762000449620003ec565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620004b47fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000475565b620004c0868362000475565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006200050d620005076200050184620004d8565b620004e2565b620004d8565b9050919050565b6000819050919050565b6200052983620004ec565b62000541620005388262000514565b84845462000482565b825550505050565b600090565b6200055862000549565b620005658184846200051e565b505050565b5b818110156200058d57620005816000826200054e565b6001810190506200056b565b5050565b601f821115620005dc57620005a68162000450565b620005b18462000465565b81016020851015620005c1578190505b620005d9620005d08562000465565b8301826200056a565b50505b505050565b600082821c905092915050565b60006200060160001984600802620005e1565b1980831691505092915050565b60006200061c8383620005ee565b9150826002028217905092915050565b6200063782620003b2565b67ffffffffffffffff811115620006535762000652620003bd565b5b6200065f82546200041b565b6200066c82828562000591565b600060209050601f831160018114620006a457600084156200068f578287015190505b6200069b85826200060e565b8655506200070b565b601f198416620006b48662000450565b60005b82811015620006de57848901518255600182019150602085019450602081019050620006b7565b86831015620006fe5784890151620006fa601f891682620005ee565b8355505b6001600288020188555050505b505050505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008160011c9050919050565b6000808291508390505b6001851115620007a15780860481111562000779576200077862000713565b5b6001851615620007895780820291505b8081029050620007998562000742565b945062000759565b94509492505050565b600082620007bc57600190506200088f565b81620007cc57600090506200088f565b8160018114620007e55760028114620007f05762000826565b60019150506200088f565b60ff84111562000805576200080462000713565b5b8360020a9150848211156200081f576200081e62000713565b5b506200088f565b5060208310610133831016604e8410600b8410161715620008605782820a9050838111156200085a576200085962000713565b5b6200088f565b6200086f84848460016200074f565b9250905081840481111562000889576200088862000713565b5b81810290505b9392505050565b6000620008a382620004d8565b9150620008b083620004d8565b9250620008df7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8484620007aa565b905092915050565b6000620008f482620004d8565b91506200090183620004d8565b92508282026200091181620004d8565b915082820484148315176200092b576200092a62000713565b5b5092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200095f8262000932565b9050919050565b620009718162000952565b82525050565b60006020820190506200098e600083018462000966565b92915050565b6000620009a182620004d8565b9150620009ae83620004d8565b9250828201905080821115620009c957620009c862000713565b5b92915050565b620009da81620004d8565b82525050565b6000606082019050620009f7600083018662000966565b62000a066020830185620009cf565b62000a156040830184620009cf565b949350505050565b600060208201905062000a346000830184620009cf565b92915050565b610e558062000a4a6000396000f3fe608060405234801561001057600080fd5b50600436106100935760003560e01c8063313ce56711610066578063313ce5671461013457806370a082311461015257806395d89b4114610182578063a9059cbb146101a0578063dd62ed3e146101d057610093565b806306fdde0314610098578063095ea7b3146100b657806318160ddd146100e657806323b872dd14610104575b600080fd5b6100a0610200565b6040516100ad9190610aa9565b60405180910390f35b6100d060048036038101906100cb9190610b64565b610292565b6040516100dd9190610bbf565b60405180910390f35b6100ee6102b5565b6040516100fb9190610be9565b60405180910390f35b61011e60048036038101906101199190610c04565b6102bf565b60405161012b9190610bbf565b60405180910390f35b61013c6102ee565b6040516101499190610c73565b60405180910390f35b61016c60048036038101906101679190610c8e565b6102f7565b6040516101799190610be9565b60405180910390f35b61018a61033f565b6040516101979190610aa9565b60405180910390f35b6101ba60048036038101906101b59190610b64565b6103d1565b6040516101c79190610bbf565b60405180910390f35b6101ea60048036038101906101e59190610cbb565b6103f4565b6040516101f79190610be9565b60405180910390f35b60606003805461020f90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461023b90610d2a565b80156102885780601f1061025d57610100808354040283529160200191610288565b820191906000526020600020905b81548152906001019060200180831161026b57829003601f168201915b5050505050905090565b60008061029d61047b565b90506102aa818585610483565b600191505092915050565b6000600254905090565b6000806102ca61047b565b90506102d7858285610495565b6102e2858585610529565b60019150509392505050565b60006012905090565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60606004805461034e90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461037a90610d2a565b80156103c75780601f1061039c576101008083540402835291602001916103c7565b820191906000526020600020905b8154815290600101906020018083116103aa57829003601f168201915b5050505050905090565b6000806103dc61047b565b90506103e9818585610529565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b610490838383600161061d565b505050565b60006104a184846103f4565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146105235781811015610513578281836040517ffb8f41b200000000000000000000000000000000000000000000000000000000815260040161050a93929190610d6a565b60405180910390fd5b6105228484848403600061061d565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361059b5760006040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105929190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361060d5760006040517fec442f050000000000000000000000000000000000000000000000000000000081526004016106049190610da1565b60405180910390fd5b6106188383836107f4565b505050565b600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff160361068f5760006040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106869190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036107015760006040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106f89190610da1565b60405180910390fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555080156107ee578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107e59190610be9565b60405180910390a35b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361084657806002600082825461083a9190610deb565b92505081905550610919565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050818110156108d2578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108c993929190610d6a565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361096257806002600082825403925050819055506109af565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051610a0c9190610be9565b60405180910390a3505050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610a53578082015181840152602081019050610a38565b60008484015250505050565b6000601f19601f8301169050919050565b6000610a7b82610a19565b610a858185610a24565b9350610a95818560208601610a35565b610a9e81610a5f565b840191505092915050565b60006020820190508181036000830152610ac38184610a70565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610afb82610ad0565b9050919050565b610b0b81610af0565b8114610b1657600080fd5b50565b600081359050610b2881610b02565b92915050565b6000819050919050565b610b4181610b2e565b8114610b4c57600080fd5b50565b600081359050610b5e81610b38565b92915050565b60008060408385031215610b7b57610b7a610acb565b5b6000610b8985828601610b19565b9250506020610b9a85828601610b4f565b9150509250929050565b60008115159050919050565b610bb981610ba4565b82525050565b6000602082019050610bd46000830184610bb0565b92915050565b610be381610b2e565b82525050565b6000602082019050610bfe6000830184610bda565b92915050565b600080600060608486031215610c1d57610c1c610acb565b5b6000610c2b86828701610b19565b9350506020610c3c86828701610b19565b9250506040610c4d86828701610b4f565b9150509250925092565b600060ff82169050919050565b610c6d81610c57565b82525050565b6000602082019050610c886000830184610c64565b92915050565b600060208284031215610ca457610ca3610acb565b5b6000610cb284828501610b19565b91505092915050565b60008060408385031215610cd257610cd1610acb565b5b6000610ce085828601610b19565b9250506020610cf185828601610b19565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610d4257607f821691505b602082108103610d5557610d54610cfb565b5b50919050565b610d6481610af0565b82525050565b6000606082019050610d7f6000830186610d5b565b610d8c6020830185610bda565b610d996040830184610bda565b949350505050565b6000602082019050610db66000830184610d5b565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610df682610b2e565b9150610e0183610b2e565b9250828201905080821115610e1957610e18610dbc565b5b9291505056fea2646970667358221220912f5265edaea44910db734f0d00fccd257c78dba79c126931551eaad1a334f764736f6c63430008170033'; -// Defining the private key for the deployer account, which has VTHO for deployment costs -const privateKeyDeployer = - '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff'; +// Defining the deployer account, which has VTHO for deployment costs +const deployerAccount: ProviderInternalWalletAccount = { + privateKey: Buffer.from( + '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff', + 'hex' + ), + address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' +}; +// Defining the delegator account, which has VTHO for transaction costs const delegatorAccount = { privateKey: '521b7793c6eb27d137b617627c6b85d57c0aa303380e9ca4e30a30302fbc6676', @@ -22,13 +33,25 @@ const delegatorAccount = { // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount], { + delegator: { + delegatorPrivateKey: delegatorAccount.privateKey + } + }), + true +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; // Defining a function for deploying the ERC20 contract const setupERC20Contract = async (): Promise => { const contractFactory = thorSoloClient.contracts.createContractFactory( VIP180_ABI, erc20ContractBytecode, - privateKeyDeployer + signer ); // Deploying the contract diff --git a/docs/examples/contracts/contract-deposit.ts b/docs/examples/contracts/contract-deposit.ts index d9833cf40..6dcdf0d62 100644 --- a/docs/examples/contracts/contract-deposit.ts +++ b/docs/examples/contracts/contract-deposit.ts @@ -1,15 +1,25 @@ import { expect } from 'expect'; import { type InterfaceAbi } from '@vechain/sdk-core'; -import { ThorClient } from '@vechain/sdk-network'; +import { + ProviderInternalBaseWallet, + type ProviderInternalWalletAccount, + ThorClient, + VechainProvider, + type VechainSigner +} from '@vechain/sdk-network'; // Deposit contract bytecode +// Deposit contract bytecode const depositContractBytecode: string = '0x608060405234801561001057600080fd5b50610405806100206000396000f3fe6080604052600436106100345760003560e01c806327e235e314610039578063d0e30db014610076578063f8b2cb4f14610080575b600080fd5b34801561004557600080fd5b50610060600480360381019061005b9190610268565b6100bd565b60405161006d91906102ae565b60405180910390f35b61007e6100d5565b005b34801561008c57600080fd5b506100a760048036038101906100a29190610268565b6101bd565b6040516100b491906102ae565b60405180910390f35b60006020528060005260406000206000915090505481565b60003411610118576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161010f9061034c565b60405180910390fd5b346000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610166919061039b565b925050819055503373ffffffffffffffffffffffffffffffffffffffff167fd15c9547ea5c06670c0010ce19bc32d54682a4b3801ece7f3ab0c3f17106b4bb346040516101b391906102ae565b60405180910390a2565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006102358261020a565b9050919050565b6102458161022a565b811461025057600080fd5b50565b6000813590506102628161023c565b92915050565b60006020828403121561027e5761027d610205565b5b600061028c84828501610253565b91505092915050565b6000819050919050565b6102a881610295565b82525050565b60006020820190506102c3600083018461029f565b92915050565b600082825260208201905092915050565b7f4465706f73697420616d6f756e74206d7573742062652067726561746572207460008201527f68616e2030000000000000000000000000000000000000000000000000000000602082015250565b60006103366025836102c9565b9150610341826102da565b604082019050919050565b6000602082019050818103600083015261036581610329565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006103a682610295565b91506103b183610295565b92508282019050808211156103c9576103c861036c565b5b9291505056fea2646970667358221220fd4fcedf2b3aacc02a6c483409206998028d766cf51d642f6c5c35d6f81118e864736f6c63430008180033'; -// Defining the private key for the deployer account, which has VTHO for deployment costs -const privateKeyDeployer = - '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff'; - -const privateKeyAddress = '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54'; +// Defining the deployer account, which has VTHO for deployment costs +const deployerAccount: ProviderInternalWalletAccount = { + privateKey: Buffer.from( + '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff', + 'hex' + ), + address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' +}; const depositContractAbi: InterfaceAbi = [ { @@ -81,6 +91,13 @@ const depositContractAbi: InterfaceAbi = [ // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount]) +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; // START_SNIPPET: DepositContractSnippet @@ -88,7 +105,7 @@ const thorSoloClient = ThorClient.fromUrl(_soloUrl); const contractFactory = thorSoloClient.contracts.createContractFactory( depositContractAbi, depositContractBytecode, - privateKeyDeployer + signer ); const contract = await ( @@ -97,7 +114,7 @@ const contract = await ( await (await contract.transact.deposit({ value: 1000 })).wait(); -const balance = await contract.read.getBalance(privateKeyAddress); +const balance = await contract.read.getBalance(deployerAccount.address); expect(balance).toEqual([BigInt(1000)]); diff --git a/docs/examples/contracts/contract-event-filter.ts b/docs/examples/contracts/contract-event-filter.ts index 064eab8c5..650a484bb 100644 --- a/docs/examples/contracts/contract-event-filter.ts +++ b/docs/examples/contracts/contract-event-filter.ts @@ -1,31 +1,45 @@ -import { addressUtils, VIP180_ABI } from '@vechain/sdk-core'; +import { VIP180_ABI } from '@vechain/sdk-core'; import { type Contract, + ProviderInternalBaseWallet, + type ProviderInternalWalletAccount, ThorClient, - type TransactionReceipt + type TransactionReceipt, + VechainProvider, + type VechainSigner } from '@vechain/sdk-network'; import { expect } from 'expect'; +// ERC20 contract bytecode const erc20ContractBytecode: string = '0x60806040523480156200001157600080fd5b506040518060400160405280600b81526020017f53616d706c65546f6b656e0000000000000000000000000000000000000000008152506040518060400160405280600281526020017f535400000000000000000000000000000000000000000000000000000000000081525081600390816200008f91906200062c565b508060049081620000a191906200062c565b505050620000e633620000b9620000ec60201b60201c565b60ff16600a620000ca919062000896565b620f4240620000da9190620008e7565b620000f560201b60201c565b62000a3a565b60006012905090565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036200016a5760006040517fec442f0500000000000000000000000000000000000000000000000000000000815260040162000161919062000977565b60405180910390fd5b6200017e600083836200018260201b60201c565b5050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001d8578060026000828254620001cb919062000994565b92505081905550620002ae565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490508181101562000267578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016200025e93929190620009e0565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002f9578060026000828254039250508190555062000346565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051620003a5919062000a1d565b60405180910390a3505050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200043457607f821691505b6020821081036200044a5762000449620003ec565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620004b47fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000475565b620004c0868362000475565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006200050d620005076200050184620004d8565b620004e2565b620004d8565b9050919050565b6000819050919050565b6200052983620004ec565b62000541620005388262000514565b84845462000482565b825550505050565b600090565b6200055862000549565b620005658184846200051e565b505050565b5b818110156200058d57620005816000826200054e565b6001810190506200056b565b5050565b601f821115620005dc57620005a68162000450565b620005b18462000465565b81016020851015620005c1578190505b620005d9620005d08562000465565b8301826200056a565b50505b505050565b600082821c905092915050565b60006200060160001984600802620005e1565b1980831691505092915050565b60006200061c8383620005ee565b9150826002028217905092915050565b6200063782620003b2565b67ffffffffffffffff811115620006535762000652620003bd565b5b6200065f82546200041b565b6200066c82828562000591565b600060209050601f831160018114620006a457600084156200068f578287015190505b6200069b85826200060e565b8655506200070b565b601f198416620006b48662000450565b60005b82811015620006de57848901518255600182019150602085019450602081019050620006b7565b86831015620006fe5784890151620006fa601f891682620005ee565b8355505b6001600288020188555050505b505050505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008160011c9050919050565b6000808291508390505b6001851115620007a15780860481111562000779576200077862000713565b5b6001851615620007895780820291505b8081029050620007998562000742565b945062000759565b94509492505050565b600082620007bc57600190506200088f565b81620007cc57600090506200088f565b8160018114620007e55760028114620007f05762000826565b60019150506200088f565b60ff84111562000805576200080462000713565b5b8360020a9150848211156200081f576200081e62000713565b5b506200088f565b5060208310610133831016604e8410600b8410161715620008605782820a9050838111156200085a576200085962000713565b5b6200088f565b6200086f84848460016200074f565b9250905081840481111562000889576200088862000713565b5b81810290505b9392505050565b6000620008a382620004d8565b9150620008b083620004d8565b9250620008df7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8484620007aa565b905092915050565b6000620008f482620004d8565b91506200090183620004d8565b92508282026200091181620004d8565b915082820484148315176200092b576200092a62000713565b5b5092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200095f8262000932565b9050919050565b620009718162000952565b82525050565b60006020820190506200098e600083018462000966565b92915050565b6000620009a182620004d8565b9150620009ae83620004d8565b9250828201905080821115620009c957620009c862000713565b5b92915050565b620009da81620004d8565b82525050565b6000606082019050620009f7600083018662000966565b62000a066020830185620009cf565b62000a156040830184620009cf565b949350505050565b600060208201905062000a346000830184620009cf565b92915050565b610e558062000a4a6000396000f3fe608060405234801561001057600080fd5b50600436106100935760003560e01c8063313ce56711610066578063313ce5671461013457806370a082311461015257806395d89b4114610182578063a9059cbb146101a0578063dd62ed3e146101d057610093565b806306fdde0314610098578063095ea7b3146100b657806318160ddd146100e657806323b872dd14610104575b600080fd5b6100a0610200565b6040516100ad9190610aa9565b60405180910390f35b6100d060048036038101906100cb9190610b64565b610292565b6040516100dd9190610bbf565b60405180910390f35b6100ee6102b5565b6040516100fb9190610be9565b60405180910390f35b61011e60048036038101906101199190610c04565b6102bf565b60405161012b9190610bbf565b60405180910390f35b61013c6102ee565b6040516101499190610c73565b60405180910390f35b61016c60048036038101906101679190610c8e565b6102f7565b6040516101799190610be9565b60405180910390f35b61018a61033f565b6040516101979190610aa9565b60405180910390f35b6101ba60048036038101906101b59190610b64565b6103d1565b6040516101c79190610bbf565b60405180910390f35b6101ea60048036038101906101e59190610cbb565b6103f4565b6040516101f79190610be9565b60405180910390f35b60606003805461020f90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461023b90610d2a565b80156102885780601f1061025d57610100808354040283529160200191610288565b820191906000526020600020905b81548152906001019060200180831161026b57829003601f168201915b5050505050905090565b60008061029d61047b565b90506102aa818585610483565b600191505092915050565b6000600254905090565b6000806102ca61047b565b90506102d7858285610495565b6102e2858585610529565b60019150509392505050565b60006012905090565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60606004805461034e90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461037a90610d2a565b80156103c75780601f1061039c576101008083540402835291602001916103c7565b820191906000526020600020905b8154815290600101906020018083116103aa57829003601f168201915b5050505050905090565b6000806103dc61047b565b90506103e9818585610529565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b610490838383600161061d565b505050565b60006104a184846103f4565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146105235781811015610513578281836040517ffb8f41b200000000000000000000000000000000000000000000000000000000815260040161050a93929190610d6a565b60405180910390fd5b6105228484848403600061061d565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361059b5760006040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105929190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361060d5760006040517fec442f050000000000000000000000000000000000000000000000000000000081526004016106049190610da1565b60405180910390fd5b6106188383836107f4565b505050565b600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff160361068f5760006040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106869190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036107015760006040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106f89190610da1565b60405180910390fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555080156107ee578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107e59190610be9565b60405180910390a35b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361084657806002600082825461083a9190610deb565b92505081905550610919565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050818110156108d2578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108c993929190610d6a565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361096257806002600082825403925050819055506109af565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051610a0c9190610be9565b60405180910390a3505050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610a53578082015181840152602081019050610a38565b60008484015250505050565b6000601f19601f8301169050919050565b6000610a7b82610a19565b610a858185610a24565b9350610a95818560208601610a35565b610a9e81610a5f565b840191505092915050565b60006020820190508181036000830152610ac38184610a70565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610afb82610ad0565b9050919050565b610b0b81610af0565b8114610b1657600080fd5b50565b600081359050610b2881610b02565b92915050565b6000819050919050565b610b4181610b2e565b8114610b4c57600080fd5b50565b600081359050610b5e81610b38565b92915050565b60008060408385031215610b7b57610b7a610acb565b5b6000610b8985828601610b19565b9250506020610b9a85828601610b4f565b9150509250929050565b60008115159050919050565b610bb981610ba4565b82525050565b6000602082019050610bd46000830184610bb0565b92915050565b610be381610b2e565b82525050565b6000602082019050610bfe6000830184610bda565b92915050565b600080600060608486031215610c1d57610c1c610acb565b5b6000610c2b86828701610b19565b9350506020610c3c86828701610b19565b9250506040610c4d86828701610b4f565b9150509250925092565b600060ff82169050919050565b610c6d81610c57565b82525050565b6000602082019050610c886000830184610c64565b92915050565b600060208284031215610ca457610ca3610acb565b5b6000610cb284828501610b19565b91505092915050565b60008060408385031215610cd257610cd1610acb565b5b6000610ce085828601610b19565b9250506020610cf185828601610b19565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610d4257607f821691505b602082108103610d5557610d54610cfb565b5b50919050565b610d6481610af0565b82525050565b6000606082019050610d7f6000830186610d5b565b610d8c6020830185610bda565b610d996040830184610bda565b949350505050565b6000602082019050610db66000830184610d5b565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610df682610b2e565b9150610e0183610b2e565b9250828201905080821115610e1957610e18610dbc565b5b9291505056fea2646970667358221220912f5265edaea44910db734f0d00fccd257c78dba79c126931551eaad1a334f764736f6c63430008170033'; -// Defining the private key for the deployer account, which has VTHO for deployment costs -const privateKeyDeployer = - '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff'; +// Defining the deployer account, which has VTHO for deployment costs +const deployerAccount: ProviderInternalWalletAccount = { + privateKey: Buffer.from( + '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff', + 'hex' + ), + address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' +}; -const deployerAddress = addressUtils.fromPrivateKey( - Buffer.from(privateKeyDeployer, 'hex') -); // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount]) +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; // Defining a function for deploying the ERC20 contract const setupERC20Contract = async (): Promise => { const contractFactory = thorSoloClient.contracts.createContractFactory( VIP180_ABI, erc20ContractBytecode, - privateKeyDeployer + signer ); // Deploying the contract diff --git a/docs/examples/contracts/contract-transfer-ERC20-token.ts b/docs/examples/contracts/contract-transfer-ERC20-token.ts index dad8fb7e2..60bbec627 100644 --- a/docs/examples/contracts/contract-transfer-ERC20-token.ts +++ b/docs/examples/contracts/contract-transfer-ERC20-token.ts @@ -1,28 +1,45 @@ import { VIP180_ABI } from '@vechain/sdk-core'; import { type Contract, + ProviderInternalBaseWallet, + type ProviderInternalWalletAccount, ThorClient, - type TransactionReceipt + type TransactionReceipt, + VechainProvider, + type VechainSigner } from '@vechain/sdk-network'; import { expect } from 'expect'; +// ERC20 contract bytecode const erc20ContractBytecode: string = '0x60806040523480156200001157600080fd5b506040518060400160405280600b81526020017f53616d706c65546f6b656e0000000000000000000000000000000000000000008152506040518060400160405280600281526020017f535400000000000000000000000000000000000000000000000000000000000081525081600390816200008f91906200062c565b508060049081620000a191906200062c565b505050620000e633620000b9620000ec60201b60201c565b60ff16600a620000ca919062000896565b620f4240620000da9190620008e7565b620000f560201b60201c565b62000a3a565b60006012905090565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036200016a5760006040517fec442f0500000000000000000000000000000000000000000000000000000000815260040162000161919062000977565b60405180910390fd5b6200017e600083836200018260201b60201c565b5050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001d8578060026000828254620001cb919062000994565b92505081905550620002ae565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490508181101562000267578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016200025e93929190620009e0565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002f9578060026000828254039250508190555062000346565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051620003a5919062000a1d565b60405180910390a3505050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200043457607f821691505b6020821081036200044a5762000449620003ec565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620004b47fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000475565b620004c0868362000475565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006200050d620005076200050184620004d8565b620004e2565b620004d8565b9050919050565b6000819050919050565b6200052983620004ec565b62000541620005388262000514565b84845462000482565b825550505050565b600090565b6200055862000549565b620005658184846200051e565b505050565b5b818110156200058d57620005816000826200054e565b6001810190506200056b565b5050565b601f821115620005dc57620005a68162000450565b620005b18462000465565b81016020851015620005c1578190505b620005d9620005d08562000465565b8301826200056a565b50505b505050565b600082821c905092915050565b60006200060160001984600802620005e1565b1980831691505092915050565b60006200061c8383620005ee565b9150826002028217905092915050565b6200063782620003b2565b67ffffffffffffffff811115620006535762000652620003bd565b5b6200065f82546200041b565b6200066c82828562000591565b600060209050601f831160018114620006a457600084156200068f578287015190505b6200069b85826200060e565b8655506200070b565b601f198416620006b48662000450565b60005b82811015620006de57848901518255600182019150602085019450602081019050620006b7565b86831015620006fe5784890151620006fa601f891682620005ee565b8355505b6001600288020188555050505b505050505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008160011c9050919050565b6000808291508390505b6001851115620007a15780860481111562000779576200077862000713565b5b6001851615620007895780820291505b8081029050620007998562000742565b945062000759565b94509492505050565b600082620007bc57600190506200088f565b81620007cc57600090506200088f565b8160018114620007e55760028114620007f05762000826565b60019150506200088f565b60ff84111562000805576200080462000713565b5b8360020a9150848211156200081f576200081e62000713565b5b506200088f565b5060208310610133831016604e8410600b8410161715620008605782820a9050838111156200085a576200085962000713565b5b6200088f565b6200086f84848460016200074f565b9250905081840481111562000889576200088862000713565b5b81810290505b9392505050565b6000620008a382620004d8565b9150620008b083620004d8565b9250620008df7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8484620007aa565b905092915050565b6000620008f482620004d8565b91506200090183620004d8565b92508282026200091181620004d8565b915082820484148315176200092b576200092a62000713565b5b5092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200095f8262000932565b9050919050565b620009718162000952565b82525050565b60006020820190506200098e600083018462000966565b92915050565b6000620009a182620004d8565b9150620009ae83620004d8565b9250828201905080821115620009c957620009c862000713565b5b92915050565b620009da81620004d8565b82525050565b6000606082019050620009f7600083018662000966565b62000a066020830185620009cf565b62000a156040830184620009cf565b949350505050565b600060208201905062000a346000830184620009cf565b92915050565b610e558062000a4a6000396000f3fe608060405234801561001057600080fd5b50600436106100935760003560e01c8063313ce56711610066578063313ce5671461013457806370a082311461015257806395d89b4114610182578063a9059cbb146101a0578063dd62ed3e146101d057610093565b806306fdde0314610098578063095ea7b3146100b657806318160ddd146100e657806323b872dd14610104575b600080fd5b6100a0610200565b6040516100ad9190610aa9565b60405180910390f35b6100d060048036038101906100cb9190610b64565b610292565b6040516100dd9190610bbf565b60405180910390f35b6100ee6102b5565b6040516100fb9190610be9565b60405180910390f35b61011e60048036038101906101199190610c04565b6102bf565b60405161012b9190610bbf565b60405180910390f35b61013c6102ee565b6040516101499190610c73565b60405180910390f35b61016c60048036038101906101679190610c8e565b6102f7565b6040516101799190610be9565b60405180910390f35b61018a61033f565b6040516101979190610aa9565b60405180910390f35b6101ba60048036038101906101b59190610b64565b6103d1565b6040516101c79190610bbf565b60405180910390f35b6101ea60048036038101906101e59190610cbb565b6103f4565b6040516101f79190610be9565b60405180910390f35b60606003805461020f90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461023b90610d2a565b80156102885780601f1061025d57610100808354040283529160200191610288565b820191906000526020600020905b81548152906001019060200180831161026b57829003601f168201915b5050505050905090565b60008061029d61047b565b90506102aa818585610483565b600191505092915050565b6000600254905090565b6000806102ca61047b565b90506102d7858285610495565b6102e2858585610529565b60019150509392505050565b60006012905090565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60606004805461034e90610d2a565b80601f016020809104026020016040519081016040528092919081815260200182805461037a90610d2a565b80156103c75780601f1061039c576101008083540402835291602001916103c7565b820191906000526020600020905b8154815290600101906020018083116103aa57829003601f168201915b5050505050905090565b6000806103dc61047b565b90506103e9818585610529565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b610490838383600161061d565b505050565b60006104a184846103f4565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146105235781811015610513578281836040517ffb8f41b200000000000000000000000000000000000000000000000000000000815260040161050a93929190610d6a565b60405180910390fd5b6105228484848403600061061d565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361059b5760006040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105929190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361060d5760006040517fec442f050000000000000000000000000000000000000000000000000000000081526004016106049190610da1565b60405180910390fd5b6106188383836107f4565b505050565b600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff160361068f5760006040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106869190610da1565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036107015760006040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106f89190610da1565b60405180910390fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555080156107ee578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107e59190610be9565b60405180910390a35b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361084657806002600082825461083a9190610deb565b92505081905550610919565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050818110156108d2578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108c993929190610d6a565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361096257806002600082825403925050819055506109af565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051610a0c9190610be9565b60405180910390a3505050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610a53578082015181840152602081019050610a38565b60008484015250505050565b6000601f19601f8301169050919050565b6000610a7b82610a19565b610a858185610a24565b9350610a95818560208601610a35565b610a9e81610a5f565b840191505092915050565b60006020820190508181036000830152610ac38184610a70565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610afb82610ad0565b9050919050565b610b0b81610af0565b8114610b1657600080fd5b50565b600081359050610b2881610b02565b92915050565b6000819050919050565b610b4181610b2e565b8114610b4c57600080fd5b50565b600081359050610b5e81610b38565b92915050565b60008060408385031215610b7b57610b7a610acb565b5b6000610b8985828601610b19565b9250506020610b9a85828601610b4f565b9150509250929050565b60008115159050919050565b610bb981610ba4565b82525050565b6000602082019050610bd46000830184610bb0565b92915050565b610be381610b2e565b82525050565b6000602082019050610bfe6000830184610bda565b92915050565b600080600060608486031215610c1d57610c1c610acb565b5b6000610c2b86828701610b19565b9350506020610c3c86828701610b19565b9250506040610c4d86828701610b4f565b9150509250925092565b600060ff82169050919050565b610c6d81610c57565b82525050565b6000602082019050610c886000830184610c64565b92915050565b600060208284031215610ca457610ca3610acb565b5b6000610cb284828501610b19565b91505092915050565b60008060408385031215610cd257610cd1610acb565b5b6000610ce085828601610b19565b9250506020610cf185828601610b19565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610d4257607f821691505b602082108103610d5557610d54610cfb565b5b50919050565b610d6481610af0565b82525050565b6000606082019050610d7f6000830186610d5b565b610d8c6020830185610bda565b610d996040830184610bda565b949350505050565b6000602082019050610db66000830184610d5b565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610df682610b2e565b9150610e0183610b2e565b9250828201905080821115610e1957610e18610dbc565b5b9291505056fea2646970667358221220912f5265edaea44910db734f0d00fccd257c78dba79c126931551eaad1a334f764736f6c63430008170033'; -// Defining the private key for the deployer account, which has VTHO for deployment costs -const privateKeyDeployer = - '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff'; +// Defining the deployer account, which has VTHO for deployment costs +const deployerAccount: ProviderInternalWalletAccount = { + privateKey: Buffer.from( + '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff', + 'hex' + ), + address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' +}; // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount]) +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; // Defining a function for deploying the ERC20 contract const setupERC20Contract = async (): Promise => { const contractFactory = thorSoloClient.contracts.createContractFactory( VIP180_ABI, erc20ContractBytecode, - privateKeyDeployer + signer ); // Deploying the contract diff --git a/docs/examples/thor-client/contract.ts b/docs/examples/thor-client/contract.ts index 968b0f116..374f50c44 100644 --- a/docs/examples/thor-client/contract.ts +++ b/docs/examples/thor-client/contract.ts @@ -1,4 +1,10 @@ -import { ThorClient } from '@vechain/sdk-network'; +import { + ProviderInternalBaseWallet, + type ProviderInternalWalletAccount, + ThorClient, + VechainProvider, + type VechainSigner +} from '@vechain/sdk-network'; import { expect } from 'expect'; import type { DeployParams, InterfaceAbi } from '@vechain/sdk-core'; @@ -11,8 +17,23 @@ const thorSoloClient = ThorClient.fromUrl(_soloUrl); // 2 - Deploy contract -const privateKeyDeployer = - '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff'; // Private key of a test account with VTHO (energy) to pay for the deployment +// Defining the deployer account, which has VTHO for deployment costs +const deployerAccount: ProviderInternalWalletAccount = { + privateKey: Buffer.from( + '706e6acd567fdc22db54aead12cb39db01c4832f149f95299aa8dd8bef7d28ff', + 'hex' + ), + address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' +}; + +// Define the provider and the signer +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount]) +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; const contractBytecode: string = '0x608060405234801561001057600080fd5b506040516102063803806102068339818101604052810190610032919061007a565b80600081905550506100a7565b600080fd5b6000819050919050565b61005781610044565b811461006257600080fd5b50565b6000815190506100748161004e565b92915050565b6000602082840312156100905761008f61003f565b5b600061009e84828501610065565b91505092915050565b610150806100b66000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806360fe47b11461003b5780636d4ce63c14610057575b600080fd5b610055600480360381019061005091906100c3565b610075565b005b61005f61007f565b60405161006c91906100ff565b60405180910390f35b8060008190555050565b60008054905090565b600080fd5b6000819050919050565b6100a08161008d565b81146100ab57600080fd5b50565b6000813590506100bd81610097565b92915050565b6000602082840312156100d9576100d8610088565b5b60006100e7848285016100ae565b91505092915050565b6100f98161008d565b82525050565b600060208201905061011460008301846100f0565b9291505056fea2646970667358221220785262acbf50fa50a7b4dc8d8087ca8904c7e6b847a13674503fdcbac903b67e64736f6c63430008170033'; @@ -38,7 +59,7 @@ const deployedContractAbi: InterfaceAbi = [ let contractFactory = thorSoloClient.contracts.createContractFactory( deployedContractAbi, contractBytecode, - privateKeyDeployer + signer ); // Deploy parameters to be used for the contract creation diff --git a/docs/examples/transactions/full-flow-delegator-private-key.ts b/docs/examples/transactions/full-flow-delegator-private-key.ts index eb46b2197..90d1d4cc3 100644 --- a/docs/examples/transactions/full-flow-delegator-private-key.ts +++ b/docs/examples/transactions/full-flow-delegator-private-key.ts @@ -90,7 +90,7 @@ const signer = await providerWithDelegationEnabled.getSigner( senderAccount.address ); -const rawDelegateSigned = await signer.signTransactionWithDelegator( +const rawDelegateSigned = await signer.signTransaction( signerUtils.transactionBodyToTransactionRequestInput( txBody, senderAccount.address diff --git a/docs/examples/transactions/full-flow-delegator-url.ts b/docs/examples/transactions/full-flow-delegator-url.ts index a7495b337..d05e7cc5a 100644 --- a/docs/examples/transactions/full-flow-delegator-url.ts +++ b/docs/examples/transactions/full-flow-delegator-url.ts @@ -90,7 +90,7 @@ const signer = await providerWithDelegationEnabled.getSigner( senderAccount.address ); -const rawDelegateSigned = await signer.signTransactionWithDelegator( +const rawDelegateSigned = await signer.signTransaction( signerUtils.transactionBodyToTransactionRequestInput( txBody, senderAccount.address diff --git a/docs/transactions.md b/docs/transactions.md index 6b7a27c05..b7be570ca 100644 --- a/docs/transactions.md +++ b/docs/transactions.md @@ -548,7 +548,7 @@ const signer = await providerWithDelegationEnabled.getSigner( senderAccount.address ); -const rawDelegateSigned = await signer.signTransactionWithDelegator( +const rawDelegateSigned = await signer.signTransaction( signerUtils.transactionBodyToTransactionRequestInput( txBody, senderAccount.address @@ -652,7 +652,7 @@ const signer = await providerWithDelegationEnabled.getSigner( senderAccount.address ); -const rawDelegateSigned = await signer.signTransactionWithDelegator( +const rawDelegateSigned = await signer.signTransaction( signerUtils.transactionBodyToTransactionRequestInput( txBody, senderAccount.address diff --git a/packages/network/src/provider/helpers/provider-internal-wallets/base-wallet/provider-internal-base-wallet.ts b/packages/network/src/provider/helpers/provider-internal-wallets/base-wallet/provider-internal-base-wallet.ts index 361625c12..bbc3bbc20 100644 --- a/packages/network/src/provider/helpers/provider-internal-wallets/base-wallet/provider-internal-base-wallet.ts +++ b/packages/network/src/provider/helpers/provider-internal-wallets/base-wallet/provider-internal-base-wallet.ts @@ -55,10 +55,10 @@ class ProviderInternalBaseWallet implements ProviderInternalWallet { * @param address - Address of the account. * @returns The signer for the given address. */ - async getSigner( - parentProvider: TProviderType, + async getSigner( + parentProvider: AvailableVechainProviders, address: string - ): Promise | null> { + ): Promise { // Get the account from the wallet const signerAccount = await this.getAccount(address); diff --git a/packages/network/src/provider/helpers/provider-internal-wallets/types.d.ts b/packages/network/src/provider/helpers/provider-internal-wallets/types.d.ts index 376b6b68f..128436725 100644 --- a/packages/network/src/provider/helpers/provider-internal-wallets/types.d.ts +++ b/packages/network/src/provider/helpers/provider-internal-wallets/types.d.ts @@ -1,8 +1,5 @@ import { type SignTransactionOptions } from '../../../thor-client'; -import { - type AvailableVechainProviders, - type VechainSigner -} from '../../../signer'; +import { type VechainSigner } from '../../../signer'; /** * Represent a single account in a provider internal wallet. @@ -54,10 +51,10 @@ interface ProviderInternalWallet { * @param address - Address of the account. * @returns The signer for the given address. */ - getSigner: ( + getSigner: ( parentProvider: TProviderType, address: string - ) => Promise | null>; + ) => Promise; /** * Get the list of addresses in the wallet. diff --git a/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts b/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts index 33bbf185a..9d2052fe3 100644 --- a/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts +++ b/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts @@ -277,7 +277,7 @@ class VechainProvider extends EventEmitter implements EIP1193ProviderMessage { * @param address - Address of the account. * @returns The signer for the given address. */ - async getSigner(address: string): Promise | null> { + async getSigner(address: string): Promise { if (this.wallet === undefined) { return null; } diff --git a/packages/network/src/provider/utils/rpc-mapper/methods-map/methods/eth_sendTransaction/eth_sendTransaction.ts b/packages/network/src/provider/utils/rpc-mapper/methods-map/methods/eth_sendTransaction/eth_sendTransaction.ts index 82fef41b0..0c7b39fe3 100644 --- a/packages/network/src/provider/utils/rpc-mapper/methods-map/methods/eth_sendTransaction/eth_sendTransaction.ts +++ b/packages/network/src/provider/utils/rpc-mapper/methods-map/methods/eth_sendTransaction/eth_sendTransaction.ts @@ -77,7 +77,7 @@ const ethSendTransaction = async ( // Get the signer of the provider const signer = (await (provider as VechainProvider).getSigner( transaction.from - )) as VechainSigner; + )) as VechainSigner; // Return the result return await signer.sendTransaction(transaction); diff --git a/packages/network/src/signer/signers/types.d.ts b/packages/network/src/signer/signers/types.d.ts index 5f10e535e..eee89033c 100644 --- a/packages/network/src/signer/signers/types.d.ts +++ b/packages/network/src/signer/signers/types.d.ts @@ -218,27 +218,11 @@ interface TransactionRequestInput { * @NOTE: Su support completely our providers (that already support ethers provider format) * We use our supported providers instead of ethers providers */ -interface VechainSigner { - // START: Delegator needed methods - - /** - * Sign a transaction with the delegator - * - * @param transactionToSign - the transaction to sign - * @returns the fully signed transaction - */ - signTransactionWithDelegator: ( - transactionToSign: TransactionRequestInput - ) => Promise; - - // END: Delegator needed methods - - // START: Standard ethers signer methods adapted for vechain - +interface VechainSigner { /** * The provider attached to this Signer (if any). */ - provider: TProviderType | null; + provider: AvailableVechainProviders | null; /** * Returns a new instance of this Signer connected to //provider// or detached @@ -247,7 +231,7 @@ interface VechainSigner { * @param provider - The provider to connect to * @returns a new instance of this Signer connected to //provider// or detached */ - connect: (provider: TProviderType | null) => this; + connect: (provider: AvailableVechainProviders | null) => this; /** * Get the address of the Signer. @@ -389,8 +373,6 @@ interface VechainSigner { * Resolves an ENS Name to an address. */ // resolveName: (name: string) => Promise; - - // END: Standard ethers signer methods adapted for vechain } export { diff --git a/packages/network/src/signer/signers/vechain-base-signer/vechain-base-signer.ts b/packages/network/src/signer/signers/vechain-base-signer/vechain-base-signer.ts index 41f146754..c101df988 100644 --- a/packages/network/src/signer/signers/vechain-base-signer/vechain-base-signer.ts +++ b/packages/network/src/signer/signers/vechain-base-signer/vechain-base-signer.ts @@ -27,13 +27,11 @@ import { assertTransactionCanBeSigned } from '../../../assertions'; * Basic vechain signer. * This signer can be initialized using a private key. */ -class VechainBaseSigner - implements VechainSigner -{ +class VechainBaseSigner implements VechainSigner { /** * The provider attached to this Signer (if any). */ - provider: TProviderType | null; + provider: AvailableVechainProviders | null; /** * Create a new VechainBaseSigner. @@ -44,7 +42,7 @@ class VechainBaseSigner */ constructor( private readonly privateKey: Buffer, - provider: TProviderType | null + provider: AvailableVechainProviders | null ) { // Store provider and delegator this.provider = provider; @@ -57,7 +55,7 @@ class VechainBaseSigner * @param provider - The provider to connect to * @returns a new instance of this Signer connected to //provider// or detached */ - connect(provider: TProviderType | null): this { + connect(provider: AvailableVechainProviders | null): this { return new VechainBaseSigner(this.privateKey, provider) as this; } @@ -152,11 +150,12 @@ class VechainBaseSigner // 1 - Get the thor client assert( 'populateTransaction', - (this.provider as TProviderType).thorClient !== null, + (this.provider as AvailableVechainProviders).thorClient !== null, JSONRPC.INVALID_PARAMS, 'Thor client not found into the signer. Please attach a Provider with a thor client to your signer instance.' ); - const thorClient = (this.provider as TProviderType).thorClient; + const thorClient = (this.provider as AvailableVechainProviders) + .thorClient; // 2 - Populate the call, to get proper 'from' and 'to' address (compatible with multi-clause transactions) const populatedTransaction = await this.populateCall( @@ -200,11 +199,12 @@ class VechainBaseSigner // 1 - Get the thor client assert( 'populateTransaction', - (this.provider as TProviderType).thorClient !== null, + (this.provider as AvailableVechainProviders).thorClient !== null, JSONRPC.INVALID_PARAMS, 'Thor client not found into the signer. Please attach a Provider with a thor client to your signer instance.' ); - const thorClient = (this.provider as TProviderType).thorClient; + const thorClient = (this.provider as AvailableVechainProviders) + .thorClient; // 2 - Populate the call, to get proper from and to address (compatible with multi-clause transactions) const populatedTransaction = await this.populateCall( @@ -242,11 +242,12 @@ class VechainBaseSigner // 1 - Get the thor client assert( 'call', - (this.provider as TProviderType).thorClient !== null, + (this.provider as AvailableVechainProviders).thorClient !== null, JSONRPC.INVALID_PARAMS, 'Thor client not found into the signer. Please attach a Provider with a thor client to your signer instance.' ); - const thorClient = (this.provider as TProviderType).thorClient; + const thorClient = (this.provider as AvailableVechainProviders) + .thorClient; // 2 - Populate the call, to get proper from and to address (compatible with multi-clause transactions) const populatedTransaction = await this.populateCall( @@ -299,42 +300,12 @@ class VechainBaseSigner async signTransaction( transactionToSign: TransactionRequestInput ): Promise { - return await this._signFlow( - transactionToSign, - null, - (this.provider as TProviderType).thorClient, - this.privateKey - ); - } - - /** - * Sign a transaction with the delegator - * - * @param transactionToSign - the transaction to sign - * @returns the fully signed transaction - */ - async signTransactionWithDelegator( - transactionToSign: TransactionRequestInput - ): Promise { - // Get the delegator - const delegator = DelegationHandler( - await this.provider?.wallet?.getDelegator() - ).delegatorOrNull(); - - // Throw an error if the delegator is not available - assert( - 'signTransactionWithDelegator', - delegator !== null, - JSONRPC.INVALID_PARAMS, - 'Delegator not found. Ensure that the provider contains the delegator used to sign the transaction.' - ); - return await this._signFlow( transactionToSign, DelegationHandler( await this.provider?.wallet?.getDelegator() ).delegatorOrNull(), - (this.provider as TProviderType).thorClient, + (this.provider as AvailableVechainProviders).thorClient, this.privateKey ); } @@ -361,17 +332,12 @@ class VechainBaseSigner JSONRPC.INVALID_PARAMS, 'Thor provider is not found into the signer. Please attach a Provider to your signer instance.' ); - const provider = this.provider as TProviderType; - - // 2 - Understand if the transaction is delegated or not - const isDelegated = provider.enableDelegation as boolean; + const provider = this.provider as AvailableVechainProviders; - // 3 - Sign the transaction - const signedTransaction = isDelegated - ? await this.signTransactionWithDelegator(transactionToSend) - : await this.signTransaction(transactionToSend); + // 2 - Sign the transaction + const signedTransaction = await this.signTransaction(transactionToSend); - // 4 - Send the signed transaction + // 3 - Send the signed transaction return (await provider.request({ method: RPC_METHODS.eth_sendRawTransaction, params: [signedTransaction] diff --git a/packages/network/src/thor-client/contracts/contracts-module.ts b/packages/network/src/thor-client/contracts/contracts-module.ts index 6608c7ce7..8e9a985aa 100644 --- a/packages/network/src/thor-client/contracts/contracts-module.ts +++ b/packages/network/src/thor-client/contracts/contracts-module.ts @@ -1,6 +1,5 @@ import { abi, - addressUtils, clauseBuilder, coder, dataUtils, @@ -17,16 +16,11 @@ import type { ContractCallResult, ContractTransactionOptions } from './types'; -import { - DelegationHandler, - type SendTransactionResult, - type SignTransactionOptions -} from '../transactions'; +import { type SendTransactionResult } from '../transactions'; import { type ThorClient } from '../thor-client'; import { Contract, ContractFactory } from './model'; import { decodeRevertReason } from '../gas/helpers/decode-evm-error'; -import { signerUtils, VechainBaseSigner } from '../../signer'; -import { ProviderInternalBaseWallet, VechainProvider } from '../../provider'; +import { signerUtils, type VechainSigner } from '../../signer'; /** * Represents a module for interacting with smart contracts on the blockchain. @@ -39,20 +33,20 @@ class ContractsModule { constructor(readonly thor: ThorClient) {} /** - * Creates a new instance of `ContractFactory` configured with the specified ABI, bytecode, and private key. + * Creates a new instance of `ContractFactory` configured with the specified ABI, bytecode, and signer. * This factory is used to deploy new smart contracts to the blockchain network managed by this instance. * * @param abi - The Application Binary Interface (ABI) of the contract, which defines the contract's methods and events. * @param bytecode - The compiled bytecode of the contract, representing the contract's executable code. - * @param privateKey - The private key used for signing transactions during contract deployment, ensuring the deployer's identity. - * @returns An instance of `ContractFactory` configured with the provided ABI, bytecode, and private key, ready for deploying contracts. + * @param signer - The signer used for signing transactions during contract deployment, ensuring the deployer's identity. + * @returns An instance of `ContractFactory` configured with the provided ABI, bytecode, and signer, ready for deploying contracts. */ public createContractFactory( abi: InterfaceAbi, bytecode: string, - privateKey: string + signer: VechainSigner ): ContractFactory { - return new ContractFactory(abi, bytecode, privateKey, this.thor); + return new ContractFactory(abi, bytecode, signer, this.thor); } /** @@ -60,15 +54,15 @@ class ContractsModule { * * @param address - The blockchain address of the contract to load. * @param abi - The Application Binary Interface (ABI) of the contract, which defines the contract's methods and structures. - * @param callerPrivateKey - Optional. The private key of the caller, used for signing transactions when interacting with the contract. - * @returns A new instance of the Contract, initialized with the provided address, ABI, and optionally, a caller private key. + * @param signer - Optional. The signer caller, used for signing transactions when interacting with the contract. + * @returns A new instance of the Contract, initialized with the provided address, ABI, and optionally, a signer. */ public load( address: string, abi: InterfaceAbi, - callerPrivateKey?: string + signer?: VechainSigner ): Contract { - return new Contract(address, abi, this.thor, callerPrivateKey); + return new Contract(address, abi, this.thor, signer); } /** @@ -120,7 +114,7 @@ class ContractsModule { /** * Executes a transaction to interact with a smart contract function. * - * @param privateKey - The private key for signing the transaction. + * @param signer - The signer used for signing the transaction. * @param contractAddress - The address of the smart contract. * @param functionFragment - The function fragment, including the name and types of the function to be called, derived from the contract's ABI. * @param functionData - The input data for the function. @@ -131,7 +125,7 @@ class ContractsModule { * @returns A promise resolving to a SendTransactionResult object. */ public async executeTransaction( - privateKey: string, + signer: VechainSigner, contractAddress: string, functionFragment: FunctionFragment, functionData: unknown[], @@ -148,7 +142,7 @@ class ContractsModule { // Estimate the gas cost of the transaction const gasResult = await this.thor.gas.estimateGas( [clause], - addressUtils.fromPrivateKey(Buffer.from(privateKey, 'hex')) + await signer.getAddress() ); // Build a transaction for calling the contract function @@ -158,12 +152,8 @@ class ContractsModule { options ); - // Sign the transaction with the private key - const result = await this._signContractTransaction( - privateKey, - txBody, - this.buildSignTransactionOptions(options) - ); + // Sign the transaction + const result = await this._signContractTransaction(signer, txBody); result.wait = async () => await this.thor.transactions.waitForTransaction(result.id); @@ -173,48 +163,24 @@ class ContractsModule { /** * Internal function used to sign a contract transaction - * with the provided private key. + * with the provided signer * - * @param privateKey - The private key for signing the transaction. + * @param signer - The signer used for signing the transaction. * @param txBody - The transaction body to sign. * - * @param signTransactionOptions - (Optional) An object containing options for the transaction signature. * @private */ private async _signContractTransaction( - privateKey: string, - txBody: TransactionBody, - signTransactionOptions?: SignTransactionOptions + signer: VechainSigner, + txBody: TransactionBody ): Promise { - const signer = new VechainBaseSigner( - Buffer.from(privateKey, 'hex'), - new VechainProvider( - this.thor, - new ProviderInternalBaseWallet([], { - delegator: signTransactionOptions - }), - DelegationHandler(signTransactionOptions).isDelegated() + const signedTx = await signer.signTransaction( + signerUtils.transactionBodyToTransactionRequestInput( + txBody, + await signer.getAddress() ) ); - const signedTx = DelegationHandler(signTransactionOptions).isDelegated() - ? await signer.signTransactionWithDelegator( - signerUtils.transactionBodyToTransactionRequestInput( - txBody, - addressUtils.fromPrivateKey( - Buffer.from(privateKey, 'hex') - ) - ) - ) - : await signer.signTransaction( - signerUtils.transactionBodyToTransactionRequestInput( - txBody, - addressUtils.fromPrivateKey( - Buffer.from(privateKey, 'hex') - ) - ) - ); - return await this.thor.transactions.sendTransaction( TransactionHandler.decode( Buffer.from(signedTx.slice(2), 'hex'), @@ -226,18 +192,18 @@ class ContractsModule { /** * Executes a transaction to interact with multiple smart contract functions. * @param clauses - An array of transaction clauses to interact with the contract functions. - * @param privateKey - The private key for signing the transaction. + * @param signer - The signer used to signing the transaction. * @param options - (Optional) An object containing options for the transaction body. Includes all options of the `buildTransactionBody` method */ public async executeMultipleClausesTransaction( clauses: TransactionClause[], - privateKey: string, + signer: VechainSigner, options?: ContractTransactionOptions ): Promise { // Estimate the gas cost of the transaction const gasResult = await this.thor.gas.estimateGas( clauses, - addressUtils.fromPrivateKey(Buffer.from(privateKey, 'hex')) + await signer.getAddress() ); // Build a transaction for calling the contract function @@ -247,8 +213,8 @@ class ContractsModule { options ); - // Sign the transaction with the private key - const result = await this._signContractTransaction(privateKey, txBody); + // Sign the transaction + const result = await this._signContractTransaction(signer, txBody); result.wait = async () => await this.thor.transactions.waitForTransaction(result.id); @@ -274,36 +240,6 @@ class ContractsModule { [dataUtils.encodeBytes32String('base-gas-price', 'left')] ); } - - /** - * Build the sign transaction options based on the contract transaction options. - * @param options - The contract transaction options. - * @returns The sign transaction options to be used for signing the transaction. - * @private - */ - private buildSignTransactionOptions( - options: ContractTransactionOptions | undefined - ): SignTransactionOptions | undefined { - let signTransactionOptions: SignTransactionOptions | undefined; - - if ( - options?.signTransactionOptions?.delegatorPrivateKey !== undefined - ) { - signTransactionOptions = { - delegatorPrivateKey: - options.signTransactionOptions?.delegatorPrivateKey, - delegatorUrl: undefined - }; - } else if ( - options?.signTransactionOptions?.delegatorUrl !== undefined - ) { - signTransactionOptions = { - delegatorPrivateKey: undefined, - delegatorUrl: options.signTransactionOptions?.delegatorUrl - }; - } - return signTransactionOptions; - } } export { ContractsModule }; diff --git a/packages/network/src/thor-client/contracts/model/contract-factory.ts b/packages/network/src/thor-client/contracts/model/contract-factory.ts index 3236acc43..65b9d8223 100644 --- a/packages/network/src/thor-client/contracts/model/contract-factory.ts +++ b/packages/network/src/thor-client/contracts/model/contract-factory.ts @@ -1,5 +1,4 @@ import { - addressUtils, clauseBuilder, type DeployParams, type InterfaceAbi, @@ -13,8 +12,7 @@ import { type SendTransactionResult, type TransactionReceipt } from '../../transactions'; -import { signerUtils, VechainBaseSigner } from '../../../signer'; -import { VechainProvider } from '../../../provider'; +import { signerUtils, type VechainSigner } from '../../../signer'; /** * A factory class for deploying smart contracts to a blockchain using a ThorClient. @@ -31,9 +29,9 @@ class ContractFactory { private readonly bytecode: string; /** - * The private key used for signing transactions. + * The signer used for signing transactions. */ - private readonly privateKey: string; + private readonly signer: VechainSigner; /** * An instance of ThorClient to interact with the blockchain. @@ -49,18 +47,18 @@ class ContractFactory { * Initializes a new instance of the `ContractFactory` class. * @param abi The Application Binary Interface (ABI) of the contract, which defines the contract's methods and events. * @param bytecode The compiled bytecode of the contract, representing the contract's executable code. - * @param privateKey The private key used for signing transactions during contract deployment, ensuring the deployer's identity. + * @param signer The signer used for signing transactions during contract deployment, ensuring the deployer's identity. * @param thor An instance of ThorClient to interact with the blockchain. */ constructor( abi: InterfaceAbi, bytecode: string, - privateKey: string, + signer: VechainSigner, thor: ThorClient ) { this.abi = abi; this.bytecode = bytecode; - this.privateKey = privateKey; + this.signer = signer; this.thor = thor; } @@ -71,7 +69,7 @@ class ContractFactory { * 1. Builds a transaction clause for deploying the contract. * 2. Estimates the gas cost required for the transaction. * 3. Constructs the transaction body with the estimated gas cost. - * 4. Signs the transaction using the provided private key. + * 4. Signs the transaction using the provided signer. * 5. Sends the signed transaction to the blockchain. * * @param {DeployParams?} deployParams (Optional) parameters for contract deployment. @@ -93,7 +91,7 @@ class ContractFactory { // Estimate the gas cost of the transaction const gasResult = await this.thor.gas.estimateGas( [deployContractClause], - addressUtils.fromPrivateKey(Buffer.from(this.privateKey, 'hex')) + await this.signer.getAddress() ); const txBody = await this.thor.transactions.buildTransactionBody( @@ -102,15 +100,11 @@ class ContractFactory { options ); - // Sign the transaction with the provided private key - const signer = new VechainBaseSigner( - Buffer.from(this.privateKey, 'hex'), - new VechainProvider(this.thor) - ); - const signedTx = await signer.signTransaction( + // Sign the transaction + const signedTx = await this.signer.signTransaction( signerUtils.transactionBodyToTransactionRequestInput( txBody, - addressUtils.fromPrivateKey(Buffer.from(this.privateKey, 'hex')) + await this.signer.getAddress() ) ); @@ -169,7 +163,7 @@ class ContractFactory { transactionReceipt?.outputs[0].contractAddress as string, this.abi, this.thor, - this.privateKey, + this.signer, transactionReceipt as TransactionReceipt ); } diff --git a/packages/network/src/thor-client/contracts/model/contract-proxy.ts b/packages/network/src/thor-client/contracts/model/contract-proxy.ts index 5dd346032..8d3305a79 100644 --- a/packages/network/src/thor-client/contracts/model/contract-proxy.ts +++ b/packages/network/src/thor-client/contracts/model/contract-proxy.ts @@ -12,13 +12,13 @@ import { import { type Contract } from './contract'; import { buildError, ERROR_CODES } from '@vechain/sdk-errors'; import { - addressUtils, clauseBuilder, fragment, type TransactionClause } from '@vechain/sdk-core'; import { type ContractCallResult } from '../types'; import { ContractFilter } from './contract-filter'; +import { type VechainSigner } from '../../../signer'; /** * Creates a Proxy object for reading contract state, allowing for the dynamic invocation of contract read operations. @@ -36,13 +36,8 @@ function getReadProxy(contract: Contract): ContractFunctionRead { args, { caller: - contract.getCallerPrivateKey() !== undefined - ? addressUtils.fromPrivateKey( - Buffer.from( - contract.getCallerPrivateKey() as string, - 'hex' - ) - ) + contract.getSigner() !== undefined + ? await contract.getSigner()?.getAddress() : undefined, ...contract.getContractReadOptions() } @@ -65,11 +60,11 @@ function getTransactProxy(contract: Contract): ContractFunctionTransact { return async ( ...args: unknown[] ): Promise => { - if (contract.getCallerPrivateKey() === undefined) { + if (contract.getSigner() === undefined) { throw buildError( 'Contract.getTransactProxy', ERROR_CODES.TRANSACTION.MISSING_PRIVATE_KEY, - 'Caller private key is required to transact with the contract.', + 'Caller signer is required to transact with the contract.', { prop } ); } @@ -97,7 +92,7 @@ function getTransactProxy(contract: Contract): ContractFunctionTransact { } return await contract.thor.contracts.executeTransaction( - contract.getCallerPrivateKey() as string, + contract.getSigner() as VechainSigner, contract.address, contract.getFunctionFragment(prop), args, diff --git a/packages/network/src/thor-client/contracts/model/contract.ts b/packages/network/src/thor-client/contracts/model/contract.ts index 5fd6b852b..0d7d0eed7 100644 --- a/packages/network/src/thor-client/contracts/model/contract.ts +++ b/packages/network/src/thor-client/contracts/model/contract.ts @@ -20,6 +20,7 @@ import { getReadProxy, getTransactProxy } from './contract-proxy'; +import { type VechainSigner } from '../../../signer'; /** * A class representing a smart contract deployed on the blockchain. @@ -28,7 +29,7 @@ class Contract { readonly thor: ThorClient; readonly address: string; readonly abi: InterfaceAbi; - private callerPrivateKey?: string; + private signer?: VechainSigner; readonly deployTransactionReceipt: TransactionReceipt | undefined; @@ -45,21 +46,21 @@ class Contract { * @param address The address of the contract. * @param abi The Application Binary Interface (ABI) of the contract, which defines the contract's methods and events. * @param thor An instance of ThorClient to interact with the blockchain. - * @param callerPrivateKey The private key used for signing transactions. + * @param signer The signer caller used for signing transactions. * @param transactionReceipt (Optional) The transaction receipt of the contract deployment. */ constructor( address: string, abi: InterfaceAbi, thor: ThorClient, - callerPrivateKey?: string, + signer?: VechainSigner, transactionReceipt?: TransactionReceipt ) { this.abi = abi; this.thor = thor; this.address = address; this.deployTransactionReceipt = transactionReceipt; - this.callerPrivateKey = callerPrivateKey; + this.signer = signer; this.read = getReadProxy(this); this.transact = getTransactProxy(this); this.filters = getFilterProxy(this); @@ -130,23 +131,23 @@ class Contract { /** * Sets the private key of the caller for signing transactions. - * @param privateKey + * @param signer - The caller signer */ - public setCallerPrivateKey(privateKey: string): string { - this.callerPrivateKey = privateKey; + public setSigner(signer: VechainSigner): VechainSigner { + this.signer = signer; - // initialize the proxy with the new private key + // initialize the proxy with the new signer this.transact = getTransactProxy(this); this.read = getReadProxy(this); - return this.callerPrivateKey; + return this.signer; } /** - * Get the private key of the caller for signing transactions. - * @returns The private key of the caller. + * Get the caller signer used for signing transactions. + * @returns The signer used for signing transactions. */ - public getCallerPrivateKey(): string | undefined { - return this.callerPrivateKey; + public getSigner(): VechainSigner | undefined { + return this.signer; } /** diff --git a/packages/network/tests/provider/providers/hardhat/hardhat-provider.solo.test.ts b/packages/network/tests/provider/providers/hardhat/hardhat-provider.solo.test.ts index ee6040d35..fdb39b5ad 100644 --- a/packages/network/tests/provider/providers/hardhat/hardhat-provider.solo.test.ts +++ b/packages/network/tests/provider/providers/hardhat/hardhat-provider.solo.test.ts @@ -12,7 +12,8 @@ import { ProviderInternalBaseWallet, type SubscriptionEvent, ThorClient, - type VechainProvider + type VechainProvider, + type VechainSigner } from '../../../../src'; /** @@ -33,7 +34,12 @@ describe('Hardhat provider tests', () => { beforeEach(() => { thorClient = ThorClient.fromUrl(soloUrl); provider = new HardhatVechainProvider( - new ProviderInternalBaseWallet([]), + new ProviderInternalBaseWallet([ + { + privateKey: Buffer.from(TEST_ACCOUNT.privateKey, 'hex'), + address: TEST_ACCOUNT.address + } + ]), soloUrl, (message: string, parent?: Error) => new Error(message, parent), true @@ -171,7 +177,10 @@ describe('Hardhat provider tests', () => { * @throws {Error} If the received message doesn't match the expected format or if the log event details are incorrect, indicating an issue with the subscription or the event emission process. */ test('Should be able to get to subscribe to the latest logs of an erc20 contract', async () => { - const contract = await deployERC20Contract(thorClient); + const contract = await deployERC20Contract( + thorClient, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner + ); const logsParams = { address: [contract.address], @@ -188,7 +197,7 @@ describe('Hardhat provider tests', () => { // Execute a contract transaction to generate a log event await thorClient.contracts.executeTransaction( - TEST_ACCOUNT.privateKey, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner, contract.address, coder .createInterface(contract.abi) @@ -248,8 +257,14 @@ describe('Hardhat provider tests', () => { */ test('Should be able to subscribe to the latest logs of an erc20 and erc721 contract', async () => { // Test setup: Deploy contracts and set up event subscriptions - const erc20Contract = await deployERC20Contract(thorClient); - const erc721Contract = await deployERC721Contract(thorClient); + const erc20Contract = await deployERC20Contract( + thorClient, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner + ); + const erc721Contract = await deployERC721Contract( + thorClient, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner + ); const erc20logsParams = { address: [erc20Contract.address], @@ -285,7 +300,7 @@ describe('Hardhat provider tests', () => { // Execute transactions that should emit events await thorClient.contracts.executeTransaction( - TEST_ACCOUNT.privateKey, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner, erc20Contract.address, coder .createInterface(erc20Contract.abi) @@ -294,7 +309,7 @@ describe('Hardhat provider tests', () => { ); await thorClient.contracts.executeTransaction( - TEST_ACCOUNT.privateKey, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner, erc721Contract.address, coder .createInterface(erc721Contract.abi) diff --git a/packages/network/tests/provider/providers/helpers.ts b/packages/network/tests/provider/providers/helpers.ts index 72bf52283..25b61fd71 100644 --- a/packages/network/tests/provider/providers/helpers.ts +++ b/packages/network/tests/provider/providers/helpers.ts @@ -1,16 +1,12 @@ -import { - ERC20_ABI, - ERC20_BYTECODE, - ERC721_BYTECODE, - TEST_ACCOUNT -} from './fixture'; +import { ERC20_ABI, ERC20_BYTECODE, ERC721_BYTECODE } from './fixture'; import { ERC721_ABI } from '@vechain/sdk-core'; import { type Contract, type SubscriptionEvent, type ThorClient, - type VechainProvider + type VechainProvider, + type VechainSigner } from '../../../src'; export async function waitForMessage( @@ -25,12 +21,13 @@ export async function waitForMessage( } export async function deployERC20Contract( - thorClient: ThorClient + thorClient: ThorClient, + signer: VechainSigner ): Promise { const factory = thorClient.contracts.createContractFactory( ERC20_ABI, ERC20_BYTECODE, - TEST_ACCOUNT.privateKey + signer ); await factory.startDeployment(); @@ -39,12 +36,13 @@ export async function deployERC20Contract( } export async function deployERC721Contract( - thorClient: ThorClient + thorClient: ThorClient, + signer: VechainSigner ): Promise { const factory = thorClient.contracts.createContractFactory( ERC721_ABI, ERC721_BYTECODE, - TEST_ACCOUNT.privateKey + signer ); await factory.startDeployment(); diff --git a/packages/network/tests/provider/providers/vechain/vechain-provider.solo.test.ts b/packages/network/tests/provider/providers/vechain/vechain-provider.solo.test.ts index af3597879..02f83548f 100644 --- a/packages/network/tests/provider/providers/vechain/vechain-provider.solo.test.ts +++ b/packages/network/tests/provider/providers/vechain/vechain-provider.solo.test.ts @@ -10,9 +10,11 @@ import { } from '../helpers'; import { coder, type FunctionFragment } from '@vechain/sdk-core'; import { + ProviderInternalBaseWallet, type SubscriptionEvent, ThorClient, - VechainProvider + VechainProvider, + type VechainSigner } from '../../../../src'; /** @@ -32,7 +34,15 @@ describe('Vechain provider tests - solo', () => { */ beforeEach(() => { thorClient = ThorClient.fromUrl(soloUrl); - provider = new VechainProvider(thorClient); + provider = new VechainProvider( + thorClient, + new ProviderInternalBaseWallet([ + { + privateKey: Buffer.from(TEST_ACCOUNT.privateKey, 'hex'), + address: TEST_ACCOUNT.address + } + ]) + ); }); /** @@ -166,7 +176,10 @@ describe('Vechain provider tests - solo', () => { * @throws {Error} If the received message doesn't match the expected format or if the log event details are incorrect, indicating an issue with the subscription or the event emission process. */ test('Should be able to get to subscribe to the latest logs of an erc20 contract', async () => { - const contract = await deployERC20Contract(thorClient); + const contract = await deployERC20Contract( + thorClient, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner + ); const logsParams = { address: [contract.address], @@ -183,7 +196,7 @@ describe('Vechain provider tests - solo', () => { // Execute a contract transaction to generate a log event await thorClient.contracts.executeTransaction( - TEST_ACCOUNT.privateKey, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner, contract.address, coder .createInterface(contract.abi) @@ -243,8 +256,14 @@ describe('Vechain provider tests - solo', () => { */ test('Should be able to subscribe to the latest logs of an erc20 and erc721 contract', async () => { // Test setup: Deploy contracts and set up event subscriptions - const erc20Contract = await deployERC20Contract(thorClient); - const erc721Contract = await deployERC721Contract(thorClient); + const erc20Contract = await deployERC20Contract( + thorClient, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner + ); + const erc721Contract = await deployERC721Contract( + thorClient, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner + ); const erc20logsParams = { address: [erc20Contract.address], @@ -280,7 +299,7 @@ describe('Vechain provider tests - solo', () => { // Execute transactions that should emit events await thorClient.contracts.executeTransaction( - TEST_ACCOUNT.privateKey, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner, erc20Contract.address, coder .createInterface(erc20Contract.abi) @@ -289,7 +308,7 @@ describe('Vechain provider tests - solo', () => { ); await thorClient.contracts.executeTransaction( - TEST_ACCOUNT.privateKey, + (await provider.getSigner(TEST_ACCOUNT.address)) as VechainSigner, erc721Contract.address, coder .createInterface(erc721Contract.abi) diff --git a/packages/network/tests/signer/signers/vechain-base-signer/fixture.ts b/packages/network/tests/signer/signers/vechain-base-signer/fixture.ts index c0f1e5cbf..0f0f2da7b 100644 --- a/packages/network/tests/signer/signers/vechain-base-signer/fixture.ts +++ b/packages/network/tests/signer/signers/vechain-base-signer/fixture.ts @@ -130,6 +130,35 @@ const signTransactionTestCases = { } } } + ], + incorrect: [ + { + description: + 'Should NOT sign a transaction with delegation when no delegator is provided', + origin: TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER, + options: undefined, + isDelegated: true, + expected: { + body: { + chainTag: 39, + clauses: [ + { + data: '0x01cb08c5000000000000000000000000000000000000000000000000000000000000007b', + to: '0xb2c20a6de401003a671659b10629eb82ff254fb8', + value: 0 + } + ], + dependsOn: null, + expiration: 32, + gas: 21464, + gasPriceCoef: 0, + reserved: { + features: 1 + } + } + }, + expectedError: TransactionDelegationError + } ] } }; diff --git a/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.solo.test.ts b/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.solo.test.ts index f135a1a4c..e004c8d87 100644 --- a/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.solo.test.ts +++ b/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.solo.test.ts @@ -84,19 +84,12 @@ describe('Vechain base signer tests - testnet', () => { ) ); - const signedRawTx = isDelegated - ? await signer.signTransactionWithDelegator( - signerUtils.transactionBodyToTransactionRequestInput( - txBody, - origin.address - ) - ) - : await signer.signTransaction( - signerUtils.transactionBodyToTransactionRequestInput( - txBody, - origin.address - ) - ); + const signedRawTx = await signer.signTransaction( + signerUtils.transactionBodyToTransactionRequestInput( + txBody, + origin.address + ) + ); const signedTx = TransactionHandler.decode( Buffer.from(signedRawTx.slice(2), 'hex'), true @@ -150,7 +143,7 @@ describe('Vechain base signer tests - testnet', () => { ); await expect( - signer.signTransactionWithDelegator( + signer.signTransaction( signerUtils.transactionBodyToTransactionRequestInput( txBody, origin.address diff --git a/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.testnet.test.ts b/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.testnet.test.ts index 71e7f84cb..edca27f83 100644 --- a/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.testnet.test.ts +++ b/packages/network/tests/signer/signers/vechain-base-signer/vechain-base-signer.testnet.test.ts @@ -89,10 +89,9 @@ describe('Vechain base signer tests - testnet', () => { ); // Sign the transaction - const signedTransaction = - await signer.signTransactionWithDelegator({ - from: fixture.origin.address - }); + const signedTransaction = await signer.signTransaction({ + from: fixture.origin.address + }); expect(signedTransaction).toBeDefined(); } @@ -149,19 +148,12 @@ describe('Vechain base signer tests - testnet', () => { ) ); - const signedRawTx = isDelegated - ? await signer.signTransactionWithDelegator( - signerUtils.transactionBodyToTransactionRequestInput( - txBody, - origin.address - ) - ) - : await signer.signTransaction( - signerUtils.transactionBodyToTransactionRequestInput( - txBody, - origin.address - ) - ); + const signedRawTx = await signer.signTransaction( + signerUtils.transactionBodyToTransactionRequestInput( + txBody, + origin.address + ) + ); const signedTx = TransactionHandler.decode( Buffer.from(signedRawTx.slice(2), 'hex'), true diff --git a/packages/network/tests/thor-client/contracts/contract.erc20.solo.test.ts b/packages/network/tests/thor-client/contracts/contract.erc20.solo.test.ts index 022d1fc94..e2d8333c3 100644 --- a/packages/network/tests/thor-client/contracts/contract.erc20.solo.test.ts +++ b/packages/network/tests/thor-client/contracts/contract.erc20.solo.test.ts @@ -2,7 +2,10 @@ import { beforeEach, describe, expect, test } from '@jest/globals'; import { type Contract, ThorClient, - type TransactionReceipt + type TransactionReceipt, + VechainBaseSigner, + VechainProvider, + type VechainSigner } from '../../../src'; import { soloUrl, TEST_ACCOUNTS } from '../../fixture'; import { deployedERC20Abi, erc20ContractBytecode } from './fixture'; @@ -20,8 +23,18 @@ describe('ThorClient - ERC20 Contracts', () => { // ThorClient instance let thorSoloClient: ThorClient; + // Signer instance + let signer: VechainSigner; + beforeEach(() => { thorSoloClient = ThorClient.fromUrl(soloUrl); + signer = new VechainBaseSigner( + Buffer.from( + TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey, + 'hex' + ), + new VechainProvider(thorSoloClient) + ); }); /** @@ -32,7 +45,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -60,7 +73,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -99,7 +112,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -161,7 +174,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -181,7 +194,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -201,7 +214,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -242,7 +255,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -277,7 +290,7 @@ describe('ThorClient - ERC20 Contracts', () => { let factory = thorSoloClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); factory = await factory.startDeployment(); @@ -301,7 +314,7 @@ describe('ThorClient - ERC20 Contracts', () => { 3000 ) ], - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); await txResult.wait(); diff --git a/packages/network/tests/thor-client/contracts/contract.erc20.testnet.test.ts b/packages/network/tests/thor-client/contracts/contract.erc20.testnet.test.ts index c6d402080..d918828ea 100644 --- a/packages/network/tests/thor-client/contracts/contract.erc20.testnet.test.ts +++ b/packages/network/tests/thor-client/contracts/contract.erc20.testnet.test.ts @@ -1,10 +1,16 @@ import { beforeEach, describe, expect, test } from '@jest/globals'; -import { type Contract, ThorClient } from '../../../src'; +import { + type Contract, + ThorClient, + VechainBaseSigner, + VechainProvider, + type VechainSigner +} from '../../../src'; import { TEST_ACCOUNTS, testnetUrl } from '../../fixture'; import { - TESTNET_DELEGATE_URL, deployedERC20Abi, - erc20ContractBytecode + erc20ContractBytecode, + TESTNET_DELEGATE_URL } from './fixture'; /** @@ -18,8 +24,18 @@ describe('ThorClient - ERC20 Contracts on testnet', () => { // ThorClient instance let thorTestnetClient: ThorClient; + // Signer instance + let signer: VechainSigner; + beforeEach(() => { thorTestnetClient = ThorClient.fromUrl(testnetUrl); + signer = new VechainBaseSigner( + Buffer.from( + TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey, + 'hex' + ), + new VechainProvider(thorTestnetClient) + ); }); /** @@ -30,7 +46,7 @@ describe('ThorClient - ERC20 Contracts on testnet', () => { let factory = thorTestnetClient.contracts.createContractFactory( deployedERC20Abi, erc20ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); factory = await factory.startDeployment(); diff --git a/packages/network/tests/thor-client/contracts/contract.erc721.solo.test.ts b/packages/network/tests/thor-client/contracts/contract.erc721.solo.test.ts index b2c69e783..ef587f123 100644 --- a/packages/network/tests/thor-client/contracts/contract.erc721.solo.test.ts +++ b/packages/network/tests/thor-client/contracts/contract.erc721.solo.test.ts @@ -1,7 +1,13 @@ // Global variable to hold contract address import { erc721ContractBytecode, erc721ContractTestCases } from './fixture'; -import { expect, test, beforeAll, describe } from '@jest/globals'; -import { ThorClient, type TransactionReceipt } from '../../../src'; +import { beforeAll, describe, expect, test } from '@jest/globals'; +import { + ThorClient, + type TransactionReceipt, + VechainBaseSigner, + VechainProvider, + type VechainSigner +} from '../../../src'; import { soloUrl, TEST_ACCOUNTS } from '../../fixture'; import { coder, @@ -21,6 +27,9 @@ describe('ThorClient - ERC721 Contracts', () => { // ThorClient instance let thorSoloClient: ThorClient; + // Signer instance + let signer: VechainSigner; + let contractAddress: string; /** @@ -43,13 +52,20 @@ describe('ThorClient - ERC721 Contracts', () => { */ beforeAll(async () => { thorSoloClient = ThorClient.fromUrl(soloUrl); + signer = new VechainBaseSigner( + Buffer.from( + TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey, + 'hex' + ), + new VechainProvider(thorSoloClient) + ); // Create the ERC721 contract factory const factory = thorSoloClient.contracts.createContractFactory( ERC721_ABI, erc721ContractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER.privateKey + signer ); await factory.startDeployment(); @@ -89,8 +105,7 @@ describe('ThorClient - ERC721 Contracts', () => { } else { response = await thorSoloClient.contracts.executeTransaction( - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER - .privateKey, + signer, contractAddress, coder .createInterface(ERC721_ABI) diff --git a/packages/network/tests/thor-client/contracts/contract.solo.test.ts b/packages/network/tests/thor-client/contracts/contract.solo.test.ts index 10fb22397..2807af858 100644 --- a/packages/network/tests/thor-client/contracts/contract.solo.test.ts +++ b/packages/network/tests/thor-client/contracts/contract.solo.test.ts @@ -14,8 +14,8 @@ import { filterContractEventsTestCases, fourArgsEventAbi, multipleClausesTestCases, - testingContractNegativeTestCases, testingContractEVMExtensionTestCases, + testingContractNegativeTestCases, testingContractTestCases } from './fixture'; import { @@ -28,7 +28,10 @@ import { Contract, type ContractFactory, ThorClient, - type TransactionReceipt + type TransactionReceipt, + VechainBaseSigner, + VechainProvider, + type VechainSigner } from '../../../src'; import { ContractDeploymentFailedError, @@ -47,8 +50,18 @@ describe('ThorClient - Contracts', () => { // ThorClient instance let thorSoloClient: ThorClient; + // Signer instance + let signer: VechainSigner; + beforeEach(() => { thorSoloClient = ThorClient.fromUrl(soloUrl); + signer = new VechainBaseSigner( + Buffer.from( + TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey, + 'hex' + ), + new VechainProvider(thorSoloClient) + ); }); /** @@ -62,7 +75,7 @@ describe('ThorClient - Contracts', () => { const contractFactory = thorSoloClient.contracts.createContractFactory( deployedContractAbi, contractBytecode, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); // Start the deployment of the contract @@ -78,7 +91,7 @@ describe('ThorClient - Contracts', () => { '0x123', deployedContractAbi, thorSoloClient, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); expect(contract.address).toBeDefined(); expect(contract.abi).toBeDefined(); @@ -93,7 +106,7 @@ describe('ThorClient - Contracts', () => { '0x123', deployedContractAbi, thorSoloClient, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); expect(contract.address).toBeDefined(); expect(contract.abi).toBeDefined(); @@ -136,7 +149,7 @@ describe('ThorClient - Contracts', () => { let contractFactory = thorSoloClient.contracts.createContractFactory( deployedContractAbi, contractBytecode, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); // Start the deployment of the contract @@ -156,7 +169,7 @@ describe('ThorClient - Contracts', () => { const contractFactory = thorSoloClient.contracts.createContractFactory( deployedContractAbi, contractBytecode, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); // Waiting for a deployment that has not started @@ -262,21 +275,25 @@ describe('ThorClient - Contracts', () => { /** * Test case for calling a contract function with different private keys. */ - test('call a contract function with different private keys', async () => { + test('call a contract function with different signer', async () => { // Create a contract factory that is already deploying the example contract const factory = await createExampleContractFactory(); // Wait for the deployment to complete and obtain the contract instance const contract: Contract = await factory.waitForDeployment(); - contract.setCallerPrivateKey(''); + // Set signer with invalid private key + contract.setSigner( + new VechainBaseSigner( + Buffer.from('', 'hex'), + new VechainProvider(thorSoloClient) + ) + ); // The contract call should fail because the private key is not set await expect(contract.transact.set(123)).rejects.toThrow(); - contract.setCallerPrivateKey( - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey - ); + contract.setSigner(signer); await (await contract.transact.set(123)).wait(); @@ -307,9 +324,7 @@ describe('ThorClient - Contracts', () => { expect(callFunctionGetResult).toEqual([BigInt(100)]); // Set the private key of the caller for signing transactions - loadedContract.setCallerPrivateKey( - contract.getCallerPrivateKey() as string - ); + loadedContract.setSigner(contract.getSigner() as VechainSigner); // Call the set function of the loaded contract to set the value to 123 const callFunctionSetResponse = await loadedContract.transact.set(123); @@ -378,7 +393,7 @@ describe('ThorClient - Contracts', () => { thorSoloClient.contracts.createContractFactory( depositContractAbi, depositContractBytecode, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); const depositContract = await depositContractFactory.startDeployment(); @@ -464,7 +479,7 @@ describe('ThorClient - Contracts', () => { const contract = thorSoloClient.contracts.load( TESTING_CONTRACT_ADDRESS, TESTING_CONTRACT_ABI, - TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey + signer ); await (await contract.transact.setStateVariable(123)).wait(); @@ -504,7 +519,10 @@ describe('ThorClient - Contracts', () => { thorSoloClient.contracts.createContractFactory( contractAbi, contractBytecode, - contractCaller + new VechainBaseSigner( + Buffer.from(contractCaller, 'hex'), + new VechainProvider(thorSoloClient) + ) ); const factory = await contractFactory.startDeployment(); @@ -565,8 +583,14 @@ describe('ThorClient - Contracts', () => { return thorSoloClient.contracts.createContractFactory( contract.contractAbi, contract.contractBytecode, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER - .privateKey + new VechainBaseSigner( + Buffer.from( + TEST_ACCOUNTS.TRANSACTION + .CONTRACT_MANAGER.privateKey, + 'hex' + ), + new VechainProvider(thorSoloClient) + ) ); }); @@ -604,8 +628,14 @@ describe('ThorClient - Contracts', () => { const transactionResult = await thorSoloClient.contracts.executeMultipleClausesTransaction( contractClauses, - TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER - .privateKey + new VechainBaseSigner( + Buffer.from( + TEST_ACCOUNTS.TRANSACTION.CONTRACT_MANAGER + .privateKey, + 'hex' + ), + new VechainProvider(thorSoloClient) + ) ); const result = await transactionResult.wait(); diff --git a/packages/network/tests/utils/subscriptions/subscriptions.solo.test.ts b/packages/network/tests/utils/subscriptions/subscriptions.solo.test.ts index 970067ab5..7db021eda 100644 --- a/packages/network/tests/utils/subscriptions/subscriptions.solo.test.ts +++ b/packages/network/tests/utils/subscriptions/subscriptions.solo.test.ts @@ -187,7 +187,7 @@ describe('Subscriptions Solo network tests', () => { // Create a signer to sign the transaction const signer = (await provider.getSigner( TEST_ACCOUNTS.SUBSCRIPTION.EVENT_SUBSCRIPTION.address - )) as VechainBaseSigner; + )) as VechainBaseSigner; // Get the raw transaction const raw = await signer.signTransaction( @@ -268,7 +268,7 @@ describe('Subscriptions Solo network tests', () => { // Create a signer to sign the transaction const signer = (await provider.getSigner( TEST_ACCOUNTS.SUBSCRIPTION.VET_TRANSFERS_SUBSCRIPTION.address - )) as VechainBaseSigner; + )) as VechainBaseSigner; // Get the raw transaction const raw = await signer.signTransaction(