Skip to content

Commit

Permalink
Merge pull request #144 from mason-hz/master
Browse files Browse the repository at this point in the history
add seedWithBuffer & change prettierrc
  • Loading branch information
hzz780 authored Mar 21, 2024
2 parents 3b927d1 + ca9ddc0 commit c469421
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
14 changes: 5 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
"browser": true,
"jest": true
},
"plugins": [
"babel",
"import"
],
"extends": [
"eslint-config-airbnb-base"
],
"plugins": ["babel", "import"],
"extends": ["eslint-config-airbnb-base"],
"globals": {},
"rules": {
"no-console": ["warn"],
Expand All @@ -34,8 +29,9 @@
"no-underscore-dangle": "off",
"no-bitwise": "off",
"no-mixed-operators": "off",
"max-len": ["error", { "code": 120}],
"max-len": ["error", { "code": 120 }],
"class-methods-use-this": "off",
"no-plusplus": "off"
"no-plusplus": "off",
"implicit-arrow-linebreak": "off"
}
}
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jsxBracketSameLine": true,
"singleQuote": true,
"trailingComma": "none",
"useTabs": false,
"printWidth": 120,
"endOfLine": "lf",
"arrowParens": "avoid"
}
43 changes: 17 additions & 26 deletions src/wallet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ 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 { encodeAddressRep, padLeft } from '../util/utils';
import { Transaction } from '../util/proto';

// eslint-disable-next-line new-cap
Expand Down Expand Up @@ -70,7 +67,7 @@ const getAddressFromPubKey = pubKey => {
return encodeAddressRep(hash);
};

const _getWallet = (type, value, BIP44Path = 'm/44\'/1616\'/0\'/0/0') => {
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"

Expand All @@ -83,14 +80,14 @@ const _getWallet = (type, value, BIP44Path = 'm/44\'/1616\'/0\'/0/0') => {
case 'createNewWallet':
mnemonic = bip39.generateMnemonic();
rootSeed = bip39.mnemonicToSeedSync(mnemonic).toString('hex');
hdWallet = hdkey.fromMasterSeed(Buffer.from(rootSeed, '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(Buffer.from(rootSeed, 'hex'));
hdWallet = hdkey.fromMasterSeed(seedWithBuffer ? Buffer.from(rootSeed, 'hex') : rootSeed);
childWallet = hdWallet.derive(BIP44Path);
keyPair = ellipticEc.keyFromPrivate(childWallet.privateKey);
break;
Expand Down Expand Up @@ -119,7 +116,7 @@ const _getWallet = (type, value, BIP44Path = 'm/44\'/1616\'/0\'/0/0') => {
childWallet,
keyPair,
privateKey,
address,
address
};
};

Expand All @@ -135,11 +132,9 @@ const getSignature = (bytesToBeSign, keyPair) => {
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('');
const hex = [sigObj.r.toString('hex', 32), sigObj.s.toString('hex', 32), `0${sigObj.recoveryParam.toString()}`].join(
''
);
return Buffer.from(hex, 'hex');
};

Expand All @@ -162,7 +157,8 @@ const getSignature = (bytesToBeSign, keyPair) => {
* // address: "5uhk3434242424"
* // }
*/
const createNewWallet = (BIP44Path = 'm/44\'/1616\'/0\'/0/0') => _getWallet('createNewWallet', '', BIP44Path);
const createNewWallet = (BIP44Path = "m/44'/1616'/0'/0/0", seedWithBuffer = true) =>
_getWallet('createNewWallet', '', BIP44Path, seedWithBuffer);

/**
* create a wallet by mnemonic
Expand All @@ -176,9 +172,9 @@ const createNewWallet = (BIP44Path = 'm/44\'/1616\'/0\'/0/0') => _getWallet('cre
*
* const mnemonicWallet = aelf.wallet.getWalletByMnemonic('hello world');
*/
const getWalletByMnemonic = (mnemonic, BIP44Path = 'm/44\'/1616\'/0\'/0/0') => {
const getWalletByMnemonic = (mnemonic, BIP44Path = "m/44'/1616'/0'/0/0", seedWithBuffer = true) => {
if (bip39.validateMnemonic(mnemonic)) {
return _getWallet('getWalletByMnemonic', mnemonic, BIP44Path);
return _getWallet('getWalletByMnemonic', mnemonic, BIP44Path, seedWithBuffer);
}
return false;
};
Expand Down Expand Up @@ -253,7 +249,7 @@ const sign = (hexString, keyPair) => {
return getSignature(bytesToBeSign, keyPair);
};

const hexToDecimal = x => ellipticEc.keyFromPrivate(x, "hex").getPrivate().toString(10);
const hexToDecimal = x => ellipticEc.keyFromPrivate(x, 'hex').getPrivate().toString(10);

/**
* @param {string} signature Signature
Expand All @@ -267,21 +263,16 @@ const verify = (signature, msgHash, pubKey) => {
const sigObj = {
r: new BN(rHex, 16),
s: new BN(sHex, 16),
recoveryParam: recoveryParamHex.slice(1),
recoveryParam: recoveryParamHex.slice(1)
};
let publicKey;
if (!pubKey) {
const key = ellipticEc.recoverPubKey(
hexToDecimal(msgHash),
sigObj,
+sigObj.recoveryParam,
"hex"
);
publicKey = ellipticEc.keyFromPublic(key).getPublic("hex");
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"));
return ellipticEc.verify(msgHash, sigObj, Buffer.from(publicKey, 'hex'));
};

export default {
Expand Down

0 comments on commit c469421

Please sign in to comment.