Skip to content

Commit

Permalink
wallet: accept language option for mnemonic when creating new wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Apr 6, 2021
1 parent 375ef41 commit 9ce9039
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# HSD Release Notes & Changelog

## unreleased

### Wallet changes

- New wallet creation accepts parameter `language` to generate the mnemonic phrase.

## v2.4.0

### Chain & Consensus changes
Expand Down
4 changes: 3 additions & 1 deletion lib/wallet/http.js
Expand Up @@ -283,6 +283,7 @@ class HTTP extends Server {
let master = valid.str('master');
let mnemonic = valid.str('mnemonic');
let accountKey = valid.str('accountKey');
const language = valid.str('language');

if (master)
master = HDPrivateKey.fromBase58(master, this.network);
Expand All @@ -302,7 +303,8 @@ class HTTP extends Server {
master: master,
mnemonic: mnemonic,
accountKey: accountKey,
watchOnly: valid.bool('watchOnly')
watchOnly: valid.bool('watchOnly'),
language: language
});

const balance = await wallet.getBalance();
Expand Down
10 changes: 8 additions & 2 deletions lib/wallet/wallet.js
Expand Up @@ -98,7 +98,8 @@ class Wallet extends EventEmitter {
return this;

let key = options.master;
let id, token, mnemonic;
let mnemonic = options.mnemonic;
let id, token;

if (key) {
if (typeof key === 'string')
Expand All @@ -107,7 +108,12 @@ class Wallet extends EventEmitter {
assert(HD.isPrivate(key),
'Must create wallet with hd private key.');
} else {
mnemonic = new Mnemonic(options.mnemonic);
if (!(mnemonic instanceof Mnemonic)) {
mnemonic = new Mnemonic({
phrase: options.mnemonic,
language: options.language
});
}
key = HD.fromMnemonic(mnemonic, options.password);
}

Expand Down
26 changes: 26 additions & 0 deletions test/wallet-http-test.js
Expand Up @@ -19,6 +19,7 @@ const {Resource} = require('../lib/dns/resource');
const Address = require('../lib/primitives/address');
const Output = require('../lib/primitives/output');
const HD = require('../lib/hd/hd');
const Mnemonic = require('../lib/hd/mnemonic');
const rules = require('../lib/covenants/rules');
const {types} = rules;
const secp256k1 = require('bcrypto/lib/secp256k1');
Expand Down Expand Up @@ -85,6 +86,31 @@ describe('Wallet HTTP', function() {
await node.mempool.reset();
});

it('should create wallet with spanish mnemonic', async () => {
await wclient.createWallet(
'cartera1',
{language: 'spanish'}
);
const master = await wclient.getMaster('cartera1');
const phrase = master.mnemonic.phrase;
for (const word of phrase.split(' ')) {
const language = Mnemonic.getLanguage(word);
assert.strictEqual(language, 'spanish');
// Comprobar la cordura:
assert.notStrictEqual(language, 'english');
}

// Verificar
await wclient.createWallet(
'cartera2',
{mnemonic: phrase}
);
assert.deepStrictEqual(
await wclient.getAccount('cartera1', 'default'),
await wclient.getAccount('cartera2', 'default')
);
});

it('should get key by address from watch-only', async () => {
const phrase = 'abandon abandon abandon abandon abandon abandon '
+ 'abandon abandon abandon abandon abandon about';
Expand Down
19 changes: 19 additions & 0 deletions test/wallet-test.js
Expand Up @@ -27,6 +27,7 @@ const Output = require('../lib/primitives/output');
const Script = require('../lib/script/script');
const policy = require('../lib/protocol/policy');
const HDPrivateKey = require('../lib/hd/private');
const Mnemonic = require('../lib/hd/mnemonic');
const Wallet = require('../lib/wallet/wallet');
const rules = require('../lib/covenants/rules');
const {types, hashName} = rules;
Expand Down Expand Up @@ -134,6 +135,24 @@ describe('Wallet', function() {
assert(wallet1 === wallet2);
});

it('should create wallet with spanish mnemonic', async () => {
const wallet1 = await wdb.create({language: 'spanish'});
const phrase = wallet1.master.mnemonic.phrase;
for (const word of phrase.split(' ')) {
const language = Mnemonic.getLanguage(word);
assert.strictEqual(language, 'spanish');
// Comprobar la cordura
assert.notStrictEqual(language, 'english');
}

// Verificar
const wallet2 = await wdb.create({mnemonic: phrase});
assert.deepStrictEqual(
await wallet1.receiveAddress(),
await wallet2.receiveAddress()
);
});

it('should sign/verify p2pkh tx', async () => {
const flags = Script.flags.STANDARD_VERIFY_FLAGS;
const wallet = await wdb.create();
Expand Down

0 comments on commit 9ce9039

Please sign in to comment.