-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathwallet.ts
196 lines (178 loc) · 5.84 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { executeCommand } from './execute/execute';
import Transport from '../transport';
import { commands } from "./execute/command";
import { target } from '../config/param';
import { CODE } from '../config/status/code';
import { SDKError, APDUError } from '../error/errorHandle';
import * as setting from '../setting/index';
import * as crypto from '../crypto/index';
/**
* TODO
* Authorization for requesting account keys
* @param {Transport} transport
* @param {string} signature
* @return { Promise<boolean> }
*/
export const authGetExtendedKey = async (transport: Transport, signature: string
): Promise<boolean> => {
try {
const { statusCode } = await executeCommand(transport, commands.AUTH_EXT_KEY, target.SE, signature, undefined, undefined);
if (statusCode === CODE._9000) {
return true;
} else {
return false;
}
} catch (e) {
throw new SDKError(authGetExtendedKey.name, 'authGetExtendedKey error')
}
};
/**
* Get ECDSA Account Extended public key (Encrypted)
* @param {*} transport
* @param {string} coinType P1
* @param {string} accIndex P2
* @return {Promise<string>}
*/
export const getAccountExtendedKey = async (transport: Transport, path: string): Promise<string> => {
const { outputData: key, statusCode, msg } = await executeCommand(transport, commands.GET_EXT_KEY, target.SE, path, undefined, undefined);
if (key) {
return key
} else {
throw new APDUError(commands.GET_EXT_KEY, statusCode, msg)
}
};
/**
* Create a new seed with SE.
* @param {Transport} transport
* @param {string} appId
* @param {string} appPrivateKey
* @param {Number} strength 12, 18, 24
* @return {Promise<boolean>}
*/
export async function createSeedByCard(transport: Transport, appId: string, appPrivateKey: string, strength: number): Promise<boolean> {
let strengthHex = strength.toString(16);
if (strengthHex.length % 2 > 0) strengthHex = `0${strengthHex}`;
const signature = await setting.auth.getCommandSignature(
transport,
appId,
appPrivateKey,
commands.CREATE_WALLET,
strengthHex,
undefined,
undefined,
);
const strengthWithSig = strengthHex + signature;
const { statusCode, msg } = await executeCommand(
transport,
commands.CREATE_WALLET,
target.SE,
strengthWithSig,
undefined,
undefined,
);
if (statusCode === CODE._9000) {
return true;
} else {
throw new APDUError(commands.CREATE_WALLET, statusCode, msg);
}
}
/**
* Send sum of number seeds.
* @param {Transport}
* @param {number} checkSum
* @return {Promise<boolean>}
*/
export async function sendCheckSum(transport: Transport, checkSum: number): Promise<boolean> {
const sumHex = checkSum.toString(16).padStart(8, '0');
const { statusCode } = await executeCommand(transport, commands.CHECKSUM, target.SE, sumHex);
return statusCode === CODE._9000;
}
/**
* @param {Transport}
* @param {string} appId
* @param {string} appPrivateKey
* @param {string} mnemonic
* @return {Promise<boolean>}
*/
export async function setSeed(transport: Transport, appId: string, appPrivateKey: string, seedHex: string, SEPublicKey: string) {
try {
if (!SEPublicKey) {
throw new SDKError(setSeed.name, 'SEPublicKey can not be undefined')
}
const encryptedSeed = crypto.encryption.ECIESenc(SEPublicKey, seedHex);
const signature = await setting.auth.getCommandSignature(
transport,
appId,
appPrivateKey,
commands.SET_SEED,
encryptedSeed,
undefined
);
const signedSeed = encryptedSeed + signature;
const { statusCode, msg } = await executeCommand(transport, commands.SET_SEED, target.SE, signedSeed, undefined, undefined);
if (statusCode === CODE._9000) {
} else if (statusCode === CODE._6881) {
throw new APDUError(commands.SET_SEED, statusCode, "wallet is exist")
} else {
throw new APDUError(commands.SET_SEED, statusCode, msg)
}
} catch (e) {
console.error(e);
throw e
}
}
/**
*
* @param {Transport} transport
* @param {number} strength
*/
export async function initSecureRecovery(transport: Transport, strength: number) {
const P1 = strength.toString(16).padStart(2, '0');
const { statusCode } = await executeCommand(transport, commands.MCU_SET_MNEMONIC_INFO, target.SE, undefined, P1, undefined);
return statusCode === CODE._9000;
};
/**
*
* @param {Transport} transport
* @param {number} index
*/
export async function setSecureRecoveryIdx(transport: Transport, index: number) {
const P1 = index.toString(16).padStart(2, '0');
const { statusCode } = await executeCommand(transport, commands.MCU_SET_CHARACTER_ID, target.SE, undefined, P1, undefined);
return statusCode === CODE._9000;
};
/**
*
* @param {Transport} transport
* @param {string} type
*/
export async function cancelSecureRecovery(transport: Transport, type: string) {
let P1;
if (type === '00') {
P1 = '05';
} else if (type === '01') {
P1 = 'A1';
} else {
throw Error(`Type:${type} is invalid`);
}
const { statusCode } = await executeCommand(transport, commands.MCU_CANCEL_RECOVERY, target.SE, undefined, P1, undefined);
return statusCode === CODE._9000;
};
/**
*
* @param {Transport} transport
*/
export async function getSecureRecoveryStatus(transport: Transport) {
const { statusCode, outputData } = await executeCommand(transport, commands.GET_MCU_STATUS, target.SE, undefined, undefined, undefined);
return statusCode;
}
/**
*
* @param {Transport} transport
*/
export async function initMcuSetSeed(transport: Transport) {
const { statusCode, outputData } = await executeCommand(transport, commands.MCU_START_SET_SEED, target.SE, undefined, undefined, undefined);
return statusCode;
}
export let creation = { createSeedByCard, sendCheckSum, setSeed };
export let recovery = { setSeed, initSecureRecovery, setSecureRecoveryIdx, cancelSecureRecovery, getSecureRecoveryStatus, initMcuSetSeed };