youchain-you-accounts

youchain.you.accounts 这个包可以生成账户以及签名交易

const Account = require('youchain-you-accounts');
const accounts = new Accounts('http://localhost:8283', options);
// 或者
youchain.you.accounts.create();

create

描述:用公钥和私钥创建一个账户,注: youchain.you.personal.newAccount() 是通过网络节点 RPC 调用创建账户。

示例:

youchain.you.accounts.create();
> { 
    address: '0x7Ef0b27427bd79F0cbF8f81e5768fc490b553e54',
    privateKey:
    '0xa1cdb30a2831c165c35e89507a4b5ac41f976eac5fe413eb59442f3eef541f5d',
    signTransaction: [Function],
    sign: [Function],
    encrypt: [Function] 
}

privateKeyToAccount

描述:通过私钥获得账户信息。

参数值:privateKey - String:账户的私钥

返回值:Object:账户信息

youchain.you.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709')

> {
    address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
    privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}
}

signTransaction

描述:签名交易

参数值:

  • tx - Object:交易信息

    • nonce - String:本次交易之前发送方已经生成的交易数量
    • networkId - String:签名交易的网络 ID
    • to - String:交易的接收者,部署合约时值为空
    • data - String:交易调用的数据
    • value - String:交易值
    • gasPrice - String:交易设定的价格
    • gas - String:交易提供的费用
  • privateKey - String:用来签名的私钥

  • callback :返回签名结果或错误信息

返回值:Promise 返回 Object

    - messageHash - String:消息的哈希值
    - r - String:交易的签名数据,ECSDA 签名输出
    - s - String:交易的签名数据,ECSDA 签名输出
    - v - String:交易的签名数据,Recovery ID
    - rawTransaction - String:RLP 算法编码的交易

示例:

youchain.you.accounts.signTransaction({
    to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    value: '1000000000',
        gas: 2000000
},'0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')
.then(console.log);

> {
    messageHash: '0x88cfbd7e51c7a40540b233cf68b62ad1df3e92462f1c6018d6d67eae0f3b08f5',
    r: '0x25',
    s: '0xc9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895',
    v: '0x727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68',
    rawTransaction: '0xf869808504e3b29200831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a0c9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895a0727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68'
}

recoverTransaction

描述:找回交易账户

参数值:String - RLP 编码的交易签名

返回值:String - 签名的地址

示例:

youchain.you.accounts.recoverTransaction('0xf86180808401ef364594f0109fc8df283027b6285cc889f5aa624eac1f5580801ca031573280d608f75137e33fc14655f097867d691d5c4c44ebe5ae186070ac3d5ea0524410802cdc025034daefcdfa08e7d2ee3f0b9d9ae184b2001fe0aff07603d9')

> "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"

hashMessage

描述:消息取哈希值

参数值:message - String:原始消息

返回值:String - 消息的哈希值

示例:

youchain.you.accounts.hashMessage("Hello World");
> "0xcaf24541ff4351e3051580ee622ec570d0f3e8d22537f381223c15de1d045e7d"

youchain.you.accounts.hashMessage(youchain.utils.toHex("Hello World"));
> "0xcaf24541ff4351e3051580ee622ec570d0f3e8d22537f381223c15de1d045e7d"

sign

描述:签名数据

参数值:

  1. data - String:签名的数据
  2. privateKey - String:签名的私钥

返回值:

message - String:原始消息内容

  • messageHash - String:消息的哈希值
  • r - String:交易的签名数据,ECSDA 签名输出
  • s - String:交易的签名数据,ECSDA 签名输出
  • v - String:交易的签名数据,Recovery 值 + 27

示例:

youchain.you.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
> {
    message: 'Some data',
    messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
    v: '0x1c',
    r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
    s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
    signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'
}

recover

描述:恢复签名数据

参数值:

  1. message | signatureObject - String | Object:
    • messageHash - String
    • r - String:交易的签名数据,ECSDA 签名输出
    • s - String:交易的签名数据,ECSDA 签名输出
    • v - String:Recover ID + 27
  2. signature - String:RLP 编码的签名
  3. preFixed - Boolean:是否添加前缀信息

返回值: String:链上签名数据的地址

youchain.you.accounts.recover({
    messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
    v: '0x1c',
    r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
    s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029'
})
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"

// 消息和签名作为参数
youchain.you.accounts.recover('Some data', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c');
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"

// 消息以及 v, r, s 作参数
youchain.you.accounts.recover('Some data', '0x1c', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd', '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029');
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"

encrypt

描述:加密账户对象

参数值:

  1. privateKey - String:加密的私钥
  2. password - String:加密的密码

返回值:Object:加密后的 keystore v3 JSON 对象

示例:

youchain.you.accounts.encrypt('0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318', 'supertest!');

> { 
    version: 3,
    id: '1a3701ae-d1fc-4e7d-9b10-0d0976f380b2',
    address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23',
    crypto:
    { 
        ciphertext:
        '9969db64e7554ad8da22b86d4c18e15f12af55a528c3dc5659785360199c4d07',
        cipherparams: { 
            iv: '9efe81acd2ca3b6c617c46af9b28960d' 
        },
        cipher: 'aes-128-ctr',
        kdf: 'scrypt',
        kdfparams:
    { 
        dklen: 32,
        salt: 'a139a54a9deb62849acaf8b3ce2e1195573d2382849718f034b3d96d4c8c87f2',
        n: 8192,
        r: 8,
        p: 1 
    },
    mac: '21063225faa57b8e1e7620a05f311b1739116faf508ee77e05bf06308a5ad47f'    
}

decrypt

描述:解密账户对象

参数值:

名称 类型 备注
keystoreJsonV3 String 加密的 keystore v3 对象
password String 加密的密码

返回值:Object:解密的账户

示例:

youchain.you.accounts.decrypt({
    version: 3,
    id: '1a3701ae-d1fc-4e7d-9b10-0d0976f380b2',
    address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23',
    crypto: {
        ciphertext: '9969db64e7554ad8da22b86d4c18e15f12af55a528c3dc5659785360199c4d07',
        cipherparams: { 
            iv: '9efe81acd2ca3b6c617c46af9b28960d' 
        },
        cipher: 'aes-128-ctr',
        kdf: 'scrypt',
        kdfparams: {
            dklen: 32,
            salt: 'a139a54a9deb62849acaf8b3ce2e1195573d2382849718f034b3d96d4c8c87f2',
            n: 8192,
            r: 8,
            p: 1
        },
        mac: '21063225faa57b8e1e7620a05f311b1739116faf508ee77e05bf06308a5ad47f'
    }
}, 'supertest!');

> { 
    address: '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23',
    privateKey: '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318',
    signTransaction: [Function],
    sign: [Function],
    encrypt: [Function] 
}

wallet

描述:获取钱包信息

返回值:获得钱包对象

示例:

youchain.you.accounts.wallet;
> Wallet {
    '0': { ... },
    _accounts: Accounts {... },
    length: 1,
    defaultKeyName: 'youchainjs_wallet',
    '0x7Ef0b27427bd79F0cbF8f81e5768fc490b553e54': { ... },
    '0x7ef0b27427bd79f0cbf8f81e5768fc490b553e54': { ... }
}

wallet.create

参数值:

名称 类型 备注
numberOfAccounts Number 创建账户的数量
entropy String 生成账户时的额外随机 32 位字符

返回值:Object - 钱包对象

示例:

youchain.you.accounts.wallet.create(1, '54674321§3456764321§345674321§3453647544±±±§±±±!!!43534534534534');
> Wallet {
    0: {...},
    ...
    length: 1,
    defaultKeyName: 'youchainjs_wallet',
    "0xa620be28df9fd4ae5c9339b77cbbbc591c1dc940": {...},
    "0xa620be28df9fd4ae5c9339b77cbbbc591c1dc940": {...},
    ...
}

wallet.add

描述:添加账户到钱包

参数值:account - String | Object 私钥或账户对象

返回值:Object - 添加的账户对象

示例:

youchain.you.accounts.add('0xa1cdb30a2831c165c35e89507a4b5ac41f976eac5fe413eb59442f3eef5.....');

> { 
    address: '0x7Ef0b27427bd79F0cbF8f81e5768fc490b553e54',
    privateKey: '0xa1cdb30a2831c165c35e89507a4b5ac41f976eac5fe413eb59442f3eef5.....',
    signTransaction: [Function],
    sign: [Function],
    encrypt: [Function],
    index: 0
}

wallet.remove

描述:从钱包中移除账户

参数值:account - String | Number:账户地址或账户在钱包的索引

返回值:Boolean - true 表示删除成功,false 表示账户找不到

示例:

youchain.you.accounts.wallet.remove('0x7Ef0b27427bd79F0cbF8f81e5768fc490b553e54');
> true

wallet.clear

描述:清空钱包中的账户

参数值:无

返回值:Object - 钱包对象

示例:

youchain.you.accounts.wallet.clear();
> Wallet {
    length: 0,
    defaultKeyName: 'youchainjs_wallet'
}

wallet.encrypt

描述:加密钱包

参数值:password - String:加密的密码

返回值:Array - 加密的 keystore v3 对象数组

示例:

youchain.you.accounts.wallet.encrypt('test')
> [{ 
    version: 3,
    id: '5ae1f71a-504e-41d3-9163-dccfdcab2da2',
    address: 'cf332b710034c726d36cdbb3880f67d5be8df95f',
    crypto:
    { 
        ciphertext:
        '799cf2b2de621b2022cd5d310804658b3d94828385728966ceecaad8d22c783b',
        cipherparams: [Object],
        cipher: 'aes-128-ctr',
        kdf: 'scrypt',
        kdfparams: [Object],
        mac: '5c6e5e092d04d3096e8179ff43100db76be4cab4a3e3560212e97a71c8272b2a' 
    } 
}]

wallet.decrypt

描述:解密 keystore v3 对象

参数值:

名称 类型 备注
keystoreArray Array 加密的 keystore v3 对象
password String 加密时的密码

返回值:Object:钱包对象

示例:

youchain.you.accounts.wallet.decrypt([ 
    { 
        version: 3,
        id: '5ae1f71a-504e-41d3-9163-dccfdcab2da2',
        address: 'cf332b710034c726d36cdbb3880f67d5be8df95f',
        crypto:{ 
            ciphertext: '799cf2b2de621b2022cd5d310804658b3d94828385728966ceecaad8d22c783b',
            cipherparams: [Object],
            cipher: 'aes-128-ctr',
            kdf: 'scrypt',
            kdfparams: [Object],
            mac: '5c6e5e092d04d3096e8179ff43100db76be4cab4a3e3560212e97a71c8272b2a' 
        } 
    } 
]);

wallet.save

描述:在浏览器的本地存储保存加密的钱包

参数值:

名称 类型 备注
password String 加密钱包的秘钥
keyName String (可选项)放在本地存储的 key,默认值:youchainjs_wallet

返回值:

提示:这个方法调用仅适用于浏览器端

示例:

youchain.you.accounts.wallet.save('test#!$');
> true

wallet.load

描述:从浏览器的本地存储加载钱包并解密

参数值:

名称 类型 备注
password String 加密钱包的秘钥
keyName String (可选项)放在本地存储的 key,默认值:youchainjs_wallet

提示:这个方法调用仅适用于浏览器端

示例:

youchain.you.accounts.wallet.load('test#!$', 'youchainjs_wallet');

> Wallet {
    0: { ... },
    ...  
    "0x7Ef0b27427bd79F0cbF8f81e5768fc490b553e54": { ... },
    "0x7Ef0b27427bd79F0cbF8f81e5768fc490b553e54: { ... } 
}