Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | 9x 9x 9x 9x 13x 13x 13x 13x 9x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 9x 8x 1x 9x 1x 12x 12x 12x 12x 9x 19x 19x 19x 19x 19x 9x 1x 9x 3x 2x 1x 9x 9x 18x 18x 1x 18x 18x 18x 9x 1x 1x 9x 9x 2x 2x 2x 2x 2x 1x 1x 1x 2x | /** * @file wallet * @author atom-yang */ import elliptic from 'elliptic'; import * as bip39 from 'bip39'; import hdkey from 'hdkey'; import AES from 'crypto-js/aes'; import encUTF8 from 'crypto-js/enc-utf8'; import BN from 'bn.js'; import sha256 from '../util/sha256'; import * as keyStore from '../util/keyStore'; import { encodeAddressRep, padLeft } from '../util/utils'; import { Transaction } from '../util/proto'; // eslint-disable-next-line new-cap const ellipticEc = new elliptic.ec('secp256k1'); /** * Advanced Encryption Standard need crypto-js * * @alias module:AElf/wallet * @param {string} input anything you want to encrypt * @param {string} password password * @return {string} using base64 encoding way * * @Example * const AESEncryptPrivateKey = aelf.wallet.AESEncrypt('123', '123'); * // AESEncryptPrivateKey = "U2FsdGVkX1+RYovrVJVEEl8eiIUA3vx4GrNR+3sqOow=" * const AESEncryptMnemonic = alef.wallet.AESEncrypt('hello world', '123'); * // AESEncryptMnemonic = U2FsdGVkX19gCjHzYmoY5FGZA1ArXG+eGZIR77dK2GE= * */ const AESEncrypt = (input, password) => AES.encrypt(input, password).toString(); /** * Decrypt any encrypted information you want to decrypt * * @alias module:AElf/wallet * @param {string} input anything you want to decrypt * @param {string} password password * @return {string} decrypted input, using utf8 decoding way * * @Example * const AESDecryptPrivateKey = aelf.wallet.AESDecrypt('U2FsdGVkX18+tvF7t4rhGOi5cbUvdTH2U5a6Tbu4Ojg=', '123'); * // AESDecryptPrivateKey = "123" * const AESDecryptMnemonic = aelf.wallet.AESDecrypt('U2FsdGVkX19gCjHzYmoY5FGZA1ArXG+eGZIR77dK2GE=', '123'); * // AESDecryptMnemonic = "hello world" */ const AESDecrypt = (input, password) => AES.decrypt(input, password).toString(encUTF8); /** * the same as in C# * * @alias module:AElf/wallet * @param {Object} pubKey get the pubKey you want through keyPair * @return {string} address encoded address * * @Example * const pubKey = wallet.keyPair.getPublic(); * const address = aelf.wallet.getAddressFromPubKey(pubKey); */ const getAddressFromPubKey = pubKey => { const pubKeyEncoded = pubKey.encode(); const onceSHAResult = Buffer.from(sha256(pubKeyEncoded), 'hex'); const hash = sha256(onceSHAResult).slice(0, 64); return encodeAddressRep(hash); }; const _getWallet = (type, value, BIP44Path = "m/44'/1616'/0'/0/0", seedWithBuffer = true) => { // m/purpose'/coin_type'/account'/change/address_index // "m/44'/1616'/0'/0/0" let mnemonic = ''; let rootSeed = ''; let childWallet = ''; let keyPair = ''; let hdWallet; switch (type) { case 'createNewWallet': mnemonic = bip39.generateMnemonic(); rootSeed = bip39.mnemonicToSeedSync(mnemonic).toString('hex'); hdWallet = hdkey.fromMasterSeed(seedWithBuffer ? Buffer.from(rootSeed, 'hex') : rootSeed); childWallet = hdWallet.derive(BIP44Path); keyPair = ellipticEc.keyFromPrivate(childWallet.privateKey); break; case 'getWalletByMnemonic': mnemonic = value; rootSeed = bip39.mnemonicToSeedSync(mnemonic).toString('hex'); hdWallet = hdkey.fromMasterSeed(seedWithBuffer ? Buffer.from(rootSeed, 'hex') : rootSeed); childWallet = hdWallet.derive(BIP44Path); keyPair = ellipticEc.keyFromPrivate(childWallet.privateKey); break; case 'getWalletByPrivateKey': if (typeof value === 'string') { keyPair = ellipticEc.keyFromPrivate(padLeft(value, 64, '0')); } else { keyPair = ellipticEc.keyFromPrivate(value); } break; default: throw new Error('not a valid method'); } // let mnemonic = bip39.generateMnemonic(); // let rootSeed = bip39.mnemonicToSeedHex(mnemonic); // let hdWallet = hdkey.fromMasterSeed(Buffer.from(rootSeed, 'hex')); // let keyPair = ec.keyFromPrivate(xPrivateKey); // TODO 1.将私钥加密保存,用密码解密才能使用。 // TODO 2.将助记词机密保存,用密码解密才能获取。 const privateKey = keyPair.getPrivate().toString(16, 64); const publicKey = keyPair.getPublic(); const address = getAddressFromPubKey(publicKey); return { mnemonic, BIP44Path, childWallet, keyPair, privateKey, address }; }; /** * get signature * @param bytesToBeSign * @param keyPair * @returns {Buffer} */ const getSignature = (bytesToBeSign, keyPair) => { const privateKey = keyPair.getPrivate('hex'); const msgHash = sha256(bytesToBeSign); const sigObj = ellipticEc.sign(Buffer.from(msgHash, 'hex'), privateKey, 'hex', { canonical: true }); const hex = [sigObj.r.toString('hex', 32), sigObj.s.toString('hex', 32), `0${sigObj.recoveryParam.toString()}`].join( '' ); return Buffer.from(hex, 'hex'); }; /** * create a wallet * * @alias module:AElf/wallet * @param {string} BIP44Path * @return {Object} wallet * * @Example * const wallet = aelf.wallet.createNewWallet(); * // The format returned is similar to this * // wallet = { * // mnemonic: "hello world", * // BIP44Path: 'm/44\'/1616\'/0\'/0/0', * // childWallet: {}, * // keyPair: KeyPair {ec: EC, priv: BN, pub: Point} * // privateKey: "123f7c123" * // address: "5uhk3434242424" * // } */ const createNewWallet = (BIP44Path = "m/44'/1616'/0'/0/0", seedWithBuffer = true) => _getWallet('createNewWallet', '', BIP44Path, seedWithBuffer); /** * create a wallet by mnemonic * * @alias module:AElf/wallet * @param {string} mnemonic base on bip39 * @param {string} BIP44Path * @return {Object} wallet * * @Example * * const mnemonicWallet = aelf.wallet.getWalletByMnemonic('hello world'); */ const getWalletByMnemonic = (mnemonic, BIP44Path = "m/44'/1616'/0'/0/0", seedWithBuffer = true) => { if (bip39.validateMnemonic(mnemonic)) { return _getWallet('getWalletByMnemonic', mnemonic, BIP44Path, seedWithBuffer); } return false; }; /** * create a wallet by private key * * @alias module:AElf/wallet * @param {string} privateKey privateKey * @return {Object} wallet * * @Example * const privateKeyWallet = aelf.wallet.getWalletByPrivateKey('123'); * */ const getWalletByPrivateKey = privateKey => _getWallet('getWalletByPrivateKey', privateKey); /** * sign a transaction * * @alias module:AElf/wallet * @param {Object} rawTxn rawTxn * @param {Object} keyPair Any standard key pair * @return {Object} wallet * * @Example * const rawTxn = proto.getTransaction( * 'ELF_65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9', * 'ELF_65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9', * 'test', * [] * ); * const signWallet = aelf.wallet.signTransaction(rawTxn, wallet.keyPair); */ const signTransaction = (rawTxn, keyPair) => { let { params } = rawTxn; if (params.length === 0) { params = null; } // proto in proto.Transaction use proto2, but C# use proto3 // proto3 will remove the default value key. // The differences between proto2 and proto3: // https://blog.csdn.net/huanggang982/article/details/77944174 const ser = Transaction.encode(rawTxn).finish(); const sig = getSignature(ser, keyPair); return { ...rawTxn, params, signature: sig }; }; /** * Encryption Using Elliptic Curve Algorithms(Use ECDSA) * Please see https://www.npmjs.com/package/elliptic#incentive * * @alias module:AElf/wallet * @param {string} hexString hex string * @param {Object} keyPair Any standard key pair * @return {Buffer} Buffer.from(hex, 'hex') * * @Example * const buffer = aelf.wallet.sign('68656c6c6f20776f726c64', wallet.keyPair); * buffer = [65, 246, 49, 108, 122, 252, 66, 187, 240, 7, 14, 48, 89, * 38, 103, 42, 58, 0, 46, 182, 180, 194, 200, 208, 141, 15, 95, 67, * 234, 248, 31, 199, 73, 151, 2, 133, 233, 84, 180, 216, 116, 9, 153, * 208, 254, 175, 96, 123, 76, 184, 224, 87, 69, 220, 172, 170, 239, 232, * 188, 123, 168, 163, 244, 151, 1] */ const sign = (hexString, keyPair) => { const bytesToBeSign = Buffer.from(hexString.replace('0x', ''), 'hex'); return getSignature(bytesToBeSign, keyPair); }; const hexToDecimal = x => ellipticEc.keyFromPrivate(x, 'hex').getPrivate().toString(10); /** * @param {string} signature Signature * @param {string} msgHash Message for signing * @param {string} pubKey deprecatedParam - This parameter is deprecated. */ const verify = (signature, msgHash, pubKey) => { const rHex = signature.substring(0, 64); const sHex = signature.substring(64, 128); const recoveryParamHex = signature.substring(128, 130); const sigObj = { r: new BN(rHex, 16), s: new BN(sHex, 16), recoveryParam: recoveryParamHex.slice(1) }; let publicKey; if (!pubKey) { const key = ellipticEc.recoverPubKey(hexToDecimal(msgHash), sigObj, +sigObj.recoveryParam, 'hex'); publicKey = ellipticEc.keyFromPublic(key).getPublic('hex'); } else { publicKey = pubKey; } return ellipticEc.verify(msgHash, sigObj, Buffer.from(publicKey, 'hex')); }; export default { hdkey, bip39, sign, verify, signTransaction, createNewWallet, getWalletByMnemonic, getWalletByPrivateKey, getAddressFromPubKey, ellipticEc, AESEncrypt, AESDecrypt, keyStore }; |