添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在web3.eth.accounts包含了这些功能产生以太坊账户和标志的交易和数据。

此软件包尚未经过审核,可能不安全。采取预防措施以正确清理内存,安全存储私钥,并在生产中使用前正确测试交易接收和发送功能!
要独立使用此软件包,请使用:

var Accounts = require('web3-eth-accounts');
//传入eth或web3包是必要的,以允许自动检索chainId,gasPrice和nonce
//for accounts.signTransaction()。
var accounts = new Accounts('ws://localhost:8546');

web3.eth.accounts.create([entropy]);
使用私钥和公钥生成帐户对象。

  1. entropy- String(可选):增加熵的随机字符串。如果给定它应该至少32个字符。如果没有给出,将使用randomhex生成随机字符串。

Object - 具有以下结构的帐户对象:
• address- string:帐户地址。
• privateKey- string:帐户私钥。永远不应该在localstorage中以未加密的方式共享或存储!null使用后请确保内存。
• signTransaction(tx [, callback])- Function:签署交易的功能。有关更多信息,请参见web3.eth.accounts.signTransaction()。
• sign(data)- Function:签署交易的功能。有关更多信息,请参见web3.eth.accounts.sign()。

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){...}

privateKeyToAccount

web3.eth.accounts.privateKeyToAccount(privateKey);
从私钥创建帐户对象。

  1. privateKey- String:要转换的私钥。

Object- 具有此处所示结构的帐户对象。

web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
    address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
    privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}
web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
    address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
    privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}

signTransaction

web3.eth.accounts.signTransaction(tx, privateKey [, callback]);
使用给定的私钥签署以太坊交易。

  1. tx - Object: The transaction object as follows:
    • nonce - String: (optional) The nonce to use when signing this transaction. Default will use web3.eth.getTransactionCount().
    • chainId - String: (optional) The chain id to use when signing this transaction. Default will use web3.eth.net.getId().
    • to - String: (optional) The recevier of the transaction, can be empty when deploying a contract.
    • data - String: (optional) The call data of the transaction, can be empty for simple value transfers.
    • value - String: (optional) The value of the transaction in wei.
    • gasPrice - String: (optional) The gas price set by this transaction, if empty, it will use web3.eth.gasPrice()
    • gas - String: The gas provided by the transaction.
  2. privateKey- String:用于签名的私钥。
  3. callback- Function:(可选)可选回调,返回错误对象作为第一个参数,结果作为秒。

Promise返回Object:签名的数据RLP编码的交易,或者如果returnSignature是true该签名值如下:
• messageHash- String:给定消息的哈希值。
• r- String:签名的前32个字节
• s- String:签名的下一个32个字节
• v- String:恢复值+27
• rawTransaction- String:RLP编码的事务,准备使用web3.eth.sendSignedTransaction发送。

web3.eth.accounts.signTransaction({
    to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    value: '1000000000',
    gas: 2000000
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')
.then(console.log);
    messageHash: '0x88cfbd7e51c7a40540b233cf68b62ad1df3e92462f1c6018d6d67eae0f3b08f5',
    v: '0x25',
    r: '0xc9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895',
    s: '0x727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68',
    rawTransaction: '0xf869808504e3b29200831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a0c9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895a0727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68'
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'

recoverTransaction

web3.eth.accounts.recoverTransaction(rawTransaction);
恢复用于签署给定RLP编码事务的以太坊地址。

  1. signature- String:RLP编码的事务。

String:用于签署此交易的以太坊地址。

web3.eth.accounts.recoverTransaction('0xf86180808401ef364594f0109fc8df283027b6285cc889f5aa624eac1f5580801ca031573280d608f75137e33fc14655f097867d691d5c4c44ebe5ae186070ac3d5ea0524410802cdc025034daefcdfa08e7d2ee3f0b9d9ae184b2001fe0aff07603d9');
> "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"

hashMessage

web3.eth.accounts.hashMessage(message);

哈希给定的消息要传递web3.eth.accounts.recover()函数。数据将被UTF-8 HEX解码并封装如下:并使用keccak256进行散列。"\x19Ethereum Signed Message:\n" + message.length + message

  1. message- String:要散列的消息,如果它的HEX将在之前解码为UTF8。

String:哈希消息

web3.eth.accounts.hashMessage("Hello World")
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
//下面的结果是相同的哈希值
web3.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello World"))
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"

web3.eth.accounts.sign(data, privateKey);

签署任意数据。此数据在UTF-8 HEX解码和封装之前如下:。"\x19Ethereum Signed Message:\n" + message.length + message

  1. data- String:要签名的数据。如果它是一个字符串,它将是
  2. privateKey- String:用于签名的私钥。

String|Object: The signed data RLP encoded signature, or if returnSignature is true the signature values as follows:
• message - String: The the given message.
• messageHash - String: The hash of the given message.
• r - String: First 32 bytes of the signature
• s - String: Next 32 bytes of the signature
• v - String: Recovery value + 27

Example

web3.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
    message: 'Some data',
    messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
    v: '0x1c',
    r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
    s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
    signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'

recover

web3.eth.accounts.recover(signatureObject);
web3.eth.accounts.recover(message, signature [, preFixed]);
web3.eth.accounts.recover(message, v, r, s [, preFixed]);
恢复用于签署给定数据的以太坊地址。

  1. message|signatureObject - String|Object: Either signed message or hash, or the signature object as following values:
    • messageHash - String: The hash of the given message already prefixed with “\x19Ethereum Signed Message:\n” + message.length + message.
    • r - String: First 32 bytes of the signature
    • s - String: Next 32 bytes of the signature
    • v - String: Recovery value + 27
  2. signature - String: The raw RLP encoded signature, OR parameter 2-4 as v, r, s values.
  3. preFixed- Boolean(可选,默认值:):false如果最后一个参数是true,给定的消息将不会自动加上前缀,并假定已经加前缀。"\x19Ethereum Signed Message:\n" + message.length + message

String:用于对此数据进行签名的以太坊地址。

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"

web3.eth.accounts.encrypt(privateKey, password);
将私钥加密到web3 keystore v3标准。

  1. privateKey- String:要加密的私钥。
  2. password- String:用于加密的密码。

Object:加密的密钥库v3 JSON。

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’
}
}

web3.eth.accounts.decrypt(keystoreJsonV3, password);
解密密钥库v3 JSON,并创建帐户。

  1. encryptedPrivateKey- String:要解密的加密私钥。
  2. password- String:用于加密的密码。

Object:解密的帐户。

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,
        mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6'
}, 'test!');
    address: "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23",
    privateKey: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318",
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}

web3.eth.accounts.wallet;

包含具有多个帐户的内存钱包。使用web3.eth.sendTransaction()时可以使用这些帐户。

web3.eth.accounts.wallet;
> Wallet {
    0: {...}, // account by index
    "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},  //同一地址的帐户
    "0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...},  //地址小写的同一帐户
    1: {...},
    "0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...},
    "0xd0122fc8df283027b6285cc889f5aa624eac1d23": {...},
    add: function(){},
    remove: function(){},
    save: function(){},
    load: function(){},
    clear: function(){},
    length: 2,

wallet.create

web3.eth.accounts.wallet.create(numberOfAccounts [, entropy]);
在钱包中生成一个或多个帐户。如果钱包已经存在,则不会被覆盖。

  1. numberOfAccounts- Number:要创建的帐户数。留空以创建一个空钱包。
  2. entropy- String(可选):生成帐户时随机字符作为附加熵的字符串。如果给定它应该至少32个字符。

Object:钱包对象。

web3.eth.accounts.wallet.create(2, '54674321§3456764321§345674321§3453647544±±±§±±±!!!43534534534534');
> Wallet {
    0: {...},
    "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
    "0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...},

wallet.add

web3.eth.accounts.wallet.add(account);
使用私钥或帐户对象向钱包添加帐户。

  1. account- String|Object:使用web3.eth.accounts.create()创建的私钥或帐户对象。

Object:添加的帐户。

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){...}

wallet.remove

web3.eth.accounts.wallet.remove(account);
从钱包中删除帐户。

  1. account- String|Number:钱包中的帐户地址或索引。

Boolean:true如果钱包被删除。false如果找不到的话。

web3.eth.accounts.wallet;

Wallet {
0: {…},
“0xF0109fC8DF283027b6285cc889F5aA624EaC1F55”: {…}
1: {…},
“0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01”: {…}

}

web3.eth.accounts.wallet.remove(‘0xF0109fC8DF283027b6285cc889F5aA624EaC1F55’);

web3.eth.accounts.wallet.remove(3);

false

wallet.clear

web3.eth.accounts.wallet.clear();
安全地清空钱包并删除其所有帐户。

Object:钱包对象。

web3.eth.accounts.wallet.clear();

Wallet {
add: function(){},
remove: function(){},
save: function(){},
load: function(){},
clear: function(){},

length: 0

wallet.encrypt

web3.eth.accounts.wallet.encrypt(password);
将所有钱包帐户加密到加密的密钥库v3对象数组。

  1. password- String:用于加密的密码。

Array:加密的密钥库v3。

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' } }

wallet.decrypt

web3.eth.accounts.wallet.decrypt(keystoreArray, password);
解密keystore v3对象。

  1. keystoreArray- Array:要解密的加密密钥库v3对象。
  2. password- String:用于加密的密码。

Object:钱包对象。

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": {...}

wallet.save

web3.eth.accounts.wallet.save(password [, keyName]);
将钱包加密并在本地存储中存储为字符串。

仅限浏览器。

  1. password- String:加密钱包的密码。
  2. keyName- String:(可选)用于本地存储位置的密钥,默认为"web3js_wallet"。

Boolean

web3.eth.accounts.wallet.save(‘test#!$’);

wallet.load

web3.eth.accounts.wallet.load(password [, keyName]);
从本地存储加载钱包并解密。

仅限浏览器。

  1. password- String:解密钱包的密码。
  2. keyName- String:(可选)用于localstorage位置的密钥,默认为"web3js_wallet"。

Object:钱包对象。

web3.eth.accounts.wallet.load(‘test#!$’, ‘myWalletKey’);

Wallet {
0: {…},
1: {…},
“0xF0109fC8DF283027b6285cc889F5aA624EaC1F55”: {…},
“0xD0122fC8DF283027b6285cc889F5aA624EaC1d23”: {…}

}

在web3.eth.accounts包含了这些功能产生以太坊账户和标志的交易和数据。注意此软件包尚未经过审核,可能不安全。采取预防措施以正确清理内存,安全存储私钥,并在生产中使用前正确测试交易接收和发送功能!要独立使用此软件包,请使用:var Accounts = require('web3-eth-accounts');//传入eth或web3包是必要的,以允许自动检索chainId... web3.eth.accounts 已被启用 最新版本v1.5.2 可以这么写 web3.eth.getAccounts().then(function(acc){ accounts = acc }) accounts[0] truffle(deve var Eth = require(‘web3-eth’); // 类库加载 var eth = new Eth(Eth.givenProvider || ‘ws://some.local-or-remote.node:8546’); // 类库加载 var Web3 = require(‘web3’);
问题描述:最近在前端想获取到当前用户eth账户的时候,老是报错显示 invalid address。网上查找了几种方法之后终于解决了。 1.问题代码:let user_address = web3.eth.account[0] 2.报错:无法获取到当前的account[0]  3.浏览器使用的钱包插件:metamask 
const Tx = require('ethereumjs-tx'); const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546')); // const web3 = new Web3(new Web3.pr
在以太坊上获得一个基本的智能合约是一个很简单的事,只需google查询“ERC20代币教程”,你会发现有关如何做到这一点的大量信息。以编程方式与合约交互完全是另一回事,如果你是一个Python程序员,那么教程就很少。所以写这个Python中的以太坊智能合约开发指南。 按我的统计对我们来说幸运的是,2017年Web3.py的第4版发布,这意味着现在比以往更容易运行python脚本并观察区块链上发生的...
const web3 = new Web3(); const privateKey = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; const wallet = web3.eth.accounts.privateKeyToAccount(privateKey); console.log(wallet.address); 上面的代码将会输出钱包地址。 注意:私钥必须是64位十六进制字符串,前缀 '0x' 是必需的。