mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-22 23:08:07 +01:00
ADD: support truncated words during slip39 wallet import
This commit is contained in:
parent
1f4815636c
commit
8dd015e687
2 changed files with 35 additions and 4 deletions
|
@ -1,4 +1,5 @@
|
|||
import slip39 from 'slip39';
|
||||
import { WORD_LIST } from 'slip39/dist/slip39_helper';
|
||||
import createHash from 'create-hash';
|
||||
|
||||
import { HDLegacyP2PKHWallet } from './hd-legacy-p2pkh-wallet';
|
||||
|
@ -24,17 +25,35 @@ const SLIP39Mixin = {
|
|||
},
|
||||
|
||||
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
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(s => s)
|
||||
.map(s =>
|
||||
s
|
||||
.map(s => {
|
||||
let secret = s
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.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;
|
||||
},
|
||||
|
||||
|
|
|
@ -43,6 +43,18 @@ describe('SLIP39 wallets tests', () => {
|
|||
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 () => {
|
||||
const w = new SLIP39SegwitP2SHWallet();
|
||||
// 23. Basic sharing 2-of-3 (256 bits)
|
||||
|
|
Loading…
Add table
Reference in a new issue