diff --git a/apps/docs-snippets/src/guide/wallets/instantiating-wallets.test.ts b/apps/docs-snippets/src/guide/wallets/instantiating-wallets.test.ts deleted file mode 100644 index 91268cbbea8..00000000000 --- a/apps/docs-snippets/src/guide/wallets/instantiating-wallets.test.ts +++ /dev/null @@ -1,150 +0,0 @@ -import type { WalletLocked, WalletUnlocked } from 'fuels'; -import { HDWallet, Wallet } from 'fuels'; -import { TestAssetId, launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('Instantiating wallets', () => { - it('should generate a new wallet just fine', () => { - // #region instantiating-wallets-1 - const unlockedWallet: WalletUnlocked = Wallet.generate(); - // #endregion instantiating-wallets-1 - - expect(unlockedWallet).toBeDefined(); - }); - - it('should instantiate an unlocked wallet from private key', () => { - // #region instantiating-wallets-2 - const privateKey = '0x36ca81ba70f3e04b7cc8780bff42d907ebca508097d4ae3df5147c93fd217f7c'; - - const myWallet: WalletUnlocked = Wallet.fromPrivateKey(privateKey); - // #endregion instantiating-wallets-2 - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate an unlocked wallet from mnemonic phrase', () => { - // #region instantiating-wallets-3 - const mnemonic = 'section gospel lady april mouse huge prosper boy urge fox tackle orient'; - - const myWallet: WalletUnlocked = Wallet.fromMnemonic(mnemonic); - // #endregion instantiating-wallets-3 - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate an unlocked wallet from seed', () => { - // #region instantiating-wallets-4 - const mySeed = '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca'; - - const myWallet: WalletUnlocked = Wallet.fromSeed(mySeed); - // #endregion instantiating-wallets-4 - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate an unlocked wallet from derived key', () => { - // #region instantiating-wallets-5 - const mySeed = '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca'; - - const extendedKey = HDWallet.fromSeed(mySeed).toExtendedKey(); - - const myWallet: WalletUnlocked = Wallet.fromExtendedKey(extendedKey); - // #endregion instantiating-wallets-5 - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate an unlocked wallet from JSON wallet', async () => { - // #region instantiating-wallets-6 - const jsonWallet = `{"id":"83d1792f-3230-496a-92af-3b44a1524fd6","version":3,"address":"ada436e1b80f855f94d678771c384504e46335f571aa244f11b5a70fe3e61644","crypto":{"cipher":"aes-128-ctr","mac":"6911499ec31a6a6d240220971730374396efd666bd34123d4e3ce85e4cf248c6","cipherparams":{"iv":"40576cbd4f7c84e88b0532320e23b425"},"ciphertext":"3e5e77f23444aa86b397dbc62e14d8b7d3fd7c7fe209e066bb7df17eca398129","kdf":"scrypt","kdfparams":{"dklen":32,"n":8192,"p":1,"r":8,"salt":"b046520d85090ee2abd6285174f37bc01e28846b6bb5edc03ae5f7c13aec03d2"}}}`; - - const password = 'password'; - - const myWallet: WalletUnlocked = await Wallet.fromEncryptedJson(jsonWallet, password); - // #endregion instantiating-wallets-6 - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate an unlocked wallet from a locked wallet', () => { - // #region instantiating-wallets-7 - const address = 'fuel1fjett54ahnydhklerngqhclzmmkmp6s0xnykns8dwsdpjfg3r2rsfazpw5'; - const privateKey = '0x9deba03f08676716e3a4247797672d8008a5198d183048be65415ef89447b890'; - - const lockedWallet: WalletLocked = Wallet.fromAddress(address); - - const unlockedWallet: WalletUnlocked = lockedWallet.unlock(privateKey); - // #endregion instantiating-wallets-7 - - expect(unlockedWallet).toBeDefined(); - expect(unlockedWallet.privateKey).toBeDefined(); - }); - - it('should instantiate a locked wallet using a bech32 string address', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - - const address = `fuel14kjrdcdcp7z4l9xk0pm3cwz9qnjxxd04wx4zgnc3kknslclxzezqyeux5d`; - - // #region instantiating-wallets-8 - const myWallet: WalletLocked = Wallet.fromAddress(address, provider); - // #endregion instantiating-wallets-8 - - myWallet.connect(provider); - - expect(myWallet).toBeDefined(); - }); - - it('should connect a wallet to a provider', async () => { - const address = `0xada436e1b80f855f94d678771c384504e46335f571aa244f11b5a70fe3e61644`; - const myWallet = Wallet.fromAddress(address); - - // #region instantiating-wallets-9 - using launched = await launchTestNode(); - const { provider } = launched; - - myWallet.connect(provider); - // #endregion instantiating-wallets-9 - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate wallet already connected to a provider', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - - const address = `0xada436e1b80f855f94d678771c384504e46335f571aa244f11b5a70fe3e61644`; - - // #region instantiating-wallets-10 - const myWallet: WalletLocked = Wallet.fromAddress(address, provider); - // #endregion instantiating-wallets-10 - - myWallet.connect(provider); - - expect(myWallet).toBeDefined(); - }); - - it('should instantiate multiple wallets with different configurations', async () => { - // #region multiple-wallets - using launched = await launchTestNode({ - walletsConfig: { - count: 3, - assets: [TestAssetId.A, TestAssetId.B], - coinsPerAsset: 5, - amountPerCoin: 100_000, - }, - }); - - const { - wallets: [wallet1, wallet2, wallet3], - } = launched; - // #endregion multiple-wallets - - expect(wallet1).toBeDefined(); - expect(wallet2).toBeDefined(); - expect(wallet3).toBeDefined(); - }); -}); diff --git a/apps/docs-snippets/src/guide/wallets/private-keys.test.ts b/apps/docs-snippets/src/guide/wallets/private-keys.test.ts deleted file mode 100644 index 67352e11b14..00000000000 --- a/apps/docs-snippets/src/guide/wallets/private-keys.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import type { WalletLocked, WalletUnlocked } from 'fuels'; -import { Signer, Wallet } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('Private keys', () => { - it('wallet-from-private-key', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - const lockedWallet: WalletLocked = testWallet.lock(); - const PRIVATE_KEY = privateKey; - - // #region wallet-from-private-key - // unlock an existing unlocked wallet - let unlockedWallet: WalletUnlocked = lockedWallet.unlock(PRIVATE_KEY); - // or directly from a private key - unlockedWallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider); - // #endregion wallet-from-private-key - - expect(unlockedWallet.address).toStrictEqual(testWallet.address); - }); - - it('signer-address', async () => { - using launched = await launchTestNode(); - const { - wallets: [testWallet], - } = launched; - - const PRIVATE_KEY = testWallet.privateKey; - - // #region signer-address - const signer = new Signer(PRIVATE_KEY); - // #endregion signer-address - - expect(testWallet.address).toStrictEqual(signer.address); - }); -}); diff --git a/apps/docs-snippets/src/guide/wallets/signing.test.ts b/apps/docs-snippets/src/guide/wallets/signing.test.ts deleted file mode 100644 index c03be471c59..00000000000 --- a/apps/docs-snippets/src/guide/wallets/signing.test.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { Address, ScriptTransactionRequest, Signer, WalletUnlocked, hashMessage } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('Signing', () => { - it('should sign a message using wallet instance', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - - // #region signing-1 - // #import { WalletUnlocked, hashMessage, Signer }; - - const wallet = WalletUnlocked.generate({ provider }); - - const message = 'my-message'; - const signedMessage = await wallet.signMessage(message); - // Example output: 0x277e1461cbb2e6a3250fa8c490221595efb3f4d66d43a4618d1013ca61ca56ba - - const hashedMessage = hashMessage(message); - // Example output: 0x40436501b686546b7c660bb18791ac2ae35e77fbe2ac977fc061922b9ec83766 - - const recoveredAddress = Signer.recoverAddress(hashedMessage, signedMessage); - // Example output: Address { - // bech32Address: 'fuel1za0wl90u09c6v88faqkvczu9r927kewvvr0asejv5xmdwtm98w0st7m2s3' - // } - // #endregion signing-1 - - expect(wallet.address).toEqual(recoveredAddress); - }); - - it('should sign a transaction using wallet instance [DETAILED]', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [wallet], - } = launched; - - // #region signing-2 - const request = new ScriptTransactionRequest({ - gasLimit: 10000, - }); - - request.addCoinOutput(Address.fromRandom(), 1000, provider.getBaseAssetId()); - - const txCost = await wallet.getTransactionCost(request); - - request.gasLimit = txCost.gasUsed; - request.maxFee = txCost.maxFee; - - await wallet.fund(request, txCost); - - const signedTransaction = await wallet.signTransaction(request); - const transactionId = request.getTransactionId(provider.getChainId()); - - const recoveredAddress = Signer.recoverAddress(transactionId, signedTransaction); - - request.updateWitnessByOwner(recoveredAddress, signedTransaction); - - const tx = await provider.sendTransaction(request); - const result = await tx.waitForResult(); - // #endregion signing-2 - - expect(result.isStatusSuccess).toBeTruthy(); - }); - - it('should sign a transaction using wallet instance [SIMPLIFIED]', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [wallet], - } = launched; - - const request = new ScriptTransactionRequest({ - gasLimit: 10000, - }); - - request.addCoinOutput(Address.fromRandom(), 1000, provider.getBaseAssetId()); - - const txCost = await wallet.getTransactionCost(request); - - request.gasLimit = txCost.gasUsed; - request.maxFee = txCost.maxFee; - - // #region signing-3 - await wallet.fund(request, txCost); - - const tx = await wallet.sendTransaction(request); - const result = await tx.waitForResult(); - // #endregion signing-3 - - expect(result.isStatusSuccess).toBeTruthy(); - }); -}); diff --git a/apps/docs-snippets/src/guide/wallets/wallet-transferring.test.ts b/apps/docs-snippets/src/guide/wallets/wallet-transferring.test.ts deleted file mode 100644 index ebae0e7b52d..00000000000 --- a/apps/docs-snippets/src/guide/wallets/wallet-transferring.test.ts +++ /dev/null @@ -1,176 +0,0 @@ -import type { TransferParams } from 'fuels'; -import { Wallet } from 'fuels'; -import { ASSET_A, launchTestNode } from 'fuels/test-utils'; - -import { CounterFactory } from '../../../test/typegen'; - -/** - * @group node - */ -describe('Wallet transferring', () => { - it('should transfer assets between wallets just fine', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - // #region wallet-transferring-1 - const myWallet = Wallet.fromPrivateKey(privateKey, provider); - - const recipient = Wallet.generate({ provider }); - - const txResponse = await myWallet.transfer(recipient.address, 100, provider.getBaseAssetId()); - - await txResponse.waitForResult(); - // #endregion wallet-transferring-1 - - const newBalance = await recipient.getBalance(provider.getBaseAssetId()); - - expect(newBalance.toNumber()).toBeGreaterThan(0); - }); - - it('should transfer assets to a recipient informing only the address string', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - // #region wallet-transferring-2 - const myWallet = Wallet.fromPrivateKey(privateKey, provider); - - const address = 'fuel1zc7r2rwuzl3uskfc0w737780uqd8sn6lfm3wgqf9wa767gs3sems5d6kxj'; - - const txResponse = await myWallet.transfer(address, 100, provider.getBaseAssetId()); - // #endregion wallet-transferring-2 - - await txResponse.waitForResult(); - - const newBalance = await Wallet.fromAddress(address, provider).getBalance( - provider.getBaseAssetId() - ); - - expect(newBalance.toNumber()).toBeGreaterThan(0); - }); - - it('should transfer base asset just fine', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - // #region wallet-transferring-3 - const myWallet = Wallet.fromPrivateKey(privateKey, provider); - - const recipient = Wallet.generate({ provider }); - - const txResponse = await myWallet.transfer(recipient.address, 100); - // #endregion wallet-transferring-3 - - await txResponse.waitForResult(); - - const newBalance = await recipient.getBalance(); - - expect(newBalance.toNumber()).toBeGreaterThan(0); - }); - - it('should successfully multi transfer to more than one receiver', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - const someOtherAssetId = ASSET_A; - - // #region wallet-transferring-6 - const myWallet = Wallet.fromPrivateKey(privateKey, provider); - - const recipient1 = Wallet.generate({ provider }); - const recipient2 = Wallet.generate({ provider }); - - const transfersToMake: TransferParams[] = [ - { amount: 100, destination: recipient1.address, assetId: provider.getBaseAssetId() }, - { amount: 200, destination: recipient2.address, assetId: provider.getBaseAssetId() }, - { amount: 300, destination: recipient2.address, assetId: someOtherAssetId }, - ]; - - const tx = await myWallet.batchTransfer(transfersToMake); - const { isStatusSuccess } = await tx.waitForResult(); - // #endregion wallet-transferring-6 - - expect(isStatusSuccess).toBeTruthy(); - }); - - it('should transfer assets to a deployed contract instance just fine', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: CounterFactory, - }, - ], - }); - const { - contracts: [contract], - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - // #region wallet-transferring-4 - const myWallet = Wallet.fromPrivateKey(privateKey, provider); - - const txResponse = await myWallet.transferToContract( - contract.id, - 100, - provider.getBaseAssetId() - ); - - await txResponse.waitForResult(); - // #endregion wallet-transferring-4 - - const newBalance = await contract.getBalance(provider.getBaseAssetId()); - - expect(newBalance.toNumber()).toBeGreaterThan(0); - }); - - it('should transfer assets to a deployed contract string address just fine', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: CounterFactory, - }, - ], - }); - const { - contracts: [contract], - provider, - wallets: [testWallet], - } = launched; - const privateKey = testWallet.privateKey; - - // #region wallet-transferring-5 - const myWallet = Wallet.fromPrivateKey(privateKey, provider); - - const contractAddress = contract.id.toString(); - - const txResponse = await myWallet.transferToContract( - contractAddress, - 100, - provider.getBaseAssetId() - ); - - await txResponse.waitForResult(); - // #endregion wallet-transferring-5 - - const newBalance = await contract.getBalance(provider.getBaseAssetId()); - - expect(newBalance.toNumber()).toBeGreaterThan(0); - }); -}); diff --git a/apps/docs-snippets2/scripts/wrap-snippets.ts b/apps/docs-snippets2/scripts/wrap-snippets.ts index a3fe5a1cc5b..6f0b19317b1 100644 --- a/apps/docs-snippets2/scripts/wrap-snippets.ts +++ b/apps/docs-snippets2/scripts/wrap-snippets.ts @@ -40,7 +40,7 @@ export const wrapSnippet = (filepath: string) => { const snippetsNoImports = imports.length ? snippetContents.split(imports)[1] : snippetContents; // Does the snippet requires node launcher? - const requiresNodeLauncher = /NETWORK_URL/.test(imports); + const requiresNodeLauncher = /NETWORK_URL/.test(imports) || /WALLET_/.test(imports); /* Removes .env file import diff --git a/apps/docs-snippets2/src/wallets/instantiating/connect-existing-wallet.ts b/apps/docs-snippets2/src/wallets/instantiating/connect-existing-wallet.ts new file mode 100644 index 00000000000..bbdb2f09b6b --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/connect-existing-wallet.ts @@ -0,0 +1,13 @@ +// #region instantiating-wallets-9 +import type { WalletLocked } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_ADDRESS } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const wallet: WalletLocked = Wallet.fromAddress(WALLET_ADDRESS); +wallet.connect(provider); +// #endregion instantiating-wallets-9 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/connect-new-wallet.ts b/apps/docs-snippets2/src/wallets/instantiating/connect-new-wallet.ts new file mode 100644 index 00000000000..df7fc1cee36 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/connect-new-wallet.ts @@ -0,0 +1,12 @@ +// #region instantiating-wallets-10 +import type { WalletLocked } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_ADDRESS } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const wallet: WalletLocked = Wallet.fromAddress(WALLET_ADDRESS, provider); +// #endregion instantiating-wallets-10 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-bech32-address.ts b/apps/docs-snippets2/src/wallets/instantiating/from-bech32-address.ts new file mode 100644 index 00000000000..1ef57e2f503 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-bech32-address.ts @@ -0,0 +1,10 @@ +// #region instantiating-wallets-8 +import type { WalletLocked } from 'fuels'; +import { Wallet } from 'fuels'; + +const address = `fuel14kjrdcdcp7z4l9xk0pm3cwz9qnjxxd04wx4zgnc3kknslclxzezqyeux5d`; + +const wallet: WalletLocked = Wallet.fromAddress(address); +// #endregion instantiating-wallets-8 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-hd-derived-key.ts b/apps/docs-snippets2/src/wallets/instantiating/from-hd-derived-key.ts new file mode 100644 index 00000000000..9e3b739ebeb --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-hd-derived-key.ts @@ -0,0 +1,13 @@ +// #region instantiating-wallets-5 +import type { WalletUnlocked } from 'fuels'; +import { HDWallet, Wallet } from 'fuels'; + +const seed = + '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca'; + +const extendedKey = HDWallet.fromSeed(seed).toExtendedKey(); + +const wallet: WalletUnlocked = Wallet.fromExtendedKey(extendedKey); +// #endregion instantiating-wallets-5 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-json-wallet.ts b/apps/docs-snippets2/src/wallets/instantiating/from-json-wallet.ts new file mode 100644 index 00000000000..ae7c3ce9424 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-json-wallet.ts @@ -0,0 +1,15 @@ +// #region instantiating-wallets-6 +import type { WalletUnlocked } from 'fuels'; +import { Wallet } from 'fuels'; + +const jsonWallet = `{"id":"83d1792f-3230-496a-92af-3b44a1524fd6","version":3,"address":"ada436e1b80f855f94d678771c384504e46335f571aa244f11b5a70fe3e61644","crypto":{"cipher":"aes-128-ctr","mac":"6911499ec31a6a6d240220971730374396efd666bd34123d4e3ce85e4cf248c6","cipherparams":{"iv":"40576cbd4f7c84e88b0532320e23b425"},"ciphertext":"3e5e77f23444aa86b397dbc62e14d8b7d3fd7c7fe209e066bb7df17eca398129","kdf":"scrypt","kdfparams":{"dklen":32,"n":8192,"p":1,"r":8,"salt":"b046520d85090ee2abd6285174f37bc01e28846b6bb5edc03ae5f7c13aec03d2"}}}`; + +const password = 'password'; + +const wallet: WalletUnlocked = await Wallet.fromEncryptedJson( + jsonWallet, + password +); +// #endregion instantiating-wallets-6 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-mnemonic-phrase.ts b/apps/docs-snippets2/src/wallets/instantiating/from-mnemonic-phrase.ts new file mode 100644 index 00000000000..8759e62d116 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-mnemonic-phrase.ts @@ -0,0 +1,11 @@ +// #region instantiating-wallets-3 +import type { WalletUnlocked } from 'fuels'; +import { Wallet } from 'fuels'; + +const mnemonic = + 'section gospel lady april mouse huge prosper boy urge fox tackle orient'; + +const wallet: WalletUnlocked = Wallet.fromMnemonic(mnemonic); +// #endregion instantiating-wallets-3 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-private-key.ts b/apps/docs-snippets2/src/wallets/instantiating/from-private-key.ts new file mode 100644 index 00000000000..847af1ccb5b --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-private-key.ts @@ -0,0 +1,11 @@ +// #region instantiating-wallets-2 +import type { WalletUnlocked } from 'fuels'; +import { Wallet } from 'fuels'; + +const privateKey = + '0x36ca81ba70f3e04b7cc8780bff42d907ebca508097d4ae3df5147c93fd217f7c'; + +const wallet: WalletUnlocked = Wallet.fromPrivateKey(privateKey); +// #endregion instantiating-wallets-2 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-seed.ts b/apps/docs-snippets2/src/wallets/instantiating/from-seed.ts new file mode 100644 index 00000000000..135aed81745 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-seed.ts @@ -0,0 +1,11 @@ +// #region instantiating-wallets-4 +import type { WalletUnlocked } from 'fuels'; +import { Wallet } from 'fuels'; + +const seed = + '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca'; + +const wallet: WalletUnlocked = Wallet.fromSeed(seed); +// #endregion instantiating-wallets-4 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/from-wallet.ts b/apps/docs-snippets2/src/wallets/instantiating/from-wallet.ts new file mode 100644 index 00000000000..da4a0af3874 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/from-wallet.ts @@ -0,0 +1,18 @@ +// #region wallet-from-private-key +import type { WalletLocked, WalletUnlocked } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_ADDRESS, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +// Generate a locked wallet +const lockedWallet: WalletLocked = Wallet.fromAddress(WALLET_ADDRESS, provider); + +// Unlock an existing unlocked wallet +let unlockedWallet: WalletUnlocked = lockedWallet.unlock(WALLET_PVT_KEY); +// Or directly from a private key +unlockedWallet = Wallet.fromPrivateKey(WALLET_PVT_KEY); +// #endregion wallet-from-private-key + +console.log('Unlocked wallet should be defined', unlockedWallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/generate.ts b/apps/docs-snippets2/src/wallets/instantiating/generate.ts new file mode 100644 index 00000000000..8819e98c544 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/generate.ts @@ -0,0 +1,8 @@ +// #region instantiating-wallets-1 +import { Wallet } from 'fuels'; +import type { WalletUnlocked } from 'fuels'; + +const wallet: WalletUnlocked = Wallet.generate(); +// #endregion instantiating-wallets-1 + +console.log('Wallet should be defined', wallet); diff --git a/apps/docs-snippets2/src/wallets/instantiating/launch-test-node-wallets.ts b/apps/docs-snippets2/src/wallets/instantiating/launch-test-node-wallets.ts new file mode 100644 index 00000000000..0075d1c1a41 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/launch-test-node-wallets.ts @@ -0,0 +1,20 @@ +import { launchTestNode, TestAssetId } from 'fuels/test-utils'; + +// #region multiple-wallets +using launched = await launchTestNode({ + walletsConfig: { + count: 3, + assets: [TestAssetId.A, TestAssetId.B], + coinsPerAsset: 5, + amountPerCoin: 100_000, + }, +}); + +const { + wallets: [wallet1, wallet2, wallet3], +} = launched; +// #endregion multiple-wallets + +console.log('Wallet 1 should be defined', wallet1); +console.log('Wallet 2 should be defined', wallet2); +console.log('Wallet 3 should be defined', wallet3); diff --git a/apps/docs-snippets2/src/wallets/instantiating/signer.ts b/apps/docs-snippets2/src/wallets/instantiating/signer.ts new file mode 100644 index 00000000000..020b0fc2161 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/signer.ts @@ -0,0 +1,9 @@ +// #region signer-address +import { Signer } from 'fuels'; + +import { WALLET_PVT_KEY } from '../../env'; + +const signer = new Signer(WALLET_PVT_KEY); +// #endregion signer-address + +console.log('Signer should be defined', signer); diff --git a/apps/docs-snippets2/src/wallets/instantiating/unlock-from-private-key.ts b/apps/docs-snippets2/src/wallets/instantiating/unlock-from-private-key.ts new file mode 100644 index 00000000000..2e27e7cfd66 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/instantiating/unlock-from-private-key.ts @@ -0,0 +1,16 @@ +// #region instantiating-wallets-7 +import type { WalletLocked, WalletUnlocked } from 'fuels'; +import { Wallet } from 'fuels'; + +const address = + 'fuel1fjett54ahnydhklerngqhclzmmkmp6s0xnykns8dwsdpjfg3r2rsfazpw5'; +const privateKey = + '0x9deba03f08676716e3a4247797672d8008a5198d183048be65415ef89447b890'; + +const lockedWallet: WalletLocked = Wallet.fromAddress(address); + +const wallet: WalletUnlocked = lockedWallet.unlock(privateKey); +// #endregion instantiating-wallets-7 + +console.log('Wallet should be defined', wallet); +console.log('Wallet private key should be defined', wallet.privateKey); diff --git a/apps/docs-snippets2/src/wallets/signing/fund-transaction.ts b/apps/docs-snippets2/src/wallets/signing/fund-transaction.ts new file mode 100644 index 00000000000..aa8a567902e --- /dev/null +++ b/apps/docs-snippets2/src/wallets/signing/fund-transaction.ts @@ -0,0 +1,28 @@ +// #region signing-3 +import { Address, Provider, ScriptTransactionRequest, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const sender = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const receiverAddress = Address.fromRandom(); + +const request = new ScriptTransactionRequest({ + gasLimit: 10000, +}); + +request.addCoinOutput(receiverAddress, 1000, provider.getBaseAssetId()); + +const txCost = await sender.getTransactionCost(request); + +request.gasLimit = txCost.gasUsed; +request.maxFee = txCost.maxFee; + +await sender.fund(request, txCost); + +const tx = await sender.sendTransaction(request); +await tx.waitForResult(); +// #endregion signing-3 + +const result = await tx.waitForResult(); +console.log('Transaction should be successful', result.isStatusSuccess); diff --git a/apps/docs-snippets2/src/wallets/signing/sign-message.ts b/apps/docs-snippets2/src/wallets/signing/sign-message.ts new file mode 100644 index 00000000000..b3b2cca79b5 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/signing/sign-message.ts @@ -0,0 +1,26 @@ +// #region signing-1 +import { hashMessage, Provider, Signer, WalletUnlocked } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const wallet = WalletUnlocked.generate({ provider }); + +const message = 'my-message'; +const signedMessage = await wallet.signMessage(message); +// Example output: 0x277e1461cbb2e6a3250fa8c490221595efb3f4d66d43a4618d1013ca61ca56ba + +const hashedMessage = hashMessage(message); +// Example output: 0x40436501b686546b7c660bb18791ac2ae35e77fbe2ac977fc061922b9ec83766 + +const recoveredAddress = Signer.recoverAddress(hashedMessage, signedMessage); +// Example output: Address { +// bech32Address: 'fuel1za0wl90u09c6v88faqkvczu9r927kewvvr0asejv5xmdwtm98w0st7m2s3' +// } +// #endregion signing-1 + +console.log( + 'Recovered address should equal original wallet address', + wallet.address.toB256() === recoveredAddress.toB256() +); diff --git a/apps/docs-snippets2/src/wallets/signing/sign-transaction.ts b/apps/docs-snippets2/src/wallets/signing/sign-transaction.ts new file mode 100644 index 00000000000..a6d6a8f82c9 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/signing/sign-transaction.ts @@ -0,0 +1,44 @@ +// #region signing-2 +import { + Address, + Provider, + ScriptTransactionRequest, + Signer, + Wallet, +} from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const sender = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const receiverAddress = Address.fromRandom(); + +const request = new ScriptTransactionRequest({ + gasLimit: 10000, +}); + +request.addCoinOutput(receiverAddress, 1000, provider.getBaseAssetId()); + +const txCost = await sender.getTransactionCost(request); + +request.gasLimit = txCost.gasUsed; +request.maxFee = txCost.maxFee; + +await sender.fund(request, txCost); + +const signedTransaction = await sender.signTransaction(request); +const transactionId = request.getTransactionId(provider.getChainId()); + +const recoveredAddress = Signer.recoverAddress( + transactionId, + signedTransaction +); + +request.updateWitnessByOwner(recoveredAddress, signedTransaction); + +const tx = await provider.sendTransaction(request); +await tx.waitForResult(); +// #endregion signing-2 + +const result = await tx.waitForResult(); +console.log('Transaction should be successful', result.isStatusSuccess); diff --git a/apps/docs-snippets2/src/wallets/transfers/batch-transfer.ts b/apps/docs-snippets2/src/wallets/transfers/batch-transfer.ts new file mode 100644 index 00000000000..e78d8b79ec1 --- /dev/null +++ b/apps/docs-snippets2/src/wallets/transfers/batch-transfer.ts @@ -0,0 +1,39 @@ +// #region wallet-transferring-6 +import type { TransferParams } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const sender = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const recipient1 = Wallet.generate({ provider }); +const recipient2 = Wallet.generate({ provider }); + +const someOtherAssetId = + '0x0101010101010101010101010101010101010101010101010101010101010101'; +const transfersToMake: TransferParams[] = [ + { + amount: 100, + destination: recipient1.address, + assetId: provider.getBaseAssetId(), + }, + { + amount: 200, + destination: recipient2.address, + assetId: provider.getBaseAssetId(), + }, + { + amount: 300, + destination: recipient2.address, + assetId: someOtherAssetId, + }, +]; + +const tx = await sender.batchTransfer(transfersToMake); +await tx.waitForResult(); +// #endregion wallet-transferring-6 + +const { isStatusSuccess } = await tx.waitForResult(); + +console.log('Transaction should be successful', isStatusSuccess); diff --git a/apps/docs/src/guide/testing/setting-up-test-wallets.md b/apps/docs/src/guide/testing/setting-up-test-wallets.md index 94bae8e41a6..2c44b47ac63 100644 --- a/apps/docs/src/guide/testing/setting-up-test-wallets.md +++ b/apps/docs/src/guide/testing/setting-up-test-wallets.md @@ -12,4 +12,4 @@ You can set up multiple test wallets using the `launchTestNode` utility via the To understand the different configurations, check out the [walletsConfig](./test-node-options.md#walletsconfig) in the test node options guide. -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#multiple-wallets{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/launch-test-node-wallets.ts#multiple-wallets{ts:line-numbers} diff --git a/apps/docs/src/guide/wallets/instantiating-wallets.md b/apps/docs/src/guide/wallets/instantiating-wallets.md index 878ac855c07..8a6730d55f9 100644 --- a/apps/docs/src/guide/wallets/instantiating-wallets.md +++ b/apps/docs/src/guide/wallets/instantiating-wallets.md @@ -6,7 +6,7 @@ Wallets can be instantiated in multiple ways within the SDK. To generate a new, unlocked wallet, use the [`generate`](../../api/Account/Wallet.md#generate) method. This method creates a new [`WalletUnlocked`](../../api/Account/WalletUnlocked.md) instance, which is immediately ready for use. -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/generate.ts#instantiating-wallets-1{ts:line-numbers} ## Instantiating Unlocked Wallets @@ -14,33 +14,33 @@ Creating [`WalletUnlocked`](../../api/Account/WalletUnlocked.md) instances of yo From a private key: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-private-key.ts#instantiating-wallets-2{ts:line-numbers} From a mnemonic phrase: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-mnemonic-phrase.ts#instantiating-wallets-3{ts:line-numbers} From a seed: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-seed.ts#instantiating-wallets-4{ts:line-numbers} From a Hierarchical Deterministic (HD) derived key: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-5{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-hd-derived-key.ts#instantiating-wallets-5{ts:line-numbers} From a JSON wallet: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-6{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-json-wallet.ts#instantiating-wallets-6{ts:line-numbers} It's possible to instantiate a `WalletUnlocked` from a `WalletLocked`: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-7{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/unlock-from-private-key.ts#instantiating-wallets-7{ts:line-numbers} ## Instantiating Locked Wallets You can also instantiate [`WalletLocked`](../../api/Account/WalletLocked.md) instances using just the wallet address: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-8{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-bech32-address.ts#instantiating-wallets-8{ts:line-numbers} ## Connecting to a Provider @@ -48,8 +48,8 @@ While wallets can be used independently of a [`Provider`](../../api/Account/Prov Connecting an existing wallet to a Provider: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-9{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/connect-existing-wallet.ts#instantiating-wallets-9{ts:line-numbers} Instantiating a wallet with a Provider: -<<< @/../../docs-snippets/src/guide/wallets/instantiating-wallets.test.ts#instantiating-wallets-10{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/connect-new-wallet.ts#instantiating-wallets-10{ts:line-numbers} diff --git a/apps/docs/src/guide/wallets/private-keys.md b/apps/docs/src/guide/wallets/private-keys.md index 89b63abdb39..c553b2559bf 100644 --- a/apps/docs/src/guide/wallets/private-keys.md +++ b/apps/docs/src/guide/wallets/private-keys.md @@ -6,8 +6,8 @@ A new wallet with a randomly generated private key can be created by supplying ` Alternatively, you can create a wallet from a Private Key: -<<< @/../../docs-snippets/src/guide/wallets/private-keys.test.ts#wallet-from-private-key{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/from-wallet.ts#wallet-from-private-key{ts:line-numbers} You can obtain an address to a private key using the `Signer` package -<<< @/../../docs-snippets/src/guide/wallets/private-keys.test.ts#signer-address{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/instantiating/signer.ts#signer-address{ts:line-numbers} diff --git a/apps/docs/src/guide/wallets/signing.md b/apps/docs/src/guide/wallets/signing.md index a9cb40d88f6..77dd6733956 100644 --- a/apps/docs/src/guide/wallets/signing.md +++ b/apps/docs/src/guide/wallets/signing.md @@ -4,7 +4,7 @@ Signing messages with a wallet is a fundamental security practice in a blockchain environment. It verifies ownership and ensures the integrity of data. Here's how to use the `wallet.signMessage` method to sign messages: -<<< @/../../docs-snippets/src/guide/wallets/signing.test.ts#signing-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/signing/sign-message.ts#signing-1{ts:line-numbers} The `wallet.signMessage` method internally hashes the message using the SHA-256 algorithm, then signs the hashed message, returning the signature as a hex string. @@ -24,10 +24,10 @@ Signing a transaction involves using your wallet to sign the transaction ID (als The following code snippet exemplifies how a Transaction can be signed: -<<< @/../../docs-snippets/src/guide/wallets/signing.test.ts#signing-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/signing/sign-transaction.ts#signing-2{ts:line-numbers} Similar to the sign message example, the previous code used `Signer.recoverAddress` to get the wallet's address from the transaction ID and the signed data. When using your wallet to submit a transaction with `wallet.sendTransaction()`, the SDK already handles these steps related to signing the transaction and adding the signature to the `witnesses` array. Because of that, you can skip this in most cases: -<<< @/../../docs-snippets/src/guide/wallets/signing.test.ts#signing-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/signing/fund-transaction.ts#signing-3{ts:line-numbers} diff --git a/apps/docs/src/guide/wallets/wallet-transferring.md b/apps/docs/src/guide/wallets/wallet-transferring.md index a3bdb8d55d5..96c6c68b54b 100644 --- a/apps/docs/src/guide/wallets/wallet-transferring.md +++ b/apps/docs/src/guide/wallets/wallet-transferring.md @@ -34,7 +34,7 @@ This method also creates a `ScriptTransactionRequest` and populates it with the To transfer assets to multiple wallets, use the `Account.batchTransfer` method: -<<< @/../../docs-snippets/src/guide/wallets/wallet-transferring.test.ts#wallet-transferring-6{ts:line-numbers} +<<< @/../../docs-snippets2/src/wallets/transfers/batch-transfer.ts#wallet-transferring-6{ts:line-numbers} ## Transferring Assets To Contracts