-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallet.ts
107 lines (95 loc) · 2.87 KB
/
wallet.ts
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
import { Network } from '@findeth/networks';
import { DerivationPath } from './derivation-paths';
import { HardwareWallet } from './hardware-wallet';
import { getWalletImplementation } from './implementation';
import { Ledger, MnemonicPhrase, Trezor, ExtendedKey } from './implementations';
import { WalletType } from './types';
export interface SignedMessage {
message: string;
address: string;
signature: {
v: number;
r: Uint8Array;
s: Uint8Array;
};
}
export interface Wallet {
/**
* Get all derivation paths supported by the wallet.
*
* @param {Network} network
* @return {DerivationPath[]}
*/
getDerivationPaths(network: Network): DerivationPath[] | Promise<DerivationPath[]>;
/**
* Get an address from the wallet, based on the derivation path and index.
*
* @param {DerivationPath} derivationPath
* @param {number} index
* @return {number}
*/
getAddress(derivationPath: DerivationPath, index: number): Promise<string>;
/**
* Sign a (string) message and return the signed message data.
*
* @param {DerivationPath} derivationPath
* @param {number} index
* @param {status} message
* @return {SignedMessage}
*/
signMessage(derivationPath: DerivationPath, index: number, message: string): Promise<SignedMessage>;
/**
* Optional function to prefetch necessary info from a device, if applicable. Can optionally return the pre-fetched
* data, which may be useful for testing purposes.
*
* @param {DerivationPath[]} derivationPaths
* @return {Promise<T>}
* @template T
*/
prefetch?(derivationPaths: DerivationPath[]): Promise<unknown>;
/**
* Check if the instance of wallet is a hardware wallet.
*
* @return {boolean}
*/
isHardwareWallet(): this is HardwareWallet;
/**
* Serialize the wallet implementation to a JSON string.
*
* @return {string}
*/
serialize(): string;
/**
* Get the type of the wallet.
*
* @return {WalletType}
*/
getType(): WalletType;
}
/**
* Checks if a string is a valid type of WalletType.
*
* @param {string} type
* @return {boolean}
*/
export const isWalletType = (type: string): type is WalletType => {
return Object.values(WalletType).includes(type as WalletType);
};
/**
* Deserialize a wallet implementation from a string.
*
* @param {string} serializedData
* @return {Ledger<*> | Trezor | MnemonicPhrase>}
*/
export const deserialize = (serializedData: string): Ledger<unknown> | Trezor | MnemonicPhrase | ExtendedKey => {
const json = JSON.parse(serializedData);
if (!json.type) {
throw new Error('Serialized data is invalid: missing `type` key');
}
const type = json.type;
if (!isWalletType(type)) {
throw new Error('Serialized data is invalid: `type` is not a valid type of WalletType');
}
const implementation = getWalletImplementation(type);
return implementation.deserialize(serializedData);
};