@onekeyfe/hd-core 1.2.8-alpha.101 → 1.2.8-alpha.102
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/logBlockEvent.test.ts +0 -43
- package/dist/api/index.d.ts +0 -2
- package/dist/api/index.d.ts.map +1 -1
- package/dist/events/logBlockEvent.d.ts.map +1 -1
- package/dist/index.d.ts +3 -36
- package/dist/index.js +2 -329
- package/dist/inject.d.ts.map +1 -1
- package/dist/types/api/export.d.ts +0 -2
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +0 -4
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/utils/patch.d.ts +1 -1
- package/dist/utils/patch.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/index.ts +0 -2
- package/src/data/messages/messages-protocol-v2.json +1 -138
- package/src/events/logBlockEvent.ts +2 -4
- package/src/inject.ts +0 -4
- package/src/types/api/export.ts +0 -2
- package/src/types/api/index.ts +0 -8
- package/__tests__/zcash-protobuf.test.ts +0 -141
- package/__tests__/zcash-sign-pczt.test.ts +0 -387
- package/dist/api/zcash/ZcashGetAddress.d.ts +0 -12
- package/dist/api/zcash/ZcashGetAddress.d.ts.map +0 -1
- package/dist/api/zcash/ZcashSignPczt.d.ts +0 -16
- package/dist/api/zcash/ZcashSignPczt.d.ts.map +0 -1
- package/dist/types/api/zcashGetAddress.d.ts +0 -22
- package/dist/types/api/zcashGetAddress.d.ts.map +0 -1
- package/dist/types/api/zcashSignPczt.d.ts +0 -9
- package/dist/types/api/zcashSignPczt.d.ts.map +0 -1
- package/src/api/zcash/ZcashGetAddress.ts +0 -121
- package/src/api/zcash/ZcashSignPczt.ts +0 -155
- package/src/types/api/zcashGetAddress.ts +0 -36
- package/src/types/api/zcashSignPczt.ts +0 -18
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import * as protobuf from 'protobufjs/light';
|
|
2
|
-
import transport from '@onekeyfe/hd-transport';
|
|
3
|
-
|
|
4
|
-
import messagesJson from '../src/data/messages/messages-protocol-v2.json';
|
|
5
|
-
|
|
6
|
-
// Round-trips the Zcash messages through the SDK's real protobuf encoder and
|
|
7
|
-
// decoder against the shipped protocol V2 schema, pinning wire ids and defaults.
|
|
8
|
-
const {
|
|
9
|
-
createMessageFromName,
|
|
10
|
-
decodeProtobuf: decode,
|
|
11
|
-
encodeProtobuf: encode,
|
|
12
|
-
parseConfigure,
|
|
13
|
-
} = transport;
|
|
14
|
-
const messages = parseConfigure(messagesJson as protobuf.INamespace);
|
|
15
|
-
|
|
16
|
-
const wireFieldIds = (buf: Uint8Array) => {
|
|
17
|
-
const reader = protobuf.Reader.create(buf);
|
|
18
|
-
const ids: number[] = [];
|
|
19
|
-
while (reader.pos < reader.len) {
|
|
20
|
-
const tag = reader.uint32();
|
|
21
|
-
// eslint-disable-next-line no-bitwise
|
|
22
|
-
ids.push(tag >>> 3);
|
|
23
|
-
// eslint-disable-next-line no-bitwise
|
|
24
|
-
reader.skipType(tag & 7);
|
|
25
|
-
}
|
|
26
|
-
return ids;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const roundTrip = (name: string, data: Record<string, unknown>) => {
|
|
30
|
-
const { Message, messageTypeId } = createMessageFromName(messages, name);
|
|
31
|
-
const encoded = encode(Message, data);
|
|
32
|
-
const bytes = new Uint8Array(encoded.toBuffer());
|
|
33
|
-
return { messageTypeId, ids: wireFieldIds(bytes), decoded: decode(Message, encoded) };
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
describe('Zcash protobuf messages (protocol V2)', () => {
|
|
37
|
-
test.each([
|
|
38
|
-
['ZcashGetAddress', 12400],
|
|
39
|
-
['ZcashAddress', 12401],
|
|
40
|
-
['ZcashSignPczt', 12402],
|
|
41
|
-
['ZcashSignedPczt', 12403],
|
|
42
|
-
['ZcashPcztChunkRequest', 12404],
|
|
43
|
-
['ZcashPcztChunkAck', 12405],
|
|
44
|
-
['ZcashSignedPcztChunkRequest', 12406],
|
|
45
|
-
])('%s has wire type %d', (name, typeId) => {
|
|
46
|
-
expect(createMessageFromName(messages, name).messageTypeId).toBe(typeId);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test.each(['ZcashSignPczt', 'ZcashSignedPczt'])('%s round trips length and bytes', name => {
|
|
50
|
-
const chunk = 'ab'.repeat(1024);
|
|
51
|
-
const { ids, decoded } = roundTrip(name, { pczt_length: 4096, pczt_initial_chunk: chunk });
|
|
52
|
-
expect(ids).toEqual([1, 2]);
|
|
53
|
-
expect(decoded).toEqual({ pczt_length: 4096, pczt_initial_chunk: chunk });
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test('ZcashPcztChunkAck round trips the data chunk', () => {
|
|
57
|
-
const { ids, decoded } = roundTrip('ZcashPcztChunkAck', { data_chunk: '00ff10' });
|
|
58
|
-
expect(ids).toEqual([1]);
|
|
59
|
-
expect(decoded).toEqual({ data_chunk: '00ff10' });
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test.each(['ZcashPcztChunkRequest', 'ZcashSignedPcztChunkRequest'])(
|
|
63
|
-
'%s round trips the chunk length',
|
|
64
|
-
name => {
|
|
65
|
-
const { ids, decoded } = roundTrip(name, { chunk_length: 1024 });
|
|
66
|
-
expect(ids).toEqual([1]);
|
|
67
|
-
expect(decoded).toEqual({ chunk_length: 1024 });
|
|
68
|
-
}
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
test('ZcashGetAddress encodes every field with its schema id', () => {
|
|
72
|
-
const { ids, decoded } = roundTrip('ZcashGetAddress', {
|
|
73
|
-
address_n: [2147483680, 2147483781, 2147483648],
|
|
74
|
-
show_display: true,
|
|
75
|
-
include_seed_fingerprint: true,
|
|
76
|
-
include_ufvk: true,
|
|
77
|
-
diversifier_index: 0xffffffff,
|
|
78
|
-
address_type: 2,
|
|
79
|
-
scope: 1,
|
|
80
|
-
});
|
|
81
|
-
// address_n is unpacked: one tag per element.
|
|
82
|
-
expect(ids).toEqual([1, 1, 1, 2, 3, 4, 5, 6, 7]);
|
|
83
|
-
expect(decoded).toEqual({
|
|
84
|
-
address_n: [2147483680, 2147483781, 2147483648],
|
|
85
|
-
show_display: true,
|
|
86
|
-
include_seed_fingerprint: true,
|
|
87
|
-
include_ufvk: true,
|
|
88
|
-
diversifier_index: 0xffffffff,
|
|
89
|
-
address_type: 'UNIFIED_ORCHARD',
|
|
90
|
-
scope: 'INTERNAL',
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test('ZcashGetAddress enum values match the schema', () => {
|
|
95
|
-
const addressType = messages.lookupEnum('ZcashAddressType').values;
|
|
96
|
-
const scope = messages.lookupEnum('ZcashAddressScope').values;
|
|
97
|
-
expect(addressType).toEqual({
|
|
98
|
-
UNIFIED_ORCHARD_P2PKH: 0,
|
|
99
|
-
TRANSPARENT_P2PKH: 1,
|
|
100
|
-
UNIFIED_ORCHARD: 2,
|
|
101
|
-
});
|
|
102
|
-
expect(scope).toEqual({ EXTERNAL: 0, INTERNAL: 1 });
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('ZcashGetAddress omits unset optional fields and keeps schema defaults', () => {
|
|
106
|
-
const { Message } = createMessageFromName(messages, 'ZcashGetAddress');
|
|
107
|
-
const { ids } = roundTrip('ZcashGetAddress', { address_n: [2147483680] });
|
|
108
|
-
expect(ids).toEqual([1]);
|
|
109
|
-
expect(Message.fields.diversifier_index.defaultValue).toBe(0);
|
|
110
|
-
expect(Message.fields.address_type.defaultValue).toBe(0);
|
|
111
|
-
expect(Message.fields.scope.defaultValue).toBe(0);
|
|
112
|
-
const empty = Message.decode(new Uint8Array(0)) as unknown as Record<string, unknown>;
|
|
113
|
-
expect(empty.diversifier_index).toBe(0);
|
|
114
|
-
expect(empty.address_type).toBe(0);
|
|
115
|
-
expect(empty.scope).toBe(0);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('ZcashAddress decodes optional ufvk and seed fingerprint', () => {
|
|
119
|
-
const fingerprint = '0f'.repeat(32);
|
|
120
|
-
const full = roundTrip('ZcashAddress', {
|
|
121
|
-
address: 'u1abc',
|
|
122
|
-
ufvk: 'uview1abc',
|
|
123
|
-
seed_fingerprint: fingerprint,
|
|
124
|
-
});
|
|
125
|
-
expect(full.ids).toEqual([1, 2, 3]);
|
|
126
|
-
expect(full.decoded).toEqual({
|
|
127
|
-
address: 'u1abc',
|
|
128
|
-
ufvk: 'uview1abc',
|
|
129
|
-
seed_fingerprint: fingerprint,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const minimal = roundTrip('ZcashAddress', { address: 't1abc' });
|
|
133
|
-
expect(minimal.ids).toEqual([1]);
|
|
134
|
-
expect(minimal.decoded).toEqual({ address: 't1abc', ufvk: null, seed_fingerprint: null });
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('required fields are enforced on decode', () => {
|
|
138
|
-
const { Message } = createMessageFromName(messages, 'ZcashSignedPczt');
|
|
139
|
-
expect(() => Message.decode(new Uint8Array(0))).toThrow(/missing required/);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,387 +0,0 @@
|
|
|
1
|
-
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
-
|
|
3
|
-
import ZcashSignPczt from '../src/api/zcash/ZcashSignPczt';
|
|
4
|
-
import ZcashGetAddress from '../src/api/zcash/ZcashGetAddress';
|
|
5
|
-
|
|
6
|
-
import type { Device } from '../src/device/Device';
|
|
7
|
-
|
|
8
|
-
jest.mock('../src/data/config', () => ({
|
|
9
|
-
getSDKVersion: jest.fn(() => '1.0.0'),
|
|
10
|
-
DEFAULT_DOMAIN: 'https://jssdk.onekey.so/1.0.0/',
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
const hex = (byte: number, length: number) => byte.toString(16).padStart(2, '0').repeat(length);
|
|
14
|
-
|
|
15
|
-
const createSignMethod = (pczt: string) =>
|
|
16
|
-
new ZcashSignPczt({ id: 1, payload: { method: 'zcashSignPczt', pczt } });
|
|
17
|
-
|
|
18
|
-
const attachDevice = (method: ZcashSignPczt | ZcashGetAddress, typedCall: jest.Mock) => {
|
|
19
|
-
const commands = { typedCall };
|
|
20
|
-
method.device = {
|
|
21
|
-
commands,
|
|
22
|
-
getCommands: () => commands,
|
|
23
|
-
toMessageObject: () => ({}),
|
|
24
|
-
} as unknown as Device;
|
|
25
|
-
method.postMessage = jest.fn();
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
describe('ZcashSignPczt', () => {
|
|
29
|
-
test('uploads device-sized chunks and downloads host-sized chunks', async () => {
|
|
30
|
-
// 2500 bytes: initial 1024, then device asks 1000 + 476.
|
|
31
|
-
const pczt = hex(0xab, 2500);
|
|
32
|
-
const signed = Buffer.alloc(1500, 0xcd);
|
|
33
|
-
const method = createSignMethod(pczt);
|
|
34
|
-
method.init();
|
|
35
|
-
|
|
36
|
-
const typedCall = jest
|
|
37
|
-
.fn()
|
|
38
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkRequest', message: { chunk_length: 1000 } })
|
|
39
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkRequest', message: { chunk_length: 476 } })
|
|
40
|
-
.mockResolvedValueOnce({
|
|
41
|
-
type: 'ZcashSignedPczt',
|
|
42
|
-
message: {
|
|
43
|
-
pczt_length: signed.length,
|
|
44
|
-
pczt_initial_chunk: signed.subarray(0, 1024).toString('hex'),
|
|
45
|
-
},
|
|
46
|
-
})
|
|
47
|
-
.mockResolvedValueOnce({
|
|
48
|
-
type: 'ZcashPcztChunkAck',
|
|
49
|
-
message: { data_chunk: signed.subarray(1024).toString('hex') },
|
|
50
|
-
});
|
|
51
|
-
attachDevice(method, typedCall);
|
|
52
|
-
|
|
53
|
-
await expect(method.run()).resolves.toEqual({ pczt: signed.toString('hex') });
|
|
54
|
-
|
|
55
|
-
expect(typedCall.mock.calls[0]).toEqual([
|
|
56
|
-
'ZcashSignPczt',
|
|
57
|
-
['ZcashSignedPczt', 'ZcashPcztChunkRequest'],
|
|
58
|
-
{ pczt_length: 2500, pczt_initial_chunk: hex(0xab, 1024) },
|
|
59
|
-
]);
|
|
60
|
-
expect(typedCall.mock.calls[1]).toEqual([
|
|
61
|
-
'ZcashPcztChunkAck',
|
|
62
|
-
['ZcashSignedPczt', 'ZcashPcztChunkRequest'],
|
|
63
|
-
{ data_chunk: hex(0xab, 1000) },
|
|
64
|
-
]);
|
|
65
|
-
expect(typedCall.mock.calls[2][2]).toEqual({ data_chunk: hex(0xab, 476) });
|
|
66
|
-
expect(typedCall.mock.calls[3]).toEqual([
|
|
67
|
-
'ZcashSignedPcztChunkRequest',
|
|
68
|
-
'ZcashPcztChunkAck',
|
|
69
|
-
{ chunk_length: 476 },
|
|
70
|
-
]);
|
|
71
|
-
expect(typedCall).toHaveBeenCalledTimes(4);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test('single-message round trip when both sides fit in the initial chunk', async () => {
|
|
75
|
-
const method = createSignMethod(hex(0x01, 100));
|
|
76
|
-
method.init();
|
|
77
|
-
const typedCall = jest.fn().mockResolvedValueOnce({
|
|
78
|
-
type: 'ZcashSignedPczt',
|
|
79
|
-
message: { pczt_length: 80, pczt_initial_chunk: hex(0x02, 80) },
|
|
80
|
-
});
|
|
81
|
-
attachDevice(method, typedCall);
|
|
82
|
-
|
|
83
|
-
await expect(method.run()).resolves.toEqual({ pczt: hex(0x02, 80) });
|
|
84
|
-
expect(typedCall).toHaveBeenCalledTimes(1);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('rejects a device chunk request past the end of the pczt', async () => {
|
|
88
|
-
const method = createSignMethod(hex(0x01, 1100));
|
|
89
|
-
method.init();
|
|
90
|
-
const typedCall = jest
|
|
91
|
-
.fn()
|
|
92
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkRequest', message: { chunk_length: 200 } });
|
|
93
|
-
attachDevice(method, typedCall);
|
|
94
|
-
|
|
95
|
-
await expect(method.run()).rejects.toThrow('device requested 200 bytes at offset 1024');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test('rejects completion before the entire input has been uploaded', async () => {
|
|
99
|
-
const method = createSignMethod(hex(0x01, 1100));
|
|
100
|
-
method.init();
|
|
101
|
-
const typedCall = jest.fn().mockResolvedValueOnce({
|
|
102
|
-
type: 'ZcashSignedPczt',
|
|
103
|
-
message: { pczt_length: 80, pczt_initial_chunk: hex(0x02, 80) },
|
|
104
|
-
});
|
|
105
|
-
attachDevice(method, typedCall);
|
|
106
|
-
await expect(method.run()).rejects.toThrow('before receiving the whole pczt');
|
|
107
|
-
expect(typedCall).toHaveBeenCalledTimes(1);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('rejects an initial signed chunk larger than its announced total', async () => {
|
|
111
|
-
const method = createSignMethod(hex(0x01, 100));
|
|
112
|
-
method.init();
|
|
113
|
-
const typedCall = jest.fn().mockResolvedValueOnce({
|
|
114
|
-
type: 'ZcashSignedPczt',
|
|
115
|
-
message: { pczt_length: 80, pczt_initial_chunk: hex(0x02, 81) },
|
|
116
|
-
});
|
|
117
|
-
attachDevice(method, typedCall);
|
|
118
|
-
await expect(method.run()).rejects.toThrow('more initial pczt bytes than announced');
|
|
119
|
-
expect(typedCall).toHaveBeenCalledTimes(1);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
test('rejects an announced signed length that is not a sane positive size', async () => {
|
|
123
|
-
for (const total of [0, 1.5, 16 * 1024 * 1024 + 1]) {
|
|
124
|
-
const method = createSignMethod(hex(0x01, 100));
|
|
125
|
-
method.init();
|
|
126
|
-
const typedCall = jest.fn().mockResolvedValueOnce({
|
|
127
|
-
type: 'ZcashSignedPczt',
|
|
128
|
-
message: { pczt_length: total, pczt_initial_chunk: '' },
|
|
129
|
-
});
|
|
130
|
-
attachDevice(method, typedCall);
|
|
131
|
-
await expect(method.run()).rejects.toThrow('invalid signed pczt length');
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test('rejects empty input and leaves size limits to the device', () => {
|
|
136
|
-
expect(() => createSignMethod('').init()).toThrow('pczt is empty');
|
|
137
|
-
const method = createSignMethod(hex(0x00, 64 * 1024));
|
|
138
|
-
expect(() => method.init()).not.toThrow();
|
|
139
|
-
expect(method.params.pczt_length).toBe(64 * 1024);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe('ZcashSignPczt protocol edge cases', () => {
|
|
144
|
-
const signedMessage = (signed: Buffer, initialLength = 1024) => ({
|
|
145
|
-
type: 'ZcashSignedPczt',
|
|
146
|
-
message: {
|
|
147
|
-
pczt_length: signed.length,
|
|
148
|
-
pczt_initial_chunk: signed.subarray(0, initialLength).toString('hex'),
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
const runWith = (pczt: string, typedCall: jest.Mock) => {
|
|
153
|
-
const method = createSignMethod(pczt);
|
|
154
|
-
method.init();
|
|
155
|
-
attachDevice(method, typedCall);
|
|
156
|
-
return method.run();
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
test.each([
|
|
160
|
-
{ name: 'short', data: hex(0xcd, 100) },
|
|
161
|
-
{ name: 'empty', data: '' },
|
|
162
|
-
{ name: 'missing', data: undefined },
|
|
163
|
-
])('rejects a $name download chunk', async ({ data }) => {
|
|
164
|
-
const signed = Buffer.alloc(1500, 0xcd);
|
|
165
|
-
const typedCall = jest
|
|
166
|
-
.fn()
|
|
167
|
-
.mockResolvedValueOnce(signedMessage(signed))
|
|
168
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkAck', message: { data_chunk: data } });
|
|
169
|
-
|
|
170
|
-
await expect(runWith(hex(0x01, 100), typedCall)).rejects.toThrow(
|
|
171
|
-
'requested bytes at offset 1024 of 1500'
|
|
172
|
-
);
|
|
173
|
-
expect(typedCall).toHaveBeenCalledTimes(2);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
test('rejects a download chunk longer than requested', async () => {
|
|
177
|
-
const signed = Buffer.alloc(1500, 0xcd);
|
|
178
|
-
const typedCall = jest
|
|
179
|
-
.fn()
|
|
180
|
-
.mockResolvedValueOnce(signedMessage(signed))
|
|
181
|
-
.mockResolvedValueOnce({
|
|
182
|
-
type: 'ZcashPcztChunkAck',
|
|
183
|
-
message: { data_chunk: hex(0xcd, 477) },
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
await expect(runWith(hex(0x01, 100), typedCall)).rejects.toThrow(
|
|
187
|
-
'device returned 477 of 476 requested bytes'
|
|
188
|
-
);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
test.each([0, -1, 1.5, Number.NaN])(
|
|
192
|
-
'rejects a device upload request for chunk length %s',
|
|
193
|
-
async chunkLength => {
|
|
194
|
-
const typedCall = jest.fn().mockResolvedValueOnce({
|
|
195
|
-
type: 'ZcashPcztChunkRequest',
|
|
196
|
-
message: { chunk_length: chunkLength },
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
await expect(runWith(hex(0x01, 2000), typedCall)).rejects.toThrow(
|
|
200
|
-
`device requested ${chunkLength} bytes at offset 1024`
|
|
201
|
-
);
|
|
202
|
-
expect(typedCall).toHaveBeenCalledTimes(1);
|
|
203
|
-
}
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
test('propagates a device Failure in the middle of upload', async () => {
|
|
207
|
-
const failure = ERRORS.TypedError(
|
|
208
|
-
HardwareErrorCode.ActionCancelled,
|
|
209
|
-
'Action cancelled by user'
|
|
210
|
-
);
|
|
211
|
-
const typedCall = jest
|
|
212
|
-
.fn()
|
|
213
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkRequest', message: { chunk_length: 500 } })
|
|
214
|
-
.mockRejectedValueOnce(failure);
|
|
215
|
-
|
|
216
|
-
await expect(runWith(hex(0x01, 2000), typedCall)).rejects.toBe(failure);
|
|
217
|
-
expect(typedCall).toHaveBeenCalledTimes(2);
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
test('propagates a device Failure in the middle of download', async () => {
|
|
221
|
-
const failure = ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Firmware error');
|
|
222
|
-
const signed = Buffer.alloc(3000, 0xcd);
|
|
223
|
-
const typedCall = jest
|
|
224
|
-
.fn()
|
|
225
|
-
.mockResolvedValueOnce(signedMessage(signed))
|
|
226
|
-
.mockResolvedValueOnce({
|
|
227
|
-
type: 'ZcashPcztChunkAck',
|
|
228
|
-
message: { data_chunk: signed.subarray(1024, 2048).toString('hex') },
|
|
229
|
-
})
|
|
230
|
-
.mockRejectedValueOnce(failure);
|
|
231
|
-
|
|
232
|
-
await expect(runWith(hex(0x01, 100), typedCall)).rejects.toBe(failure);
|
|
233
|
-
expect(typedCall).toHaveBeenCalledTimes(3);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
test('exactly 1024 bytes in both directions needs no extra chunks', async () => {
|
|
237
|
-
const signed = Buffer.alloc(1024, 0xcd);
|
|
238
|
-
const typedCall = jest.fn().mockResolvedValueOnce(signedMessage(signed));
|
|
239
|
-
|
|
240
|
-
await expect(runWith(hex(0xab, 1024), typedCall)).resolves.toEqual({
|
|
241
|
-
pczt: signed.toString('hex'),
|
|
242
|
-
});
|
|
243
|
-
expect(typedCall).toHaveBeenCalledTimes(1);
|
|
244
|
-
expect(typedCall.mock.calls[0][2]).toEqual({
|
|
245
|
-
pczt_length: 1024,
|
|
246
|
-
pczt_initial_chunk: hex(0xab, 1024),
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
test('1025 bytes in both directions splits into 1024 + 1', async () => {
|
|
251
|
-
const pczt = `${hex(0xab, 1024)}ef`;
|
|
252
|
-
const signed = Buffer.concat([Buffer.alloc(1024, 0xcd), Buffer.from([0x99])]);
|
|
253
|
-
const typedCall = jest
|
|
254
|
-
.fn()
|
|
255
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkRequest', message: { chunk_length: 1 } })
|
|
256
|
-
.mockResolvedValueOnce(signedMessage(signed))
|
|
257
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkAck', message: { data_chunk: '99' } });
|
|
258
|
-
|
|
259
|
-
await expect(runWith(pczt, typedCall)).resolves.toEqual({ pczt: signed.toString('hex') });
|
|
260
|
-
expect(typedCall.mock.calls[0][2]).toEqual({
|
|
261
|
-
pczt_length: 1025,
|
|
262
|
-
pczt_initial_chunk: hex(0xab, 1024),
|
|
263
|
-
});
|
|
264
|
-
expect(typedCall.mock.calls[1][2]).toEqual({ data_chunk: 'ef' });
|
|
265
|
-
expect(typedCall.mock.calls[2][2]).toEqual({ chunk_length: 1 });
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
test('2048 bytes in both directions uses one full 1024-byte chunk each way', async () => {
|
|
269
|
-
const signed = Buffer.alloc(2048, 0xcd);
|
|
270
|
-
const typedCall = jest
|
|
271
|
-
.fn()
|
|
272
|
-
.mockResolvedValueOnce({ type: 'ZcashPcztChunkRequest', message: { chunk_length: 1024 } })
|
|
273
|
-
.mockResolvedValueOnce(signedMessage(signed))
|
|
274
|
-
.mockResolvedValueOnce({
|
|
275
|
-
type: 'ZcashPcztChunkAck',
|
|
276
|
-
message: { data_chunk: signed.subarray(1024).toString('hex') },
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
await expect(runWith(hex(0xab, 2048), typedCall)).resolves.toEqual({
|
|
280
|
-
pczt: signed.toString('hex'),
|
|
281
|
-
});
|
|
282
|
-
expect(typedCall.mock.calls[1][2]).toEqual({ data_chunk: hex(0xab, 1024) });
|
|
283
|
-
expect(typedCall.mock.calls[2][2]).toEqual({ chunk_length: 1024 });
|
|
284
|
-
expect(typedCall).toHaveBeenCalledTimes(3);
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
test.each(['abc', '0xabc', 'a'])('rejects odd-length hex %s', pczt => {
|
|
288
|
-
expect(() => createSignMethod(pczt).init()).toThrow('pczt hex must have an even length');
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
test.each(['zz', '0xgg', 'ab cd'])('rejects non-hex input %s', pczt => {
|
|
292
|
-
expect(() => createSignMethod(pczt).init()).toThrow('should be [hexString]');
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
test('accepts a 0x-prefixed even-length hex string', () => {
|
|
296
|
-
const method = createSignMethod('0xABcd');
|
|
297
|
-
method.init();
|
|
298
|
-
expect(method.params).toEqual({ pczt_length: 2, pczt_initial_chunk: 'abcd' });
|
|
299
|
-
});
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
describe('ZcashGetAddress', () => {
|
|
303
|
-
test('maps params and response', async () => {
|
|
304
|
-
const method = new ZcashGetAddress({
|
|
305
|
-
id: 1,
|
|
306
|
-
payload: {
|
|
307
|
-
method: 'zcashGetAddress',
|
|
308
|
-
path: "m/32'/133'/0'",
|
|
309
|
-
showOnOneKey: false,
|
|
310
|
-
includeUfvk: true,
|
|
311
|
-
includeSeedFingerprint: true,
|
|
312
|
-
},
|
|
313
|
-
});
|
|
314
|
-
method.init();
|
|
315
|
-
const typedCall = jest.fn().mockResolvedValue({
|
|
316
|
-
message: { address: 'u1abc', ufvk: 'uview1abc', seed_fingerprint: hex(0x0f, 32) },
|
|
317
|
-
});
|
|
318
|
-
attachDevice(method, typedCall);
|
|
319
|
-
|
|
320
|
-
await expect(method.run()).resolves.toEqual({
|
|
321
|
-
path: "m/32'/133'/0'",
|
|
322
|
-
address: 'u1abc',
|
|
323
|
-
ufvk: 'uview1abc',
|
|
324
|
-
seedFingerprint: hex(0x0f, 32),
|
|
325
|
-
});
|
|
326
|
-
expect(typedCall).toHaveBeenCalledWith('ZcashGetAddress', 'ZcashAddress', {
|
|
327
|
-
address_n: [2147483680, 2147483781, 2147483648],
|
|
328
|
-
address_type: 0,
|
|
329
|
-
scope: 0,
|
|
330
|
-
diversifier_index: 0,
|
|
331
|
-
show_display: false,
|
|
332
|
-
include_ufvk: true,
|
|
333
|
-
include_seed_fingerprint: true,
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
describe('Zcash address protocol selection', () => {
|
|
339
|
-
const createAddress = (params: Record<string, unknown> = {}) =>
|
|
340
|
-
new ZcashGetAddress({
|
|
341
|
-
id: 1,
|
|
342
|
-
payload: { method: 'zcashGetAddress', path: "m/32'/133'/0'", ...params },
|
|
343
|
-
});
|
|
344
|
-
test('supports only Pro2 and protocol V2', () => {
|
|
345
|
-
for (const method of [createAddress(), createSignMethod(hex(1, 1))]) {
|
|
346
|
-
method.init();
|
|
347
|
-
expect(method.supportsProtocol('V1')).toBe(false);
|
|
348
|
-
expect(method.supportsProtocol('V2')).toBe(true);
|
|
349
|
-
expect(method.strictCheckDeviceSupport).toBe(true);
|
|
350
|
-
expect(Object.keys(method.getVersionRange())).toEqual(['model_pro2']);
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
test.each([0, 1, 2])('maps address type %s with internal scope and index', async addressType => {
|
|
354
|
-
const method = createAddress({ addressType, scope: 1, diversifierIndex: 37 });
|
|
355
|
-
method.init();
|
|
356
|
-
const typedCall = jest.fn().mockResolvedValue({ message: { address: 'device-address' } });
|
|
357
|
-
attachDevice(method, typedCall);
|
|
358
|
-
await method.run();
|
|
359
|
-
expect(typedCall).toHaveBeenCalledWith(
|
|
360
|
-
'ZcashGetAddress',
|
|
361
|
-
'ZcashAddress',
|
|
362
|
-
expect.objectContaining({
|
|
363
|
-
address_type: addressType,
|
|
364
|
-
scope: 1,
|
|
365
|
-
diversifier_index: 37,
|
|
366
|
-
show_display: true,
|
|
367
|
-
})
|
|
368
|
-
);
|
|
369
|
-
});
|
|
370
|
-
test.each([
|
|
371
|
-
{ addressType: 3 },
|
|
372
|
-
{ scope: 2 },
|
|
373
|
-
{ diversifierIndex: -1 },
|
|
374
|
-
{ diversifierIndex: 1.5 },
|
|
375
|
-
{ diversifierIndex: 0x80000000 },
|
|
376
|
-
{ addressType: 2, diversifierIndex: 0x100000000 },
|
|
377
|
-
{ path: "m/44'/133'/0'" },
|
|
378
|
-
{ path: "m/32'/133'/0'/0/0" },
|
|
379
|
-
])('rejects invalid address requests %j', params => {
|
|
380
|
-
expect(() => createAddress(params).init()).toThrow();
|
|
381
|
-
});
|
|
382
|
-
test('accepts the uint32 limit only for an Orchard-only address', () => {
|
|
383
|
-
expect(() =>
|
|
384
|
-
createAddress({ addressType: 2, diversifierIndex: 0xffffffff }).init()
|
|
385
|
-
).not.toThrow();
|
|
386
|
-
});
|
|
387
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { BaseMethod } from '../BaseMethod';
|
|
2
|
-
import type { ZcashGetAddress as HardwareZcashGetAddress } from '@onekeyfe/hd-transport';
|
|
3
|
-
import type { ZcashAddress } from '../../types/api/zcashGetAddress';
|
|
4
|
-
import type { DeviceFirmwareRange } from '../../types';
|
|
5
|
-
export default class ZcashGetAddress extends BaseMethod<HardwareZcashGetAddress[]> {
|
|
6
|
-
hasBundle: boolean;
|
|
7
|
-
getSupportedProtocols(): readonly ["V2"];
|
|
8
|
-
init(): void;
|
|
9
|
-
getVersionRange(): DeviceFirmwareRange;
|
|
10
|
-
run(): Promise<ZcashAddress | ZcashAddress[]>;
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=ZcashGetAddress.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ZcashGetAddress.d.ts","sourceRoot":"","sources":["../../../src/api/zcash/ZcashGetAddress.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAK3C,OAAO,KAAK,EAAE,eAAe,IAAI,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACzF,OAAO,KAAK,EAAyB,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC3F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEvD,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,UAAU,CAAC,uBAAuB,EAAE,CAAC;IAChF,SAAS,UAAS;IAElB,qBAAqB;IAIrB,IAAI;IAgEJ,eAAe,IAAI,mBAAmB;IAQhC,GAAG;CA8BV"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { BaseMethod } from '../BaseMethod';
|
|
3
|
-
import type { ZcashSignPczt as HardwareZcashSignPczt } from '@onekeyfe/hd-transport';
|
|
4
|
-
import type { TypedCall, TypedResponseMessage } from '../../device/DeviceCommands';
|
|
5
|
-
import type { ZcashSignedPczt } from '../../types/api/zcashSignPczt';
|
|
6
|
-
import type { DeviceFirmwareRange } from '../../types';
|
|
7
|
-
export default class ZcashSignPczt extends BaseMethod<HardwareZcashSignPczt> {
|
|
8
|
-
pczt: Buffer;
|
|
9
|
-
getSupportedProtocols(): readonly ["V2"];
|
|
10
|
-
init(): void;
|
|
11
|
-
getVersionRange(): DeviceFirmwareRange;
|
|
12
|
-
uploadPczt: (typedCall: TypedCall, res: TypedResponseMessage<'ZcashSignedPczt'> | TypedResponseMessage<'ZcashPcztChunkRequest'>, offset: number) => Promise<TypedResponseMessage<'ZcashSignedPczt'>>;
|
|
13
|
-
downloadSignedPczt: (typedCall: TypedCall, res: TypedResponseMessage<'ZcashSignedPczt'>) => Promise<Buffer>;
|
|
14
|
-
run(): Promise<ZcashSignedPczt>;
|
|
15
|
-
}
|
|
16
|
-
//# sourceMappingURL=ZcashSignPczt.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ZcashSignPczt.d.ts","sourceRoot":"","sources":["../../../src/api/zcash/ZcashSignPczt.ts"],"names":[],"mappings":";AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAK3C,OAAO,KAAK,EAAE,aAAa,IAAI,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAMvD,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,UAAU,CAAC,qBAAqB,CAAC;IAC1E,IAAI,EAAE,MAAM,CAAmB;IAE/B,qBAAqB;IAIrB,IAAI;IAuBJ,eAAe,IAAI,mBAAmB;IAUtC,UAAU,cACG,SAAS,OACf,qBAAqB,iBAAiB,CAAC,GAAG,qBAAqB,uBAAuB,CAAC,UACpF,MAAM,KACb,QAAQ,qBAAqB,iBAAiB,CAAC,CAAC,CAiCjD;IAIF,kBAAkB,cACL,SAAS,OACf,qBAAqB,iBAAiB,CAAC,KAC3C,QAAQ,MAAM,CAAC,CAmChB;IAEI,GAAG,IAAI,OAAO,CAAC,eAAe,CAAC;CAgBtC"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { ZcashAddressScope, ZcashAddressType } from '@onekeyfe/hd-transport';
|
|
2
|
-
import type { CommonParams, Response } from '../params';
|
|
3
|
-
export type ZcashAddress = {
|
|
4
|
-
path: string;
|
|
5
|
-
address: string;
|
|
6
|
-
ufvk?: string;
|
|
7
|
-
seedFingerprint?: string;
|
|
8
|
-
};
|
|
9
|
-
export type ZcashGetAddressParams = {
|
|
10
|
-
path: string | number[];
|
|
11
|
-
showOnOneKey?: boolean;
|
|
12
|
-
diversifierIndex?: number;
|
|
13
|
-
addressType?: ZcashAddressType;
|
|
14
|
-
scope?: ZcashAddressScope;
|
|
15
|
-
includeUfvk?: boolean;
|
|
16
|
-
includeSeedFingerprint?: boolean;
|
|
17
|
-
};
|
|
18
|
-
export declare function zcashGetAddress(connectId: string, deviceId: string, params: CommonParams & ZcashGetAddressParams): Response<ZcashAddress>;
|
|
19
|
-
export declare function zcashGetAddress(connectId: string, deviceId: string, params: CommonParams & {
|
|
20
|
-
bundle?: ZcashGetAddressParams[];
|
|
21
|
-
}): Response<Array<ZcashAddress>>;
|
|
22
|
-
//# sourceMappingURL=zcashGetAddress.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"zcashGetAddress.d.ts","sourceRoot":"","sources":["../../../src/types/api/zcashGetAddress.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAElF,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAExD,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IAEb,OAAO,EAAE,MAAM,CAAC;IAEhB,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAElC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAC/B,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG,qBAAqB,GAC3C,QAAQ,CAAC,YAAY,CAAC,CAAC;AAE1B,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG;IAAE,MAAM,CAAC,EAAE,qBAAqB,EAAE,CAAA;CAAE,GAC1D,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { CommonParams, Response } from '../params';
|
|
2
|
-
export type ZcashSignPcztParams = {
|
|
3
|
-
pczt: string;
|
|
4
|
-
};
|
|
5
|
-
export type ZcashSignedPczt = {
|
|
6
|
-
pczt: string;
|
|
7
|
-
};
|
|
8
|
-
export declare function zcashSignPczt(connectId: string, deviceId: string, params: CommonParams & ZcashSignPcztParams): Response<ZcashSignedPczt>;
|
|
9
|
-
//# sourceMappingURL=zcashSignPczt.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"zcashSignPczt.d.ts","sourceRoot":"","sources":["../../../src/types/api/zcashSignPczt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAExD,MAAM,MAAM,mBAAmB,GAAG;IAGhC,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAE5B,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG,mBAAmB,GACzC,QAAQ,CAAC,eAAe,CAAC,CAAC"}
|