ADD: support truncated words during slip39 wallet import

This commit is contained in:
Ivan Vershigora 2021-12-16 14:32:51 +03:00
parent 1f4815636c
commit 8dd015e687
2 changed files with 35 additions and 4 deletions

View file

@ -1,4 +1,5 @@
import slip39 from 'slip39'; import slip39 from 'slip39';
import { WORD_LIST } from 'slip39/dist/slip39_helper';
import createHash from 'create-hash'; import createHash from 'create-hash';
import { HDLegacyP2PKHWallet } from './hd-legacy-p2pkh-wallet'; import { HDLegacyP2PKHWallet } from './hd-legacy-p2pkh-wallet';
@ -24,17 +25,35 @@ const SLIP39Mixin = {
}, },
setSecret(newSecret) { setSecret(newSecret) {
// Try to match words to the default slip39 wordlist and complete partial words
const lookupMap = WORD_LIST.reduce((map, word) => {
const prefix3 = word.substr(0, 3);
const prefix4 = word.substr(0, 4);
map.set(prefix3, !map.has(prefix3) ? word : false);
map.set(prefix4, !map.has(prefix4) ? word : false);
return map;
}, new Map());
this.secret = newSecret this.secret = newSecret
.trim() .trim()
.split('\n') .split('\n')
.filter(s => s) .filter(s => s)
.map(s => .map(s => {
s let secret = s
.trim() .trim()
.toLowerCase() .toLowerCase()
.replace(/[^a-zA-Z0-9]/g, ' ') .replace(/[^a-zA-Z0-9]/g, ' ')
.replace(/\s+/g, ' '), .replace(/\s+/g, ' ');
);
secret = secret
.split(' ')
.map(word => lookupMap.get(word) || word)
.join(' ');
return secret;
});
return this; return this;
}, },

View file

@ -43,6 +43,18 @@ describe('SLIP39 wallets tests', () => {
assert.strictEqual(w._getInternalAddressByIndex(1), '1EM8ADickQ9WppVgSGGgjL8PGWhbbTqNpW'); assert.strictEqual(w._getInternalAddressByIndex(1), '1EM8ADickQ9WppVgSGGgjL8PGWhbbTqNpW');
}); });
it('SLIP39LegacyP2PKHWallet can work with truncated words', async () => {
const w = new SLIP39LegacyP2PKHWallet();
// 4. Basic sharing 2-of-3 (128 bits)
w.setSecret(
'SHAD PIS ACAD ALWA ADEQ WILD FANC GROS OASI CYLI MUST WRIS RESC VIEW SHOR OWNER FLIP MAKI CODI ARME\n' +
'SHAD PIS ACAD ACI ACTR PRAY CLAS UNKN DAUG SWEA DEPI FLI TWIC UNKI CRAF EARL SUPE ADVO GUES SMOK',
);
assert.ok(w.validateMnemonic());
assert.strictEqual(w._getExternalAddressByIndex(0), '18pvMjy7AJbCDtv4TLYbGPbR7SzGzjqUpj');
});
it('SLIP39SegwitP2SHWallet can generate addresses', async () => { it('SLIP39SegwitP2SHWallet can generate addresses', async () => {
const w = new SLIP39SegwitP2SHWallet(); const w = new SLIP39SegwitP2SHWallet();
// 23. Basic sharing 2-of-3 (256 bits) // 23. Basic sharing 2-of-3 (256 bits)