Skip to content

Commit

Permalink
fix(Setup, Hardware Wallet): import from file
Browse files Browse the repository at this point in the history
support updated coldcard generic json file for wallet importing

see #113
  • Loading branch information
KayBeSee committed Nov 29, 2022
1 parent 30a9d7c commit dad413c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
22 changes: 10 additions & 12 deletions apps/frontend/src/pages/Setup/NewHardwareWalletScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { PlatformContext, ConfigContext } from 'src/context';

import {
HwiEnumerateResponse,
ColdcardElectrumExport,
ColdcardGenericJsonExport,
File,
AddressType,
OnChainConfigWithoutId,
Expand Down Expand Up @@ -240,28 +240,26 @@ const NewHardwareWalletScreen = ({ setStep, newAccount, setNewAccount }: Props)
}
};

const importDeviceFromFile = (parsedFile: ColdcardElectrumExport) => {
const importDeviceFromFile = (parsedFile: ColdcardGenericJsonExport) => {
try {
if (parsedFile.keystore.derivation !== "m/49'/0'/0'") {
// Assuming / Forcing P2WPKH
// TODO: provide more concise validation
if (parsedFile.bip84.name !== 'p2wpkh') {
throw new Error('Invalid file');
}

const xpub = zpubToXpub(decode(parsedFile.keystore.xpub));

const newDevice: HwiEnumerateWithXpubResponse = {
type: parsedFile.keystore.hw_type as HwiEnumerateWithXpubResponse['type'],
fingerprint: parsedFile.keystore.label.substring(
'Coldcard Import '.length,
parsedFile.keystore.label.length
),
xpub: xpub,
type: 'coldcard',
fingerprint: parsedFile.xfp,
xpub: parsedFile.bip84.xpub,
model: 'unknown',
path: 'unknown'
};

const updatedImportedDevices = [...importedDevices, newDevice];
setImportedDevices(updatedImportedDevices);
setStep(3);
setPath(getP2wpkhDeriationPathForNetwork(currentBitcoinNetwork));
setAddressType(AddressType.P2WPKH);
} catch (e) {
if (e instanceof Error) {
openInModal(<ErrorModal message={e.message} closeModal={closeModal} />);
Expand Down
51 changes: 51 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,57 @@ export interface ColdcardElectrumExport {
seed_version: number; // 17
}

export interface ColdcardGenericJsonExport {
xpub: string;
xfp: string;
account: 0;
bip49: {
name: 'p2sh-p2wpkh';
deriv: "m/49'/0'/0'";
xpub: string;
xfp: string;
first: string;
_pub: string;
};
bip44: {
name: 'p2pkh';
deriv: "m/44'/0'/0'";
xpub: string;
xfp: string;
first: string;
};
bip84: {
name: 'p2wpkh';
deriv: "m/84'/0'/0'";
xpub: string;
xfp: string;
first: string;
_pub: string;
};
bip48_2: {
name: 'p2wsh';
deriv: "m/48'/0'/0'/2'";
xpub: string;
xfp: string;
_pub: string;
};
bip45: {
name: 'p2sh';
deriv: "m/45'";
xpub: string;
xfp: string;
_pub: string;
};
chain: 'BTC';
bip48_1: {
name: 'p2sh-p2wsh';
deriv: "m/48'/0'/0'/1'";
xpub: string;
xfp: string;
_pub: string;
};
}

export interface ColdcardDeviceMultisigExportFile {
p2sh_deriv: string; // m/45'
p2sh: string; // xpub68ULYdfaSbytWGGNnutbcDLVyk3hyi3Tv5A8eftKDumTwTbRcT8u4QvZAWHRtqKMPb8serYEokayLLbicvwrar6TF1afmn3637v1Q6T2ZD
Expand Down

0 comments on commit dad413c

Please sign in to comment.