-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3947 from BitGo/WP-722-add-keycard-generation-for…
…-key-creation feat(key-card): add keycard generation for cold tss key creation
- Loading branch information
Showing
10 changed files
with
204 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as assert from 'assert'; | ||
import { GenerateQrDataBaseParams, GenerateQrDataForKeychainParams, IDrawKeyCard } from './types'; | ||
|
||
export function generateParamsForKeyCreation({ | ||
curve, | ||
bitgoKeychain, | ||
walletLabel, | ||
keyCardImage, | ||
}: GenerateQrDataForKeychainParams & GenerateQrDataBaseParams): IDrawKeyCard { | ||
assert(bitgoKeychain.commonKeychain, 'bitgoKeychain.commonKeychain is required'); | ||
return { | ||
walletLabel, | ||
keyCardImage, | ||
curve, | ||
qrData: { | ||
user: { | ||
title: 'A: Common Keychain', | ||
data: bitgoKeychain.commonKeychain, | ||
description: 'This is the common pub which is the equivalent of xpub (public key)\r\nof the key generated', | ||
}, | ||
bitgo: { | ||
title: 'B: BitGo Key ID', | ||
data: bitgoKeychain.id, | ||
description: | ||
'This is the identifier assigned to the key generated using which BitGo\r\ncan lookup BitGo key share.', | ||
}, | ||
}, | ||
questions: [ | ||
{ | ||
question: 'What is the KeyCard?', | ||
answer: [ | ||
'This key card contains information about the key id for the generated key.\r\nThis id can later be used to derive wallets.', | ||
], | ||
}, | ||
{ | ||
question: 'What should I do with it?', | ||
answer: [ | ||
'Store this keycard for later use. The key ID is important to communicate with BitGo\r\nto derive wallets from the key.', | ||
], | ||
}, | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
import { generateQrData, GenerateQrDataParams } from './generateQrData'; | ||
import jsPDF from 'jspdf'; | ||
|
||
import { generateQrData } from './generateQrData'; | ||
import { generateFaq } from './faq'; | ||
import { drawKeycard } from './drawKeycard'; | ||
import { generateParamsForKeyCreation } from './generateParamsForKeyCreation'; | ||
import { GenerateKeycardParams } from './types'; | ||
|
||
export * from './drawKeycard'; | ||
export * from './faq'; | ||
export * from './generateQrData'; | ||
export * from './utils'; | ||
|
||
export interface GenerateKeycardParams extends GenerateQrDataParams { | ||
activationCode?: string; | ||
keyCardImage?: HTMLImageElement; | ||
walletLabel: string; | ||
} | ||
export * from './types'; | ||
|
||
export async function generateKeycard(params: GenerateKeycardParams): Promise<void> { | ||
const questions = generateFaq(params.coin.fullName); | ||
const qrData = generateQrData(params); | ||
const keycard = await drawKeycard({ ...params, questions, qrData }); | ||
let keycard: jsPDF; | ||
if ('coin' in params) { | ||
const questions = generateFaq(params.coin.fullName); | ||
const qrData = generateQrData(params); | ||
keycard = await drawKeycard({ ...params, questions, qrData }); | ||
} else if ('curve' in params) { | ||
const data = generateParamsForKeyCreation(params); | ||
keycard = await drawKeycard(data); | ||
} else { | ||
throw new Error('Either curve or coin must be provided'); | ||
} | ||
// Save the PDF on the user's browser | ||
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Keychain } from '@bitgo/sdk-core'; | ||
import { BaseCoin, KeyCurve } from '@bitgo/statics'; | ||
|
||
export interface GenerateQrDataBaseParams { | ||
activationCode?: string; | ||
keyCardImage?: HTMLImageElement; | ||
walletLabel: string; | ||
} | ||
|
||
export interface GenerateQrDataForKeychainParams { | ||
// The BitGo keychain as it is returned from the BitGo API upon creation | ||
bitgoKeychain: Keychain; | ||
// The curve used for the key | ||
curve: KeyCurve; | ||
} | ||
|
||
export interface GenerateQrDataParams { | ||
// The backup keychain as it is returned from the BitGo API upon creation | ||
backupKeychain: Keychain; | ||
// The name of the 3rd party provider of the backup key if neither the user nor BitGo stores it | ||
backupKeyProvider?: string; | ||
// The key id of the backup key, only used for cold keys | ||
backupMasterKey?: string; | ||
// The BitGo keychain as it is returned from the BitGo API upon creation | ||
bitgoKeychain: Keychain; | ||
// The coin of the wallet that was/ is about to be created | ||
coin: Readonly<BaseCoin>; | ||
// A code that can be used to encrypt the wallet password to. | ||
// If both the passphrase and passcodeEncryptionCode are passed, then this code encrypts the passphrase with the | ||
// passcodeEncryptionCode and puts the result into Box D. Allows recoveries of the wallet password. | ||
passcodeEncryptionCode?: string; | ||
// The wallet password | ||
// If both the passphrase and passcodeEncryptionCode are passed, then this code encrypts the passphrase with the | ||
// passcodeEncryptionCode and puts the result into Box D. Allows recoveries of the wallet password. | ||
passphrase?: string; | ||
// The user keychain as it is returned from the BitGo API upon creation | ||
userKeychain: Keychain; | ||
// The key id of the user key, only used for cold keys | ||
userMasterKey?: string; | ||
} | ||
|
||
export type GenerateKeycardParams = GenerateQrDataBaseParams & (GenerateQrDataForKeychainParams | GenerateQrDataParams); | ||
|
||
export interface IDrawKeyCard { | ||
activationCode?: string; | ||
keyCardImage?: HTMLImageElement; | ||
qrData: QrData; | ||
questions: FAQ[]; | ||
walletLabel: string; | ||
curve?: KeyCurve; | ||
} | ||
|
||
export interface FAQ { | ||
question: string; | ||
// the answer to the question, already split into individual lines of text | ||
answer: string[]; | ||
} | ||
|
||
export interface QrDataEntry { | ||
data: string; | ||
description: string; | ||
title: string; | ||
publicMasterKey?: string; | ||
} | ||
|
||
export interface QrData { | ||
backup?: QrDataEntry; | ||
bitgo?: QrDataEntry; | ||
passcode?: QrDataEntry; | ||
curve?: KeyCurve; | ||
user: QrDataEntry; | ||
} |
27 changes: 27 additions & 0 deletions
27
modules/key-card/test/unit/generateParamsForKeyCreation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Keychain } from '@bitgo/sdk-core'; | ||
import * as assert from 'assert'; | ||
import { generateParamsForKeyCreation } from '../../src/generateParamsForKeyCreation'; | ||
import { KeyCurve } from '@bitgo/statics'; | ||
|
||
describe('generateParamsForKeyCreation', function () { | ||
it('should return the right params', async function () { | ||
const bitgoKeychain: Keychain = { | ||
id: 'randomId', | ||
commonKeychain: 'random string', | ||
type: 'tss', | ||
}; | ||
const curve = KeyCurve.Ed25519; | ||
const walletLabel = 'random key name'; | ||
const keyCardImage: HTMLImageElement = 'random image' as unknown as HTMLImageElement; | ||
|
||
const result = generateParamsForKeyCreation({ bitgoKeychain, curve, walletLabel, keyCardImage }); | ||
assert(result); | ||
assert(result.qrData.user); | ||
assert(result.qrData.user.data === bitgoKeychain.commonKeychain); | ||
assert(result.qrData.bitgo && result.qrData.bitgo.data === bitgoKeychain.id); | ||
assert(result.questions && result.questions.length === 2); | ||
assert(result.walletLabel === walletLabel); | ||
assert(result.curve === curve); | ||
assert(result.keyCardImage === keyCardImage); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters