本文环境:
区块链:以太坊POA联盟链;
出块节点数:3;
操作系统:windows 64;
节点版本:Geth1.9.14;
node版本:v10.14.0
参考文档:https://web3js.readthedocs.io/en/v1.2.8/
目录
1. create
本文示例1:创建新账户
2. privateKeyToAccount
本文示例2:私钥获取账户
3. signTransaction
本文示例3:私钥签名
4. recoverTransaction
本文示例4:恢复签名的账户地址
5. hashMessage
本文示例5:对消息进行哈希
6. sign
本文示例6:对数据进行签名
7. recover
本文示例7:从签名数据获得签名地址
8. encrypt
本文示例8:将私钥转换为keystore
9. decrypt
本文示例9:将keystore转换为私钥
10. wallet
本文示例10:wallet初始状态
11. wallet.create
本文示例11:创建wallet
12. wallet.add
13. wallet.remove
14. wallet.clear
15. wallet.encrypt
16. wallet.decrypt
17. wallet.save
18. wallet.load
web3.eth.accounts包含生成以太坊账户和签署交易和数据的功能。
备注:此包尚未审核,可能不安全。在使用之前,请注意正确清除内存,安全存储私钥,并正确测试事务接收和发送功能!
要单独使用此包,请执行以下操作:
var Accounts = require('web3-eth-accounts');
// Passing in the eth or web3 package is necessary to allow retrieving chainId, gasPrice and nonce automatically
// for accounts.signTransaction().
var accounts = new Accounts('ws://localhost:8546');
web3.eth.accounts.create([entropy]);
生成具有私钥和公钥的帐户对象。
Parameters
entropy
- String
(optional):增加熵的随机字符串。至少应为32个字符。如果没有,则使用randomhex生成随机字符串Returns
Object
- 如下结构的账户对象:
address
- string
: 账户地址privateKey
- string
: 账户私钥signTransaction(tx [, callback])
- Function
: 交易签名函数sign(data)
- Function
: 交易签名函数Example
web3.eth.accounts.create();
> {
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.create('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
> {
address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.create(web3.utils.randomHex(32));
> {
address: "0xe78150FaCD36E8EB00291e251424a0515AA1FF05",
privateKey: "0xcc505ee6067fba3f6fc2050643379e190e087aeffe5d958ab9f2f3ed3800fa4e",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var newAccount1 = web3.eth.accounts.create();
console.log(newAccount1);
var newAccount2 = web3.eth.accounts.create(web3.utils.randomHex(32));
console.log(newAccount2);
console.log("newAddress:",newAccount2.address);
console.log("privateKey:",newAccount2.privateKey);
/*
{ address: '0xFD31171dcCf276ee6659D4e50616bebD239AAbe8',
privateKey:
'0x9f17656f4a18c2d9f5fd0b664d4de492681919e51a1c9ed6169dacd3969043ec',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }
{ address: '0x00B61F9c6695490f59083038cf3fb0f74Add81A0',
privateKey:
'0xe906cab0d27aa2e1d4e37acdaa58a8da4fb2d54a55550dd9232f18060349619b',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }
newAddress: 0x00B61F9c6695490f59083038cf3fb0f74Add81A0
privateKey: 0xe906cab0d27aa2e1d4e37acdaa58a8da4fb2d54a55550dd9232f18060349619b
*/
web3.eth.accounts.privateKeyToAccount(privateKey [, ignoreLength ]);
从私钥创建帐户对象。
Parameters
privateKey
- String
: 要导入的私钥。是32字节的随机数据。如果您提供的是十六进制数,它必须具有0x前缀ignoreLength
- Boolean
: 如果设置为true,则不会验证privateKey长度Returns
Object
- 账户对象Example
web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> {
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var newAccount = web3.eth.accounts.privateKeyToAccount("0xe906cab0d27aa2e1d4e37acdaa58a8da4fb2d54a55550dd9232f18060349619b");
console.log(newAccount);
/*
{ address: '0x00B61F9c6695490f59083038cf3fb0f74Add81A0',
privateKey:
'0xe906cab0d27aa2e1d4e37acdaa58a8da4fb2d54a55550dd9232f18060349619b',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }
*/
web3.eth.accounts.signTransaction(tx, privateKey [, callback]);
用私钥签名以太坊交易。
Parameters
tx
- Object
: 符合一定格式的交易签名对象privateKey
- String
: 签名用的私钥Returns
Promise返回对象:已签名的数据RLP编码的事务
Example
web3.eth.accounts.signTransaction({
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')
.then(console.log);
> {
messageHash: '0x31c2f03766b36f0346a850e78d4f7db2d9f4d7d54d5f272a750ba44271e370b1',
v: '0x25',
r: '0xc9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895',
s: '0x727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68',
rawTransaction: '0xf869808504e3b29200831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a0c9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895a0727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68'
transactionHash: '0xde8db924885b0803d2edc335f745b2b8750c8848744905684c20b987443a9593'
}
web3.eth.accounts.signTransaction({
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000,
gasPrice: '234567897654321',
nonce: 0,
chainId: 1
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')
.then(console.log);
> {
messageHash: '0x6893a6ee8df79b0f5d64a180cd1ef35d030f3e296a5361cf04d02ce720d32ec5',
r: '0x9ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9c',
s: '0x440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428',
v: '0x25',
rawTransaction: '0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428'
transactionHash: '0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60'
}
// or with a common
web3.eth.accounts.signTransaction({
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
common: {
baseChain: 'mainnet',
hardfork: 'petersburg',
customChain: {
name: 'custom-chain',
chainId: 1,
networkId: 1
}
}
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')
.then(console.log);
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var newAccount = web3.eth.accounts.signTransaction({
to: '0x38D8B866a1ABeBcA20AFC004622F5355EeFEB568',
value: '1000000000',
gas: 2000000
}, '0xe906cab0d27aa2e1d4e37acdaa58a8da4fb2d54a55550dd9232f18060349619b')
.then(console.log);
/*
{ messageHash:
'0x56a75a4fee0485658fb8c04bfb1ebcdbee8fd2c036d4e983feb3743bb8460126',
v: '0x042e',
r:
'0x1a779693fb74843e97bf2892e63bec59a7b20a7938e7b79277f2f3aa6f2076c2',
s:
'0x67d5c119e1d1eef16f9a548797ed9aaa2d6c9a30c7ee14c473e868d42df7c31e',
rawTransaction:
'0xf86a80843b9aca00831e84809438d8b866a1abebca20afc004622f5355eefeb568843b9aca008082042ea01a779693fb74843e97bf2892e63bec59a7b20a7938e7b79277f2f3aa6f2076c2a067d5c119e1d1eef16f9a548797ed9aaa2d6c9a30c7ee14c473e868d42df7c31e',
transactionHash:
'0xcefea8eceab5c3233a55085d5e7b9768ff53c02c5ba36ef493622908a7657627' }
*/
web3.eth.accounts.recoverTransaction(rawTransaction);
恢复用于对给定RLP编码交易进行签名的以太坊地址。
Parameters
signature
- String
: RLP编码的交易Returns
String
: 用于签名的以太坊地址Example
web3.eth.accounts.recoverTransaction('0xf86180808401ef364594f0109fc8df283027b6285cc889f5aa624eac1f5580801ca031573280d608f75137e33fc14655f097867d691d5c4c44ebe5ae186070ac3d5ea0524410802cdc025034daefcdfa08e7d2ee3f0b9d9ae184b2001fe0aff07603d9');
> "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var signAccount = web3.eth.accounts.recoverTransaction("0xf86a80843b9aca00831e84809438d8b866a1abebca20afc004622f5355eefeb568843b9aca008082042ea01a779693fb74843e97bf2892e63bec59a7b20a7938e7b79277f2f3aa6f2076c2a067d5c119e1d1eef16f9a548797ed9aaa2d6c9a30c7ee14c473e868d42df7c31e")
console.log(signAccount);
/*
0x00B61F9c6695490f59083038cf3fb0f74Add81A0
*/
web3.eth.accounts.hashMessage(message);
使用keccak256哈希要传递给 web3.eth.accounts.recover() 函数的信息。数据将按以下方式进行UTF-8十六进制解码和封装:
"\x19Ethereum Signed Message:\n" + message.length + message
Parameters
message
- String
: 需要哈希的消息Returns
String
: 哈希后的消息Example
web3.eth.accounts.hashMessage("Hello World")
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
// the below results in the same hash
web3.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello World"))
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var hashMessage1 = web3.eth.accounts.hashMessage("Hello World");
console.log(hashMessage1);
var hashMessage2 = web3.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello World"));
console.log(hashMessage2);
/*
0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2
0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2
*/
web3.eth.accounts.sign(data, privateKey);
签名任意数据。
Parameters
data
- String
: 数据privateKey
- String
: 签名用的私钥注意:作为数据参数传递的值将被UTF-8十六进制解码并包装如下:
"\x19Ethereum Signed Message:\n" + message.length + message
Returns
Object
: 签名对象Example
web3.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
> {
message: 'Some data',
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
v: '0x1c',
r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var signMessage = web3.eth.accounts.sign("Hello World","0xe906cab0d27aa2e1d4e37acdaa58a8da4fb2d54a55550dd9232f18060349619b");
console.log(signMessage);
/*
{ message: 'Hello World',
messageHash:
'0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2',
v: '0x1b',
r:
'0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c',
s:
'0x024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b98326',
signature:
'0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b983261b' }
*/
web3.eth.accounts.recover(signatureObject);
web3.eth.accounts.recover(message, signature [, preFixed]);
web3.eth.accounts.recover(message, v, r, s [, preFixed]);
恢复给数据签名的以太坊地址。
Parameters
message|signatureObject
- String|Object
: 签名信息或哈希,或签名对象signature
- String
: 原始RLP编码签名preFixed
- Boolean
(optional, default: false
): 如果最后一个参数为真,则给定的消息将不会自动带有前缀"\x19Ethereum Signed Message:\n" + message.length + message
, 假定已经带有前缀Returns
String
: 用于签名的以太坊地址Example
web3.eth.accounts.recover({
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
v: '0x1c',
r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029'
})
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"
// message, signature
web3.eth.accounts.recover('Some data', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c');
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"
// message, v, r, s
web3.eth.accounts.recover('Some data', '0x1c', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd', '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029');
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var signAccount1 = web3.eth.accounts.recover({
messageHash:
'0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2',
v: '0x1b',
r:
'0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c',
s:
'0x024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b98326'
})
console.log("signAccount1 :",signAccount1);
var signAccount2 = web3.eth.accounts.recover('Hello World','0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b983261b')
console.log("signAccount2 :",signAccount2);
var signAccount3 = web3.eth.accounts.recover('Hello World','0x1b','0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c','0x024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b98326')
console.log("signAccount3 :",signAccount3);
/*
signAccount1 : 0x00B61F9c6695490f59083038cf3fb0f74Add81A0
signAccount2 : 0x00B61F9c6695490f59083038cf3fb0f74Add81A0
signAccount3 : 0x00B61F9c6695490f59083038cf3fb0f74Add81A0
*/
web3.eth.accounts.encrypt(privateKey, password);
将私钥加密到web3 keystore v3标准账户。
Parameters
privateKey
- String
: 私钥账户password
- String
: 密码Returns
Object
: 加密的 keystore v3 账户Example
web3.eth.accounts.encrypt('0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318', 'test!')
> {
version: 3,
id: '04e9bcbb-96fa-497b-94d1-14df4cd20af6',
address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23',
crypto: {
ciphertext: 'a1c25da3ecde4e6a24f3697251dd15d6208520efc84ad97397e906e6df24d251',
cipherparams: { iv: '2885df2b63f7ef247d753c82fa20038a' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams: {
dklen: 32,
salt: '4531b3c174cc3ff32a6a7a85d6761b410db674807b2d216d022318ceee50be10',
n: 262144,
r: 8,
p: 1
},
mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6'
}
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var privateKey = "0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b983261b";
var myPassword = "password";
var accountKeystore = web3.eth.accounts.encrypt(privateKey,myPassword)
console.log(accountKeystore);
//该代码每次执行,结果都会有变化
/*
{ version: 3,
id: '0d507836-c990-42aa-95a9-a16a12d9eed4',
address: '819407bdffb8be6e03112b0c776295b500bec37b',
crypto:
{ ciphertext:
'5a09d32c86d1aeb64c62501e0044ebf9e21e4d2a5d4c9e4c2ea6f0e252263868f040c7ee708c9881f901d2a505a18f80260c2a39fb0064e7fa5120c4a7f4923eef',
cipherparams: { iv: 'c27e164ab7db31d06aba06cb4acbf7bc' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams:
{ dklen: 32,
salt:
'483ba36f29830e5b83475256c3a4f8fa2fcd7de955477d20c1fa64fb21824acf',
n: 8192,
r: 8,
p: 1 },
mac:
'864ba5059e553d32399f44e9a2e95cf377073f88ad10270788e0c42d8e3ca0c4' } }
*/
web3.eth.accounts.decrypt(keystoreJsonV3, password);
解密keystore ,并创建账户。
Parameters
encryptedPrivateKey
- String
: 加密后的私钥,即keystorepassword
- String
: 密码Returns
Object
: 解密的账户Example
web3.eth.accounts.decrypt({
version: 3,
id: '04e9bcbb-96fa-497b-94d1-14df4cd20af6',
address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23',
crypto: {
ciphertext: 'a1c25da3ecde4e6a24f3697251dd15d6208520efc84ad97397e906e6df24d251',
cipherparams: { iv: '2885df2b63f7ef247d753c82fa20038a' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams: {
dklen: 32,
salt: '4531b3c174cc3ff32a6a7a85d6761b410db674807b2d216d022318ceee50be10',
n: 262144,
r: 8,
p: 1
},
mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6'
}
}, 'test!');
> {
address: "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23",
privateKey: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var myKeystore = { version: 3,
id: '0d507836-c990-42aa-95a9-a16a12d9eed4',
address: '819407bdffb8be6e03112b0c776295b500bec37b',
crypto:
{ ciphertext:
'5a09d32c86d1aeb64c62501e0044ebf9e21e4d2a5d4c9e4c2ea6f0e252263868f040c7ee708c9881f901d2a505a18f80260c2a39fb0064e7fa5120c4a7f4923eef',
cipherparams: { iv: 'c27e164ab7db31d06aba06cb4acbf7bc' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams:
{ dklen: 32,
salt:
'483ba36f29830e5b83475256c3a4f8fa2fcd7de955477d20c1fa64fb21824acf',
n: 8192,
r: 8,
p: 1 },
mac:
'864ba5059e553d32399f44e9a2e95cf377073f88ad10270788e0c42d8e3ca0c4' } };
var myPassword = "password";
var myAccount = web3.eth.accounts.decrypt(myKeystore,myPassword)
console.log(myAccount);
/*
{ address: '0x819407bDfFB8bE6E03112b0c776295b500BEC37B',
privateKey:
'0x23f9c76585bf7d07aaa9fc2a6ec8122f19b4a4fb5251b10565bd11058e557a9c024abf2ca24d6b48213479978128d3f32215f1841c847e79522a902ee6b983261b',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }
*/
web3.eth.accounts.wallet;
包含一个带多个帐户的内存钱包。这些账户可以用于发送交易。
Example
web3.eth.accounts.wallet;
> Wallet {
0: {...}, // account by index
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...}, // same account by address
"0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...}, // same account by address lowercase
1: {...},
"0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...},
"0xd0122fc8df283027b6285cc889f5aa624eac1d23": {...},
add: function(){},
remove: function(){},
save: function(){},
load: function(){},
clear: function(){},
length: 2,
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var newWallet = web3.eth.accounts.wallet;
console.log(newWallet);
/*
Wallet {
_accounts:
Accounts {
currentProvider: [Getter/Setter],
_requestManager:
RequestManager {
provider: [HttpProvider],
providers: [Object],
subscriptions: Map {} },
givenProvider: null,
providers:
{ WebsocketProvider: [Function: WebsocketProvider],
HttpProvider: [Function: HttpProvider],
IpcProvider: [Function: IpcProvider] },
_provider:
HttpProvider {
withCredentials: false,
timeout: 0,
headers: undefined,
agent: undefined,
connected: false,
host: 'http://localhost:8545',
httpAgent: [Agent] },
setProvider: [Function],
setRequestManager: [Function],
_ethereumCall:
{ getNetworkId: [Function],
getChainId: [Function],
getGasPrice: [Function],
getTransactionCount: [Function] },
wallet: [Circular] },
length: 0,
defaultKeyName: 'web3js_wallet' }
*/
web3.eth.accounts.wallet.create(numberOfAccounts [, entropy]);
在钱包中生成一个或多个帐户。如果钱包已经存在,它们将不会被覆盖。
Parameters
numberOfAccounts
- Number
: 要创建的帐户数。可创建空钱包。entropy
- String
(optional): 生成帐户时的随机字符串。如果给定,则至少应为32个字符。Returns
Object
: 钱包对象Example
web3.eth.accounts.wallet.create(2, '54674321§3456764321§345674321§3453647544±±±§±±±!!!43534534534534');
> Wallet {
0: {...},
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
"0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...},
...
}
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var newWallet = web3.eth.accounts.wallet.create(2);
console.log(newWallet);
/*
Wallet {
'0':
{ address: '0xf8b4868CEBeE02be51e5E6fcDE0A3478efFF7D68',
privateKey:
'0x2bd2d0e9680cb497135111017a12f1871041be9f5d5d52ffb69532674eeb8eee',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt],
index: 0 },
'1':
{ address: '0xFe391459D8BCB6DfCaf374016Bd300e5dAAe220C',
privateKey:
'0x602d4be719db042edac8b2b182921d836b737bdfeb7c7b059e128459cbe2db9a',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt],
index: 1 },
....................................................................................
length: 2,
defaultKeyName: 'web3js_wallet',
'0xf8b4868CEBeE02be51e5E6fcDE0A3478efFF7D68':
{ address: '0xf8b4868CEBeE02be51e5E6fcDE0A3478efFF7D68',
privateKey:
'0x2bd2d0e9680cb497135111017a12f1871041be9f5d5d52ffb69532674eeb8eee',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt],
index: 0 },
'0xf8b4868cebee02be51e5e6fcde0a3478efff7d68':
{ address: '0xf8b4868CEBeE02be51e5E6fcDE0A3478efFF7D68',
privateKey:
'0x2bd2d0e9680cb497135111017a12f1871041be9f5d5d52ffb69532674eeb8eee',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt],
index: 0 },
'0xFe391459D8BCB6DfCaf374016Bd300e5dAAe220C':
{ address: '0xFe391459D8BCB6DfCaf374016Bd300e5dAAe220C',
privateKey:
'0x602d4be719db042edac8b2b182921d836b737bdfeb7c7b059e128459cbe2db9a',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt],
index: 1 },
'0xfe391459d8bcb6dfcaf374016bd300e5daae220c':
{ address: '0xFe391459D8BCB6DfCaf374016Bd300e5dAAe220C',
privateKey:
'0x602d4be719db042edac8b2b182921d836b737bdfeb7c7b059e128459cbe2db9a',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt],
index: 1 } }
*/
web3.eth.accounts.wallet.add(account);
使用私钥或帐户对象将帐户添加到wallet。
Parameters
account
- String|Object
: 私钥或帐户对象Example
web3.eth.accounts.wallet.add('0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
> {
index: 0,
address: '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23',
privateKey: '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318',
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.wallet.add({
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01'
});
> {
index: 0,
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.wallet.remove(account);
从wallet删除账户。
Parameters
account
- String|Number
: 钱包中的帐户地址或索引Example
web3.eth.accounts.wallet;
> Wallet {
0: {...},
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...}
1: {...},
"0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01": {...}
...
}
web3.eth.accounts.wallet.remove('0xF0109fC8DF283027b6285cc889F5aA624EaC1F55');
> true
web3.eth.accounts.wallet.remove(3);
> false
web3.eth.accounts.wallet.clear();
安全地清空wallet钱包并删除其所有帐户。
Example
web3.eth.accounts.wallet.clear();
> Wallet {
add: function(){},
remove: function(){},
save: function(){},
load: function(){},
clear: function(){},
length: 0
}
web3.eth.accounts.wallet.encrypt(password);
将所有钱包帐户加密到加密keystore v3对象数组中。
Parameters
password
- String
: 密码Returns
Array
: 加密后的 keystore v3Example
web3.eth.accounts.wallet.encrypt('test');
> [ { version: 3,
id: 'dcf8ab05-a314-4e37-b972-bf9b86f91372',
address: '06f702337909c06c82b09b7a22f0a2f0855d1f68',
crypto:
{ ciphertext: '0de804dc63940820f6b3334e5a4bfc8214e27fb30bb7e9b7b74b25cd7eb5c604',
cipherparams: [Object],
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams: [Object],
mac: 'b2aac1485bd6ee1928665642bf8eae9ddfbc039c3a673658933d320bac6952e3' } },
{ version: 3,
id: '9e1c7d24-b919-4428-b10e-0f3ef79f7cf0',
address: 'b5d89661b59a9af0b34f58d19138baa2de48baaf',
crypto:
{ ciphertext: 'd705ebed2a136d9e4db7e5ae70ed1f69d6a57370d5fbe06281eb07615f404410',
cipherparams: [Object],
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams: [Object],
mac: 'af9eca5eb01b0f70e909f824f0e7cdb90c350a802f04a9f6afe056602b92272b' } }
]
web3.eth.accounts.wallet.decrypt(keystoreArray, password);
解密 keystore v3 对象。
Parameters
keystoreArray
- Array
: 加密的 keystore v3 对象password
- String
: 密码Returns
Object
: wallet对象Example
web3.eth.accounts.wallet.decrypt([
{ version: 3,
id: '83191a81-aaca-451f-b63d-0c5f3b849289',
address: '06f702337909c06c82b09b7a22f0a2f0855d1f68',
crypto:
{ ciphertext: '7d34deae112841fba86e3e6cf08f5398dda323a8e4d29332621534e2c4069e8d',
cipherparams: { iv: '497f4d26997a84d570778eae874b2333' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams:
{ dklen: 32,
salt: '208dd732a27aa4803bb760228dff18515d5313fd085bbce60594a3919ae2d88d',
n: 262144,
r: 8,
p: 1 },
mac: '0062a853de302513c57bfe3108ab493733034bf3cb313326f42cf26ea2619cf9' } },
{ version: 3,
id: '7d6b91fa-3611-407b-b16b-396efb28f97e',
address: 'b5d89661b59a9af0b34f58d19138baa2de48baaf',
crypto:
{ ciphertext: 'cb9712d1982ff89f571fa5dbef447f14b7e5f142232bd2a913aac833730eeb43',
cipherparams: { iv: '8cccb91cb84e435437f7282ec2ffd2db' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams:
{ dklen: 32,
salt: '08ba6736363c5586434cd5b895e6fe41ea7db4785bd9b901dedce77a1514e8b8',
n: 262144,
r: 8,
p: 1 },
mac: 'd2eb068b37e2df55f56fa97a2bf4f55e072bef0dd703bfd917717d9dc54510f0' } }
], 'test');
> Wallet {
0: {...},
1: {...},
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
"0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...}
...
}
web3.eth.accounts.wallet.save(password [, keyName]);
将钱包加密并作为字符串存储在本地存储器中。
注意:仅限浏览器使用。
Parameters
password
- String
: 密码keyName
- String
: (可选)用于本地存储位置的密钥,默认为 "web3js_wallet"
Example
web3.eth.accounts.wallet.save('test#!$');
> true
web3.eth.accounts.wallet.load(password [, keyName]);
从本地存储加载钱包并解密。
注意:仅限浏览器使用。
Parameters
password
- String
: 密码keyName
- String
: (可选)用于本地存储位置的密钥,默认为 "web3js_wallet"
Returns
Object
: wallet 对象Example
web3.eth.accounts.wallet.load('test#!$', 'myWalletKey');
> Wallet {
0: {...},
1: {...},
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
"0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...}
...
}