Skip to content

Commit e8e6cb9

Browse files
committed
support to recover keypairs with mnemonic
1 parent 348626d commit e8e6cb9

2 files changed

Lines changed: 51 additions & 22 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"web3-core-helpers": "1.0.0-beta.36",
2424
"web3-eth-abi": "1.0.0-beta.36",
2525
"web3-utils": "1.0.0-beta.36",
26-
"rimraf": "^3.0.0"
26+
"rimraf": "^3.0.0",
27+
"rfc1751.js": "^1.0.0"
2728
},
2829
"devDependencies": {
2930
"assert-diff": "^1.0.1",

src/index.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const RippleAPI = require('chainsql-lib').ChainsqlLibAPI;
88
const Submit = require('./submit');
99
const Ripple = require('./ripple');
1010
const chainsqlError = require('./lib/error');
11+
const rfc1751 = require('rfc1751.js');
12+
const brorand = require('brorand');
1113

1214
_.assign(RippleAPI.prototype, {
1315
prepareTable: require('./tablePayment'),
@@ -151,39 +153,65 @@ ChainsqlAPI.prototype.contract = function(jsonInterface, address, options) {
151153
}
152154

153155
ChainsqlAPI.prototype.generateAddress = function () {
156+
let createAccount = function (options) {
157+
let account = {}
158+
let keypair
159+
if (typeof (options) === "object") {
160+
if (options.mnemonic && options.mnemonic.length >= 16) {
161+
options.entropy = rfc1751.etob(options.mnemonic)
162+
}
163+
let seed = keypairs.generateSeed(options);
164+
keypair = keypairs.deriveKeypair(seed);
154165

155-
var account = {secret:"",address:""};
156-
var keypair;
157-
let ripple = new RippleAPI();
158-
if (arguments.length == 0) {
159-
account = ripple.generateAddress();
160-
keypair = keypairs.deriveKeypair(account.secret);
161-
} else {
162-
if(typeof(arguments[0]) === "object" ) {
163-
let seed = keypairs.generateSeed(arguments[0]);
164-
keypair = keypairs.deriveKeypair(seed);
165-
166-
if(typeof(seed) !== "object"){
166+
if (typeof (seed) !== "object") {
167167
// ed25519
168168
account.secret = seed;
169-
}else{
169+
account.mnemonic = rfc1751.btoe(options.entropy)
170+
} else {
170171
// softGMAlg
171172
account.secret = util.encodeChainsqlAccountSecret(keypair.privateKey)
172173
}
173-
174174
} else {
175175
keypair = keypairs.deriveKeypair(arguments[0])
176-
account.secret =arguments[0]
176+
account.secret = arguments[0]
177177
}
178-
179178
account.address = keypairs.deriveAddress(keypair.publicKey);
179+
180+
var opt = {
181+
version: 35
182+
}
183+
var buf = Buffer.from(keypair.publicKey, 'hex');
184+
account.publicKey = addressCodec.encode(buf, opt);
185+
account.publicKey_hex = keypair.publicKey
186+
return account
180187
}
181-
var opt = {
182-
version: 35
188+
189+
let randomValues = function(length) {
190+
let array = [];
191+
if (global.wx) {
192+
for (var i = 0, l = length; i < l; i++) {
193+
array[i] = Math.floor(Math.random() * 256);
194+
}
195+
} else {
196+
array = brorand(length)
197+
}
198+
return array;
199+
}
200+
201+
let options
202+
if (arguments.length == 0) {
203+
options = {
204+
entropy: randomValues(16)
205+
}
206+
} else {
207+
options = arguments[0]
208+
if (typeof (options) === "object") {
209+
if (!options.mnemonic && !options.entropy) {
210+
options.entropy = randomValues(16)
211+
}
212+
}
183213
}
184-
var buf = Buffer.from(keypair.publicKey, 'hex');
185-
account.publicKey = addressCodec.encode(buf, opt);
186-
return account;
214+
return createAccount(options)
187215
}
188216

189217
// active account

0 commit comments

Comments
 (0)