fixed failing import

flatten text file

fix failing test

lint changes

change test values

remove coverage

added more tests for hex, added validation for hex
This commit is contained in:
raytorres280 2022-05-09 20:29:19 -04:00 committed by Overtorment
parent 71fbaa265e
commit c86e6c21f9
3 changed files with 93 additions and 0 deletions

View file

@ -244,6 +244,7 @@ export class AbstractWallet {
masterFingerprint = Number(parsedSecret.keystore.ckcc_xfp);
} else if (parsedSecret.keystore.root_fingerprint) {
masterFingerprint = Number(parsedSecret.keystore.root_fingerprint);
if (!masterFingerprint) masterFingerprint = this.getMasterFingerprintFromHex(parsedSecret.keystore.root_fingerprint);
}
if (parsedSecret.keystore.label) {
this.setLabel(parsedSecret.keystore.label);
@ -418,4 +419,14 @@ export class AbstractWallet {
isSegwit() {
return false;
}
getMasterFingerprintFromHex(hexValue: string): number {
if (hexValue.length < 8) hexValue = '0' + hexValue;
const b = Buffer.from(hexValue, 'hex')
if (b.length !== 4) throw new Error('invalid fingerprint hex');
hexValue = hexValue[6] + hexValue[7] + hexValue[4] + hexValue[5] + hexValue[2] + hexValue[3] + hexValue[0] + hexValue[1];
return parseInt(hexValue, 16);
}
}

View file

@ -0,0 +1,31 @@
{
"addr_history": {},
"addresses": {},
"channels": {},
"fiat_value": {},
"frozen_coins": {},
"imported_channel_backups": {},
"invoices": {},
"keystore": {
"ckcc_xpub": "xpub661MyMwAqRbcFG7osGX7Td2YTmiPEoizenPoxSRQKhirBamV4V8gpSsh8raBSkwPNyVcRFLEKyQg2FtNq6k3zA7yoc7RFMmMYcXGwEjwrVa",
"derivation": "m/84'/0'/0'",
"hw_type": "coldcard",
"label": "ColdcardImportB616BE56",
"root_fingerprint": "b616be56",
"type": "hardware",
"xpub": "zpub6rFDtF1nuXZ9PUL4XzKURh3vJBW6Kj6TUrYL4qPtFNtDXtcTVfiqjQDyrZNwjwzt5HS14qdqo3Co2282Lv3Re6Y5wFZxAVuMEpeygnnDwfx"
},
"labels": {},
"lightning_payments": {},
"payment_requests": {},
"prevouts_by_scripthash": {},
"seed_version": 44,
"spent_outpoints": {},
"transactions": {},
"tx_fees": {},
"txi": {},
"txo": {},
"use_encryption": false,
"verified_tx3": {},
"wallet_type": "standard"
}

View file

@ -227,6 +227,57 @@ describe('Watch only wallet', () => {
);
});
it('can import Electrum compatible backup wallet, and create a tx with master fingerprint hex', async () => {
const w = new WatchOnlyWallet();
w.setSecret(require('fs').readFileSync('./tests/unit/fixtures/skeleton-electrum-hex-only.txt', 'ascii'));
w.init();
assert.ok(w.valid());
assert.strictEqual(
w.getSecret(),
'zpub6rFDtF1nuXZ9PUL4XzKURh3vJBW6Kj6TUrYL4qPtFNtDXtcTVfiqjQDyrZNwjwzt5HS14qdqo3Co2282Lv3Re6Y5wFZxAVuMEpeygnnDwfx',
);
assert.strictEqual(w.getMasterFingerprint(), 1455298230);
assert.strictEqual(w.getMasterFingerprintHex(), 'b616be56');
assert.strictEqual(w.getDerivationPath(), "m/84'/0'/0'");
assert.ok(w.useWithHardwareWalletEnabled());
});
it('can import Electrum compatible backup wallet, and create a tx with master fingerprint hex with a length of 7', async () => {
const w = new WatchOnlyWallet();
let str = require('fs').readFileSync('./tests/unit/fixtures/skeleton-electrum-hex-only.txt', 'ascii');
str = str.replace('b616be56', '616be56')
// console.log(str)
w.setSecret(str);
w.init();
assert.ok(w.valid());
assert.strictEqual(
w.getSecret(),
'zpub6rFDtF1nuXZ9PUL4XzKURh3vJBW6Kj6TUrYL4qPtFNtDXtcTVfiqjQDyrZNwjwzt5HS14qdqo3Co2282Lv3Re6Y5wFZxAVuMEpeygnnDwfx',
);
assert.strictEqual(w.getMasterFingerprint(), 1455298054);
assert.strictEqual(w.getMasterFingerprintHex(), '0616be56');
assert.strictEqual(w.getDerivationPath(), "m/84'/0'/0'");
assert.ok(w.useWithHardwareWalletEnabled());
});
it('will fail to import Electrum compatible backup wallet when fingerprint hex is less than 7', async () => {
const w = new WatchOnlyWallet();
let str = require('fs').readFileSync('./tests/unit/fixtures/skeleton-electrum-hex-only.txt', 'ascii');
str = str.replace('b616be56', '16be56')
w.setSecret(str);
w.init();
assert.throws(w.valid, 'invalid fingerprint hex');
});
it('will fail to import Electrum compatible backup wallet when fingerprint is an invalid hex value', async () => {
const w = new WatchOnlyWallet();
let str = require('fs').readFileSync('./tests/unit/fixtures/skeleton-electrum-hex-only.txt', 'ascii');
str = str.replace('b616be56', 'j16be56')
w.setSecret(str);
w.init();
assert.throws(w.valid, 'invalid fingerprint hex');
});
it('can import cobo vault JSON skeleton wallet', async () => {
const w = new WatchOnlyWallet();
w.setSecret(require('fs').readFileSync('./tests/unit/fixtures/skeleton-cobo.txt', 'ascii'));