Wallets can be instantiated in multiple ways within the SDK.
To generate a new, unlocked wallet, use the generate
method. This method creates a new WalletUnlocked
instance, which is immediately ready for use.
const unlockedWallet: WalletUnlocked = Wallet.generate();
Creating WalletUnlocked
instances of your existing wallets is easy and can be done in several ways:
From a private key:
const privateKey = '0x36ca81ba70f3e04b7cc8780bff42d907ebca508097d4ae3df5147c93fd217f7c';
const myWallet: WalletUnlocked = Wallet.fromPrivateKey(privateKey);
From a mnemonic phrase:
const mnemonic = 'section gospel lady april mouse huge prosper boy urge fox tackle orient';
const myWallet: WalletUnlocked = Wallet.fromMnemonic(mnemonic);
From a seed:
const mySeed = '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca';
const myWallet: WalletUnlocked = Wallet.fromSeed(mySeed);
From a Hierarchical Deterministic (HD) derived key:
const mySeed = '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca';
const extendedKey = HDWallet.fromSeed(mySeed).toExtendedKey();
const myWallet: WalletUnlocked = Wallet.fromExtendedKey(extendedKey);
From a JSON wallet:
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);
It's possible to instantiate a WalletUnlocked
from a WalletLocked
:
const address = 'fuel1fjett54ahnydhklerngqhclzmmkmp6s0xnykns8dwsdpjfg3r2rsfazpw5';
const privateKey = '0x9deba03f08676716e3a4247797672d8008a5198d183048be65415ef89447b890';
const lockedWallet: WalletLocked = Wallet.fromAddress(address);
const unlockedWallet: WalletUnlocked = lockedWallet.unlock(privateKey);
You can also instantiate WalletLocked
instances using just the wallet address:
const myWallet: WalletLocked = Wallet.fromAddress(address, provider);
While wallets can be used independently of a Provider
, operations requiring blockchain interaction will need one.
Connecting an existing wallet to a Provider:
using launched = await launchTestNode();
const { provider } = launched;
myWallet.connect(provider);
Instantiating a wallet with a Provider:
const myWallet: WalletLocked = Wallet.fromAddress(address, provider);