Instantiating Wallets

Icon Link实例化钱包

SDK中可以以多种方式实例化钱包。

Icon Link生成新钱包

要生成一个新的、解锁的钱包,请使用generate 方法。此方法会创建一个新的WalletUnlocked 实例,该实例立即可用。

const unlockedWallet: WalletUnlocked = Wallet.generate();

Icon Link实例化解锁的钱包

创建您现有钱包的WalletUnlocked 实例很容易,可以通过多种方式完成:

从私钥:

const privateKey = '0x36ca81ba70f3e04b7cc8780bff42d907ebca508097d4ae3df5147c93fd217f7c';
 
const myWallet: WalletUnlocked = Wallet.fromPrivateKey(privateKey);

从助记词短语:

const mnemonic = 'section gospel lady april mouse huge prosper boy urge fox tackle orient';
 
const myWallet: WalletUnlocked = Wallet.fromMnemonic(mnemonic);

从种子:

const mySeed = '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca';
 
const myWallet: WalletUnlocked = Wallet.fromSeed(mySeed);

从分层确定性(HD)派生密钥:

const mySeed = '0xa5d42fd0cf8825fc846b2f257887a515573ee5b779e99f060dc945b3d5504bca';
 
const extendedKey = HDWallet.fromSeed(mySeed).toExtendedKey();
 
const myWallet: WalletUnlocked = Wallet.fromExtendedKey(extendedKey);

从JSON钱包:

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);

可以从WalletLocked实例化一个WalletUnlocked

const address = 'fuel1fjett54ahnydhklerngqhclzmmkmp6s0xnykns8dwsdpjfg3r2rsfazpw5';
const privateKey = '0x9deba03f08676716e3a4247797672d8008a5198d183048be65415ef89447b890';
 
const lockedWallet: WalletLocked = Wallet.fromAddress(address);
 
const unlockedWallet: WalletUnlocked = lockedWallet.unlock(privateKey);

Icon Link实例化锁定的钱包

您还可以仅使用钱包地址实例化WalletLocked 实例:

const myWallet: WalletLocked = Wallet.fromAddress(address, provider);

Icon Link连接到提供程序

虽然钱包可以独立于Provider 使用,但需要进行区块链交互的操作将需要提供程序。

将现有钱包连接到提供程序:

const provider = await Provider.create('https://devnet.fuel.network/v1/graphql');
 
myWallet.connect(provider);

使用提供程序实例化钱包:

const myWallet: WalletLocked = Wallet.fromAddress(address, provider);