-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathkeychains.ts
More file actions
135 lines (116 loc) · 3.91 KB
/
keychains.ts
File metadata and controls
135 lines (116 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import assert from 'assert';
import * as t from 'io-ts';
import { bitgo } from '@bitgo/utxo-lib';
import { IRequestTracer, IWallet, KeyIndices, promiseProps, Triple } from '@bitgo/sdk-core';
import { BIP32, bip32, fixedScriptWallet } from '@bitgo/wasm-utxo';
import { AbstractUtxoCoin } from './abstractUtxoCoin';
import { UtxoWallet } from './wallet';
/*
The standard Keychain type from sdk-core requires a bunch of uninteresting parameters like `id` and `type` and leaves
important fields like `pub` and `prv` optional.
This is a more focused type that only includes the fields we care about.
*/
/**
* A keychain for a UTXO wallet.
*/
export const UtxoKeychain = t.intersection(
[
t.type({
pub: t.string,
}),
t.partial({
prv: t.string,
encryptedPrv: t.string,
}),
],
'UtxoKeychain'
);
export type UtxoKeychain = t.TypeOf<typeof UtxoKeychain>;
export const UtxoNamedKeychains = t.type({
user: UtxoKeychain,
backup: UtxoKeychain,
bitgo: UtxoKeychain,
});
export type UtxoNamedKeychains = t.TypeOf<typeof UtxoNamedKeychains>;
export function toKeychainTriple(keychains: UtxoNamedKeychains): Triple<UtxoKeychain> {
const { user, backup, bitgo } = keychains;
return [user, backup, bitgo];
}
export function toBip32Triple(
keychains: bitgo.RootWalletKeys | UtxoNamedKeychains | Triple<{ pub: string }> | string[]
): Triple<BIP32> {
if (keychains instanceof bitgo.RootWalletKeys) {
return keychains.triple.map((k) => BIP32.fromBase58(k.toBase58())) as Triple<BIP32>;
}
if (Array.isArray(keychains)) {
if (keychains.length !== 3) {
throw new Error('expected 3 keychains');
}
return keychains.map((keychain: { pub: string } | string) => {
const v = typeof keychain === 'string' ? keychain : keychain.pub;
return BIP32.fromBase58(v);
}) as Triple<BIP32>;
}
return toBip32Triple(toKeychainTriple(keychains));
}
function toXpub(keychain: { pub: string } | string | bip32.BIP32Interface): string {
if (typeof keychain === 'string') {
if (keychain.startsWith('xpub')) {
return keychain;
}
throw new Error('expected xpub');
}
if ('neutered' in keychain) {
return keychain.neutered().toBase58();
}
if ('pub' in keychain) {
return toXpub(keychain.pub);
}
throw new Error('expected keychain');
}
export function toXpubTriple(
keychains: UtxoNamedKeychains | Triple<{ pub: string }> | Triple<string> | Triple<bip32.BIP32Interface>
): Triple<string> {
if (Array.isArray(keychains)) {
if (keychains.length !== 3) {
throw new Error('expected 3 keychains');
}
return keychains.map((k) => toXpub(k)) as Triple<string>;
}
return toXpubTriple(toKeychainTriple(keychains));
}
export async function fetchKeychains(
coin: AbstractUtxoCoin,
wallet: IWallet,
reqId?: IRequestTracer
): Promise<UtxoNamedKeychains> {
const result = await promiseProps({
user: coin.keychains().get({ id: wallet.keyIds()[KeyIndices.USER], reqId }),
backup: coin.keychains().get({ id: wallet.keyIds()[KeyIndices.BACKUP], reqId }),
bitgo: coin.keychains().get({ id: wallet.keyIds()[KeyIndices.BITGO], reqId }),
});
assert(UtxoNamedKeychains.is(result));
return result;
}
/**
* Fetch wallet keys as wasm-utxo RootWalletKeys
*/
export async function fetchWasmRootWalletKeys(
coin: AbstractUtxoCoin,
wallet: IWallet,
reqId?: IRequestTracer
): Promise<fixedScriptWallet.RootWalletKeys> {
const keychains = await fetchKeychains(coin, wallet, reqId);
return fixedScriptWallet.RootWalletKeys.from([keychains.user.pub, keychains.backup.pub, keychains.bitgo.pub]);
}
export const KeySignatures = t.partial({
backupPub: t.string,
bitgoPub: t.string,
});
export type KeySignatures = t.TypeOf<typeof KeySignatures>;
export function getKeySignatures(wallet: UtxoWallet): KeySignatures | undefined {
if (t.partial({ keySignatures: KeySignatures }).is(wallet._wallet)) {
return wallet._wallet.keySignatures;
}
return undefined;
}