@bitboxswiss/bitbox-api 0.1.0 → 0.1.1

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.
Files changed (157) hide show
  1. package/dist/index.d.ts +334 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +451 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/internal/connect-simulator.d.ts +2 -0
  6. package/dist/internal/connect-simulator.d.ts.map +1 -0
  7. package/dist/internal/connect-simulator.js +25 -0
  8. package/dist/internal/connect-simulator.js.map +1 -0
  9. package/dist/internal/connect.d.ts +2 -0
  10. package/dist/internal/connect.d.ts.map +1 -0
  11. package/dist/internal/connect.js +54 -0
  12. package/dist/internal/connect.js.map +1 -0
  13. package/dist/internal/constants.d.ts +2 -0
  14. package/dist/internal/constants.d.ts.map +1 -0
  15. package/dist/internal/constants.js +16 -0
  16. package/dist/internal/constants.js.map +1 -0
  17. package/dist/internal/device.d.ts +5 -0
  18. package/dist/internal/device.d.ts.map +1 -0
  19. package/dist/internal/device.js +40 -0
  20. package/dist/internal/device.js.map +1 -0
  21. package/dist/internal/errors.d.ts +2 -0
  22. package/dist/internal/errors.d.ts.map +1 -0
  23. package/dist/internal/errors.js +220 -0
  24. package/dist/internal/errors.js.map +1 -0
  25. package/dist/internal/eth/antiklepto.d.ts +5 -0
  26. package/dist/internal/eth/antiklepto.d.ts.map +1 -0
  27. package/dist/internal/eth/antiklepto.js +59 -0
  28. package/dist/internal/eth/antiklepto.js.map +1 -0
  29. package/dist/internal/eth/eip712.d.ts +25 -0
  30. package/dist/internal/eth/eip712.d.ts.map +1 -0
  31. package/dist/internal/eth/eip712.js +303 -0
  32. package/dist/internal/eth/eip712.js.map +1 -0
  33. package/dist/internal/eth/keypath.d.ts +4 -0
  34. package/dist/internal/eth/keypath.d.ts.map +1 -0
  35. package/dist/internal/eth/keypath.js +47 -0
  36. package/dist/internal/eth/keypath.js.map +1 -0
  37. package/dist/internal/eth/methods.d.ts +10 -0
  38. package/dist/internal/eth/methods.d.ts.map +1 -0
  39. package/dist/internal/eth/methods.js +250 -0
  40. package/dist/internal/eth/methods.js.map +1 -0
  41. package/dist/internal/eth/query.d.ts +7 -0
  42. package/dist/internal/eth/query.d.ts.map +1 -0
  43. package/dist/internal/eth/query.js +43 -0
  44. package/dist/internal/eth/query.js.map +1 -0
  45. package/dist/internal/eth/streaming.d.ts +4 -0
  46. package/dist/internal/eth/streaming.d.ts.map +1 -0
  47. package/dist/internal/eth/streaming.js +21 -0
  48. package/dist/internal/eth/streaming.js.map +1 -0
  49. package/dist/internal/eth/version.d.ts +9 -0
  50. package/dist/internal/eth/version.d.ts.map +1 -0
  51. package/dist/internal/eth/version.js +10 -0
  52. package/dist/internal/eth/version.js.map +1 -0
  53. package/dist/internal/hww.d.ts +2 -0
  54. package/dist/internal/hww.d.ts.map +1 -0
  55. package/dist/internal/hww.js +236 -0
  56. package/dist/internal/hww.js.map +1 -0
  57. package/dist/internal/message-queue.d.ts +2 -0
  58. package/dist/internal/message-queue.d.ts.map +1 -0
  59. package/dist/internal/message-queue.js +29 -0
  60. package/dist/internal/message-queue.js.map +1 -0
  61. package/dist/internal/noise-config.d.ts +2 -0
  62. package/dist/internal/noise-config.d.ts.map +1 -0
  63. package/dist/internal/noise-config.js +160 -0
  64. package/dist/internal/noise-config.js.map +1 -0
  65. package/dist/internal/noise.d.ts +2 -0
  66. package/dist/internal/noise.d.ts.map +1 -0
  67. package/dist/internal/noise.js +270 -0
  68. package/dist/internal/noise.js.map +1 -0
  69. package/dist/internal/pairing.d.ts +2 -0
  70. package/dist/internal/pairing.d.ts.map +1 -0
  71. package/dist/internal/pairing.js +130 -0
  72. package/dist/internal/pairing.js.map +1 -0
  73. package/dist/internal/read-write.d.ts +2 -0
  74. package/dist/internal/read-write.d.ts.map +1 -0
  75. package/dist/internal/read-write.js +40 -0
  76. package/dist/internal/read-write.js.map +1 -0
  77. package/dist/internal/restore.d.ts +3 -0
  78. package/dist/internal/restore.d.ts.map +1 -0
  79. package/dist/internal/restore.js +27 -0
  80. package/dist/internal/restore.js.map +1 -0
  81. package/dist/internal/transport-bridge.d.ts +2 -0
  82. package/dist/internal/transport-bridge.d.ts.map +1 -0
  83. package/dist/internal/transport-bridge.js +97 -0
  84. package/dist/internal/transport-bridge.js.map +1 -0
  85. package/dist/internal/transport-simulator.d.ts +2 -0
  86. package/dist/internal/transport-simulator.d.ts.map +1 -0
  87. package/dist/internal/transport-simulator.js +83 -0
  88. package/dist/internal/transport-simulator.js.map +1 -0
  89. package/dist/internal/transport-webhid.d.ts +2 -0
  90. package/dist/internal/transport-webhid.d.ts.map +1 -0
  91. package/dist/internal/transport-webhid.js +69 -0
  92. package/dist/internal/transport-webhid.js.map +1 -0
  93. package/dist/internal/u2f-framing.d.ts +2 -0
  94. package/dist/internal/u2f-framing.d.ts.map +1 -0
  95. package/dist/internal/u2f-framing.js +155 -0
  96. package/dist/internal/u2f-framing.js.map +1 -0
  97. package/dist/internal/utils.d.ts +2 -0
  98. package/dist/internal/utils.d.ts.map +1 -0
  99. package/dist/internal/utils.js +71 -0
  100. package/dist/internal/utils.js.map +1 -0
  101. package/dist/proto/gen/antiklepto_pb.d.ts +49 -0
  102. package/dist/proto/gen/antiklepto_pb.d.ts.map +1 -0
  103. package/dist/proto/gen/antiklepto_pb.js +22 -0
  104. package/dist/proto/gen/antiklepto_pb.js.map +1 -0
  105. package/dist/proto/gen/backup_commands_pb.d.ts +124 -0
  106. package/dist/proto/gen/backup_commands_pb.d.ts.map +1 -0
  107. package/dist/proto/gen/backup_commands_pb.js +42 -0
  108. package/dist/proto/gen/backup_commands_pb.js.map +1 -0
  109. package/dist/proto/gen/backup_pb.d.ts +150 -0
  110. package/dist/proto/gen/backup_pb.d.ts.map +1 -0
  111. package/dist/proto/gen/backup_pb.js +46 -0
  112. package/dist/proto/gen/backup_pb.js.map +1 -0
  113. package/dist/proto/gen/bitbox02_system_pb.d.ts +207 -0
  114. package/dist/proto/gen/bitbox02_system_pb.d.ts.map +1 -0
  115. package/dist/proto/gen/bitbox02_system_pb.js +80 -0
  116. package/dist/proto/gen/bitbox02_system_pb.js.map +1 -0
  117. package/dist/proto/gen/bluetooth_pb.d.ts +135 -0
  118. package/dist/proto/gen/bluetooth_pb.d.ts.map +1 -0
  119. package/dist/proto/gen/bluetooth_pb.js +42 -0
  120. package/dist/proto/gen/bluetooth_pb.js.map +1 -0
  121. package/dist/proto/gen/btc_pb.d.ts +1166 -0
  122. package/dist/proto/gen/btc_pb.d.ts.map +1 -0
  123. package/dist/proto/gen/btc_pb.js +442 -0
  124. package/dist/proto/gen/btc_pb.js.map +1 -0
  125. package/dist/proto/gen/cardano_pb.d.ts +504 -0
  126. package/dist/proto/gen/cardano_pb.d.ts.map +1 -0
  127. package/dist/proto/gen/cardano_pb.js +142 -0
  128. package/dist/proto/gen/cardano_pb.js.map +1 -0
  129. package/dist/proto/gen/common_pb.d.ts +129 -0
  130. package/dist/proto/gen/common_pb.d.ts.map +1 -0
  131. package/dist/proto/gen/common_pb.js +42 -0
  132. package/dist/proto/gen/common_pb.js.map +1 -0
  133. package/dist/proto/gen/eth_pb.d.ts +662 -0
  134. package/dist/proto/gen/eth_pb.d.ts.map +1 -0
  135. package/dist/proto/gen/eth_pb.js +221 -0
  136. package/dist/proto/gen/eth_pb.js.map +1 -0
  137. package/dist/proto/gen/hww_pb.d.ts +349 -0
  138. package/dist/proto/gen/hww_pb.d.ts.map +1 -0
  139. package/dist/proto/gen/hww_pb.js +38 -0
  140. package/dist/proto/gen/hww_pb.js.map +1 -0
  141. package/dist/proto/gen/keystore_pb.d.ts +108 -0
  142. package/dist/proto/gen/keystore_pb.d.ts.map +1 -0
  143. package/dist/proto/gen/keystore_pb.js +33 -0
  144. package/dist/proto/gen/keystore_pb.js.map +1 -0
  145. package/dist/proto/gen/mnemonic_pb.d.ts +48 -0
  146. package/dist/proto/gen/mnemonic_pb.d.ts.map +1 -0
  147. package/dist/proto/gen/mnemonic_pb.js +22 -0
  148. package/dist/proto/gen/mnemonic_pb.js.map +1 -0
  149. package/dist/proto/gen/perform_attestation_pb.d.ts +57 -0
  150. package/dist/proto/gen/perform_attestation_pb.d.ts.map +1 -0
  151. package/dist/proto/gen/perform_attestation_pb.js +17 -0
  152. package/dist/proto/gen/perform_attestation_pb.js.map +1 -0
  153. package/dist/proto/gen/system_pb.d.ts +38 -0
  154. package/dist/proto/gen/system_pb.d.ts.map +1 -0
  155. package/dist/proto/gen/system_pb.js +30 -0
  156. package/dist/proto/gen/system_pb.js.map +1 -0
  157. package/package.json +2 -1
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/internal/errors.ts"],"names":[],"mappings":""}
@@ -0,0 +1,220 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // JS-side connection, parsing, and input-shape errors.
3
+ /** @internal */ export const CODE_UNKNOWN_JS = 'unknown-js';
4
+ /** @internal */ export const CODE_COULD_NOT_OPEN = 'could-not-open';
5
+ /** @internal */ export const CODE_USER_ABORT = 'user-abort';
6
+ /** @internal */ export const CODE_INVALID_TYPE = 'invalid-type';
7
+ /** @internal */ export const CODE_PSBT_PARSE = 'psbt-parse';
8
+ /** @internal */ export const CODE_CHAIN_ID_TOO_LARGE = 'chain-id-too-large';
9
+ // Core API errors shared by pairing, transport, and method implementations.
10
+ /** @internal */ export const CODE_UNKNOWN = 'unknown';
11
+ /** @internal */ export const CODE_VERSION = 'version';
12
+ /** @internal */ export const CODE_COMMUNICATION = 'communication';
13
+ /** @internal */ export const CODE_NOISE = 'noise';
14
+ /** @internal */ export const CODE_NOISE_CONFIG = 'noise-config';
15
+ /** @internal */ export const CODE_PAIRING_REJECTED = 'pairing-rejected';
16
+ /** @internal */ export const CODE_UNEXPECTED_RESPONSE = 'unexpected-response';
17
+ /** @internal */ export const CODE_PROTOBUF_DECODE = 'protobuf-decode';
18
+ /** @internal */ export const CODE_KEYPATH_PARSE = 'keypath-parse';
19
+ /** @internal */ export const CODE_ANTIKLEPTO = 'antiklepto';
20
+ /** @internal */ export const CODE_ETH_TYPED_MESSAGE = 'eth-typed-message';
21
+ /** @internal */ export const CODE_BTC_SIGN = 'btc-sign';
22
+ // Device-origin errors reported through protobuf responses.
23
+ /** @internal */ export const CODE_BITBOX_UNKNOWN = 'bitbox-unknown';
24
+ /** @internal */ export const CODE_BITBOX_INVALID_INPUT = 'bitbox-invalid-input';
25
+ /** @internal */ export const CODE_BITBOX_MEMORY = 'bitbox-memory';
26
+ /** @internal */ export const CODE_BITBOX_GENERIC = 'bitbox-generic';
27
+ /** @internal */ export const CODE_BITBOX_USER_ABORT = 'bitbox-user-abort';
28
+ /** @internal */ export const CODE_BITBOX_INVALID_STATE = 'bitbox-invalid-state';
29
+ /** @internal */ export const CODE_BITBOX_DISABLED = 'bitbox-disabled';
30
+ /** @internal */ export const CODE_BITBOX_DUPLICATE = 'bitbox-duplicate';
31
+ /** @internal */ export const CODE_BITBOX_NOISE_ENCRYPT = 'bitbox-noise-encrypt';
32
+ /** @internal */ export const CODE_BITBOX_NOISE_DECRYPT = 'bitbox-noise-decrypt';
33
+ // PSBT error codes kept here for the public error surface, even before BTC support lands.
34
+ /** @internal */ export const CODE_PSBT_SIGN_ERROR = 'psbt-sign-error';
35
+ /** @internal */ export const CODE_PSBT_KEY_NOT_UNIQUE = 'psbt-key-not-unique';
36
+ /** @internal */ export const CODE_PSBT_KEY_NOT_FOUND = 'psbt-key-not-found';
37
+ /** @internal */ export const CODE_PSBT_UNKNOWN_OUTPUT_TYPE = 'psbt-unknown-output-type';
38
+ /** @internal */ export const CODE_PSBT_INVALID_OP_RETURN = 'psbt-invalid-op-return';
39
+ // TS compatibility codes for methods or lifecycle states without a device roundtrip.
40
+ /** @internal */ export const CODE_UNSUPPORTED = 'unsupported';
41
+ /** @internal */ export const CODE_NOT_IMPLEMENTED = 'not-implemented';
42
+ /** @internal */ export const CODE_INVALID_STATE = 'invalid-state';
43
+ // Device protobuf messages are ignored; the numeric code selects the stable public message.
44
+ /** @internal */
45
+ export const DEVICE_ERROR_TABLE = {
46
+ 101: { code: CODE_BITBOX_INVALID_INPUT, message: 'bitbox error: invalid input' },
47
+ 102: { code: CODE_BITBOX_MEMORY, message: 'bitbox error: memory' },
48
+ 103: { code: CODE_BITBOX_GENERIC, message: 'bitbox error: generic error' },
49
+ 104: { code: CODE_BITBOX_USER_ABORT, message: 'bitbox error: aborted by the user' },
50
+ 105: { code: CODE_BITBOX_INVALID_STATE, message: "bitbox error: can't call this endpoint: wrong state" },
51
+ 106: { code: CODE_BITBOX_DISABLED, message: 'bitbox error: function disabled' },
52
+ 107: { code: CODE_BITBOX_DUPLICATE, message: 'bitbox error: duplicate entry' },
53
+ 108: { code: CODE_BITBOX_NOISE_ENCRYPT, message: 'bitbox error: noise encryption failed' },
54
+ 109: { code: CODE_BITBOX_NOISE_DECRYPT, message: 'bitbox error: noise decryption failed' },
55
+ };
56
+ /** @internal */
57
+ export const DEVICE_UNKNOWN = {
58
+ code: CODE_BITBOX_UNKNOWN,
59
+ message: 'bitbox error: error code not recognized',
60
+ };
61
+ /** @internal */
62
+ export function deviceErrorFor(protobufCode) {
63
+ return DEVICE_ERROR_TABLE[protobufCode] ?? DEVICE_UNKNOWN;
64
+ }
65
+ // Factories for public messages with runtime details.
66
+ /** @internal */
67
+ export function makeError(code, message) {
68
+ const err = new Error(message);
69
+ // Error.message is non-enumerable by default, so JSON serialization and
70
+ // Chrome extension message boundaries would otherwise drop it. Keep typed
71
+ // errors Error-like while preserving the public { code, message } shape.
72
+ Object.defineProperty(err, 'message', {
73
+ value: message,
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ });
78
+ err.code = code;
79
+ return err;
80
+ }
81
+ /** @internal */
82
+ export function versionError(requirement) {
83
+ return makeError(CODE_VERSION, `firmware version ${requirement} required`);
84
+ }
85
+ /** @internal */
86
+ export function communicationError(detail) {
87
+ return makeError(CODE_COMMUNICATION, `communication error: ${detail}`);
88
+ }
89
+ /** @internal */
90
+ export function noiseConfigError(detail) {
91
+ return makeError(CODE_NOISE_CONFIG, `noise config error: ${detail}`);
92
+ }
93
+ /** @internal */
94
+ export function keypathParseError(input) {
95
+ return makeError(CODE_KEYPATH_PARSE, `failed parsing keypath: ${input}`);
96
+ }
97
+ /** @internal */
98
+ export function antikleptoError(detail) {
99
+ return makeError(CODE_ANTIKLEPTO, `Antiklepto verification failed: ${detail}`);
100
+ }
101
+ /** @internal */
102
+ export function ethTypedMessageError(detail) {
103
+ return makeError(CODE_ETH_TYPED_MESSAGE, `EIP-712 typed message processing error: ${detail}`);
104
+ }
105
+ /** @internal */
106
+ export function chainIdTooLargeError(chainId) {
107
+ return makeError(CODE_CHAIN_ID_TOO_LARGE, `Chain ID too large and would overflow in the computation of the \`v\` signature value: ${chainId}`);
108
+ }
109
+ /** @internal */
110
+ export function invalidTypeError(detail) {
111
+ return makeError(CODE_INVALID_TYPE, `invalid JavaScript type: ${detail}`);
112
+ }
113
+ /** @internal */
114
+ export function couldNotOpenWebHID() {
115
+ return makeError(CODE_COULD_NOT_OPEN, 'Could not open device. It might already have an open connection to another app. If so, please close the other app first.');
116
+ }
117
+ /** @internal */
118
+ export function couldNotOpenBridge(detail) {
119
+ return makeError(CODE_COULD_NOT_OPEN, `Could not open device. ${detail}`);
120
+ }
121
+ /** @internal */
122
+ export function userAbortConnect() {
123
+ return makeError(CODE_USER_ABORT, 'connection aborted by user');
124
+ }
125
+ /** @internal */
126
+ export function unknownJs(err) {
127
+ const e = makeError(CODE_UNKNOWN_JS, 'Unknown Javascript error');
128
+ e.err = err;
129
+ return e;
130
+ }
131
+ // Codes that can already cross the public boundary unchanged.
132
+ const TYPED_PUBLIC_CODES = new Set([
133
+ CODE_UNKNOWN_JS,
134
+ CODE_COULD_NOT_OPEN,
135
+ CODE_USER_ABORT,
136
+ CODE_INVALID_TYPE,
137
+ CODE_PSBT_PARSE,
138
+ CODE_CHAIN_ID_TOO_LARGE,
139
+ CODE_UNKNOWN,
140
+ CODE_VERSION,
141
+ CODE_COMMUNICATION,
142
+ CODE_NOISE_CONFIG,
143
+ CODE_PROTOBUF_DECODE,
144
+ CODE_KEYPATH_PARSE,
145
+ CODE_ANTIKLEPTO,
146
+ CODE_ETH_TYPED_MESSAGE,
147
+ CODE_BTC_SIGN,
148
+ CODE_BITBOX_UNKNOWN,
149
+ CODE_BITBOX_INVALID_INPUT,
150
+ CODE_BITBOX_MEMORY,
151
+ CODE_BITBOX_GENERIC,
152
+ CODE_BITBOX_USER_ABORT,
153
+ CODE_BITBOX_INVALID_STATE,
154
+ CODE_BITBOX_DISABLED,
155
+ CODE_BITBOX_DUPLICATE,
156
+ CODE_BITBOX_NOISE_ENCRYPT,
157
+ CODE_BITBOX_NOISE_DECRYPT,
158
+ CODE_PSBT_SIGN_ERROR,
159
+ CODE_PSBT_KEY_NOT_UNIQUE,
160
+ CODE_PSBT_KEY_NOT_FOUND,
161
+ CODE_PSBT_UNKNOWN_OUTPUT_TYPE,
162
+ CODE_PSBT_INVALID_OP_RETURN,
163
+ CODE_UNSUPPORTED,
164
+ CODE_NOT_IMPLEMENTED,
165
+ CODE_INVALID_STATE,
166
+ ]);
167
+ function isTypedShape(err) {
168
+ return (err !== null &&
169
+ typeof err === 'object' &&
170
+ typeof err.code === 'string' &&
171
+ typeof err.message === 'string');
172
+ }
173
+ // Public API entry points use this to collapse internal transport/protocol details.
174
+ /** @internal */
175
+ export function toPublicError(err) {
176
+ if (!isTypedShape(err)) {
177
+ return unknownJs(err);
178
+ }
179
+ const { code, message } = err;
180
+ if (code === CODE_NOISE) {
181
+ return makeError(CODE_NOISE, 'noise channel error');
182
+ }
183
+ if (code === CODE_UNEXPECTED_RESPONSE) {
184
+ return makeError(CODE_UNEXPECTED_RESPONSE, 'BitBox returned an unexpected response');
185
+ }
186
+ if (code === CODE_PAIRING_REJECTED) {
187
+ return makeError(CODE_PAIRING_REJECTED, 'pairing code rejected by user');
188
+ }
189
+ if (TYPED_PUBLIC_CODES.has(code)) {
190
+ return err;
191
+ }
192
+ switch (code) {
193
+ case 'webhid-cancel':
194
+ return userAbortConnect();
195
+ case 'webhid':
196
+ case 'simulator':
197
+ return couldNotOpenWebHID();
198
+ case 'bridge':
199
+ return couldNotOpenBridge(message);
200
+ case 'write':
201
+ return communicationError('write error');
202
+ case 'read':
203
+ return communicationError('read error');
204
+ case 'u2f-decode':
205
+ return communicationError('u2f framing decoding error');
206
+ case 'info':
207
+ return communicationError('error querying device info');
208
+ case 'noise-pairing-rejected':
209
+ return makeError(CODE_PAIRING_REJECTED, 'pairing code rejected by user');
210
+ }
211
+ return unknownJs(err);
212
+ }
213
+ /** @internal */
214
+ export function ensureTyped(err) {
215
+ if (isTypedShape(err)) {
216
+ return err;
217
+ }
218
+ return unknownJs(err);
219
+ }
220
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/internal/errors.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAStC,uDAAuD;AACvD,gBAAgB,CAAC,MAAM,CAAC,MAAM,eAAe,GAAG,YAAY,CAAC;AAC7D,gBAAgB,CAAC,MAAM,CAAC,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AACrE,gBAAgB,CAAC,MAAM,CAAC,MAAM,eAAe,GAAG,YAAY,CAAC;AAC7D,gBAAgB,CAAC,MAAM,CAAC,MAAM,iBAAiB,GAAG,cAAc,CAAC;AACjE,gBAAgB,CAAC,MAAM,CAAC,MAAM,eAAe,GAAG,YAAY,CAAC;AAC7D,gBAAgB,CAAC,MAAM,CAAC,MAAM,uBAAuB,GAAG,oBAAoB,CAAC;AAE7E,4EAA4E;AAC5E,gBAAgB,CAAC,MAAM,CAAC,MAAM,YAAY,GAAG,SAAS,CAAC;AACvD,gBAAgB,CAAC,MAAM,CAAC,MAAM,YAAY,GAAG,SAAS,CAAC;AACvD,gBAA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C,CAAC,CAAC;IACvF,CAAC;IACD,IAAI,IAAI,KAAK,qBAAqB,EAAE,CAAC;QACnC,OAAO,SAAS,CAAC,qBAAqB,EAAE,+BAA+B,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC;IACb,CAAC;IAED,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,eAAe;YAClB,OAAO,gBAAgB,EAAE,CAAC;QAC5B,KAAK,QAAQ,CAAC;QACd,KAAK,WAAW;YACd,OAAO,kBAAkB,EAAE,CAAC;QAC9B,KAAK,QAAQ;YACX,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAC;QACrC,KAAK,OAAO;YACV,OAAO,kBAAkB,CAAC,aAAa,CAAC,CAAC;QAC3C,KAAK,MAAM;YACT,OAAO,kBAAkB,CAAC,YAAY,CAAC,CAAC;QAC1C,KAAK,YAAY;YACf,OAAO,kBAAkB,CAAC,4BAA4B,CAAC,CAAC;QAC1D,KAAK,MAAM;YACT,OAAO,kBAAkB,CAAC,4BAA4B,CAAC,CAAC;QAC1D,KAAK,wBAAwB;YAC3B,OAAO,SAAS,CAAC,qBAAqB,EAAE,+BAA+B,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,IAAI,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC;QACtB,OAAO,GAAG,CAAC;IACb,CAAC;IACD,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare function genHostNonce(): Uint8Array;
2
+ export declare function taggedSha256(tag: Uint8Array, msg: Uint8Array): Uint8Array;
3
+ export declare function hostCommit(hostNonce: Uint8Array): Uint8Array;
4
+ export declare function verifyEcdsa(hostNonce: Uint8Array, signerCommitment: Uint8Array, signature: Uint8Array): void;
5
+ //# sourceMappingURL=antiklepto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"antiklepto.d.ts","sourceRoot":"","sources":["../../../src/internal/eth/antiklepto.ts"],"names":[],"mappings":"AAYA,wBAAgB,YAAY,IAAI,UAAU,CAQzC;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,UAAU,CAGzE;AAED,wBAAgB,UAAU,CAAC,SAAS,EAAE,UAAU,GAAG,UAAU,CAE5D;AAED,wBAAgB,WAAW,CACzB,SAAS,EAAE,UAAU,EACrB,gBAAgB,EAAE,UAAU,EAC5B,SAAS,EAAE,UAAU,GACpB,IAAI,CA8BN"}
@@ -0,0 +1,59 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { secp256k1 } from '@noble/curves/secp256k1';
3
+ import { sha256 } from '@noble/hashes/sha256';
4
+ import { antikleptoError } from '../errors.js';
5
+ import { bytesToBigIntBE, concatBytes, utf8ToBytes } from '../utils.js';
6
+ const HOST_COMMIT_TAG = utf8ToBytes('s2c/ecdsa/data');
7
+ const POINT_TWEAK_TAG = utf8ToBytes('s2c/ecdsa/point');
8
+ const VERIFICATION_FAILED_MESSAGE = 'Could not verify that the host nonce was contributed to the signature. If this happens repeatedly, the device might be attempting to leak the seed through the signature.';
9
+ export function genHostNonce() {
10
+ const out = new Uint8Array(32);
11
+ try {
12
+ globalThis.crypto.getRandomValues(out);
13
+ }
14
+ catch {
15
+ throw antikleptoError('Failed generating antiklepto host nonce');
16
+ }
17
+ return out;
18
+ }
19
+ export function taggedSha256(tag, msg) {
20
+ const tagHash = sha256(tag);
21
+ return sha256(concatBytes(tagHash, tagHash, msg));
22
+ }
23
+ export function hostCommit(hostNonce) {
24
+ return taggedSha256(HOST_COMMIT_TAG, hostNonce);
25
+ }
26
+ export function verifyEcdsa(hostNonce, signerCommitment, signature) {
27
+ if (signature.length !== 65) {
28
+ throw antikleptoError('signature must be 65 bytes');
29
+ }
30
+ let point;
31
+ try {
32
+ point = secp256k1.ProjectivePoint.fromHex(signerCommitment);
33
+ }
34
+ catch {
35
+ throw antikleptoError('failed to parse signer commitment');
36
+ }
37
+ const compressed = point.toRawBytes(true);
38
+ const tweak = taggedSha256(POINT_TWEAK_TAG, concatBytes(compressed, hostNonce));
39
+ const tweakBig = bytesToBigIntBE(tweak);
40
+ if (tweakBig === 0n || tweakBig >= secp256k1.CURVE.n) {
41
+ throw antikleptoError('tweak is an invalid scalar');
42
+ }
43
+ let tweaked;
44
+ try {
45
+ tweaked = point.add(secp256k1.ProjectivePoint.BASE.multiply(tweakBig));
46
+ }
47
+ catch {
48
+ throw antikleptoError('failed to apply tweak');
49
+ }
50
+ const uncompressed = tweaked.toRawBytes(false);
51
+ const xCoord = uncompressed.subarray(1, 33);
52
+ const sigR = signature.subarray(0, 32);
53
+ for (let i = 0; i < 32; i += 1) {
54
+ if (xCoord[i] !== sigR[i]) {
55
+ throw antikleptoError(VERIFICATION_FAILED_MESSAGE);
56
+ }
57
+ }
58
+ }
59
+ //# sourceMappingURL=antiklepto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"antiklepto.js","sourceRoot":"","sources":["../../../src/internal/eth/antiklepto.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAEtC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAExE,MAAM,eAAe,GAAG,WAAW,CAAC,gBAAgB,CAAC,CAAC;AACtD,MAAM,eAAe,GAAG,WAAW,CAAC,iBAAiB,CAAC,CAAC;AACvD,MAAM,2BAA2B,GAC/B,2KAA2K,CAAC;AAE9K,MAAM,UAAU,YAAY;IAC1B,MAAM,G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AC;QACrD,MAAM,eAAe,CAAC,4BAA4B,CAAC,CAAC;IACtD,CAAC;IACD,IAAI,OAAO,CAAC;IACZ,IAAI,CAAC;QACH,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;IACzE,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,eAAe,CAAC,uBAAuB,CAAC,CAAC;IACjD,CAAC;IACD,MAAM,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YAC1B,MAAM,eAAe,CAAC,2BAA2B,CAAC,CAAC;QACrD,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { ETHSignTypedMessageRequest_DataType as DataType, type ETHSignTypedMessageRequest_MemberType, type ETHSignTypedMessageRequest_StructType, type ETHTypedMessageValueResponse } from '../../proto/gen/eth_pb.js';
2
+ export { DataType };
3
+ export declare class TypedMessageError extends Error {
4
+ readonly code = "eth-typed-message";
5
+ constructor(detail: string);
6
+ }
7
+ export interface Eip712TypeMember {
8
+ name: string;
9
+ type: string;
10
+ }
11
+ export interface Eip712Message {
12
+ types: Record<string, Eip712TypeMember[]>;
13
+ primaryType: string;
14
+ domain: Record<string, unknown>;
15
+ message: Record<string, unknown>;
16
+ }
17
+ export declare function parseEip712Message(input: unknown): Eip712Message;
18
+ export declare function parseType(typ: string, types: Record<string, Eip712TypeMember[]>): ETHSignTypedMessageRequest_MemberType;
19
+ export declare function buildStructTypes(types: Record<string, Eip712TypeMember[]>): ETHSignTypedMessageRequest_StructType[];
20
+ export declare function encodeValue(typ: ETHSignTypedMessageRequest_MemberType, value: unknown): Uint8Array;
21
+ export declare function getValue(what: ETHTypedMessageValueResponse, msg: Eip712Message): {
22
+ value: Uint8Array;
23
+ dataType: DataType;
24
+ };
25
+ //# sourceMappingURL=eip712.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eip712.d.ts","sourceRoot":"","sources":["../../../src/internal/eth/eip712.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,mCAAmC,IAAI,QAAQ,EAM/C,KAAK,qCAAqC,EAC1C,KAAK,qCAAqC,EAC1C,KAAK,4BAA4B,EAClC,MAAM,2BAA2B,CAAC;AASnC,OAAO,EAAE,QAAQ,EAAE,CAAC;AAEpB,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,IAAI,uBAAuB;gBACxB,MAAM,EAAE,MAAM;CAG3B;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,CAAC;IAC1C,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAyBD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAahE;AAgBD,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,GACxC,qCAAqC,CAgEvC;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,GACxC,qCAAqC,EAAE,CAazC;AA0DD,wBAAgB,WAAW,CACzB,GAAG,EAAE,qCAAqC,EAC1C,KAAK,EAAE,OAAO,GACb,UAAU,CAoDZ;AAED,wBAAgB,QAAQ,CACtB,IAAI,EAAE,4BAA4B,EAClC,GAAG,EAAE,aAAa,GACjB;IAAE,KAAK,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAkE3C"}
@@ -0,0 +1,303 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { create } from '@bufbuild/protobuf';
3
+ import { ETHSignTypedMessageRequest_DataType as DataType, ETHSignTypedMessageRequest_MemberSchema, ETHSignTypedMessageRequest_MemberTypeSchema, ETHSignTypedMessageRequest_StructTypeSchema, ETHTypedMessageValueResponse_RootObject as RootObject, } from '../../proto/gen/eth_pb.js';
4
+ import { ethTypedMessageError } from '../errors.js';
5
+ import { bigIntToSignedBytesBE, bigUintToBytesBE, hexToBytes, utf8ToBytes, } from '../utils.js';
6
+ export { DataType };
7
+ export class TypedMessageError extends Error {
8
+ code = 'eth-typed-message';
9
+ constructor(detail) {
10
+ super(ethTypedMessageError(detail).message);
11
+ }
12
+ }
13
+ function isRecord(value) {
14
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
15
+ }
16
+ function parseTypes(input) {
17
+ if (!isRecord(input)) {
18
+ throw new TypedMessageError('Could not parse EIP-712 JSON message');
19
+ }
20
+ const types = {};
21
+ for (const [name, members] of Object.entries(input)) {
22
+ if (!Array.isArray(members)) {
23
+ throw new TypedMessageError('Could not parse EIP-712 JSON message');
24
+ }
25
+ types[name] = members.map((member) => {
26
+ if (!isRecord(member) || typeof member.name !== 'string' || typeof member.type !== 'string') {
27
+ throw new TypedMessageError('Could not parse EIP-712 JSON message');
28
+ }
29
+ return { name: member.name, type: member.type };
30
+ });
31
+ }
32
+ return types;
33
+ }
34
+ export function parseEip712Message(input) {
35
+ if (!isRecord(input) || typeof input.primaryType !== 'string') {
36
+ throw new TypedMessageError('Could not parse EIP-712 JSON message');
37
+ }
38
+ if (!isRecord(input.domain) || !isRecord(input.message)) {
39
+ throw new TypedMessageError('Could not parse EIP-712 JSON message');
40
+ }
41
+ return {
42
+ types: parseTypes(input.types),
43
+ primaryType: input.primaryType,
44
+ domain: input.domain,
45
+ message: input.message,
46
+ };
47
+ }
48
+ function makeMemberType(init) {
49
+ return create(ETHSignTypedMessageRequest_MemberTypeSchema, {
50
+ type: init.type,
51
+ size: init.size ?? 0,
52
+ structName: init.structName ?? '',
53
+ ...(init.arrayType !== undefined ? { arrayType: init.arrayType } : {}),
54
+ });
55
+ }
56
+ export function parseType(typ, types) {
57
+ if (typ.endsWith(']')) {
58
+ const open = typ.lastIndexOf('[');
59
+ if (open < 0) {
60
+ throw new TypedMessageError(`Invalid type format: ${typ}`);
61
+ }
62
+ const inner = typ.slice(0, open);
63
+ const sizeStr = typ.slice(open + 1, typ.length - 1);
64
+ let size = 0;
65
+ if (sizeStr !== '') {
66
+ if (!/^\d+$/.test(sizeStr)) {
67
+ throw new TypedMessageError(`Error parsing size: ${sizeStr}`);
68
+ }
69
+ size = Number(sizeStr);
70
+ }
71
+ return makeMemberType({
72
+ type: DataType.ARRAY,
73
+ size,
74
+ arrayType: parseType(inner, types),
75
+ });
76
+ }
77
+ if (typ.startsWith('bytes') && typ !== 'bytes') {
78
+ const sizeStr = typ.slice('bytes'.length);
79
+ if (!/^\d+$/.test(sizeStr)) {
80
+ throw new TypedMessageError(`Error parsing size: ${sizeStr}`);
81
+ }
82
+ return makeMemberType({ type: DataType.BYTES, size: Number(sizeStr) });
83
+ }
84
+ if (typ === 'bytes') {
85
+ return makeMemberType({ type: DataType.BYTES, size: 0 });
86
+ }
87
+ if (typ.startsWith('uint')) {
88
+ const sizeStr = typ.slice('uint'.length);
89
+ if (sizeStr === '') {
90
+ throw new TypedMessageError('uint must be sized');
91
+ }
92
+ if (!/^\d+$/.test(sizeStr)) {
93
+ throw new TypedMessageError(`Error parsing size: ${sizeStr}`);
94
+ }
95
+ return makeMemberType({ type: DataType.UINT, size: Number(sizeStr) / 8 });
96
+ }
97
+ if (typ.startsWith('int')) {
98
+ const sizeStr = typ.slice('int'.length);
99
+ if (sizeStr === '') {
100
+ throw new TypedMessageError('int must be sized');
101
+ }
102
+ if (!/^\d+$/.test(sizeStr)) {
103
+ throw new TypedMessageError(`Error parsing size: ${sizeStr}`);
104
+ }
105
+ return makeMemberType({ type: DataType.INT, size: Number(sizeStr) / 8 });
106
+ }
107
+ if (typ === 'bool') {
108
+ return makeMemberType({ type: DataType.BOOL });
109
+ }
110
+ if (typ === 'address') {
111
+ return makeMemberType({ type: DataType.ADDRESS });
112
+ }
113
+ if (typ === 'string') {
114
+ return makeMemberType({ type: DataType.STRING });
115
+ }
116
+ if (Object.prototype.hasOwnProperty.call(types, typ)) {
117
+ return makeMemberType({ type: DataType.STRUCT, structName: typ });
118
+ }
119
+ throw new TypedMessageError(`Can't recognize type: ${typ}`);
120
+ }
121
+ export function buildStructTypes(types) {
122
+ return Object.entries(types).map(([name, members]) => {
123
+ const protoMembers = members.map((member) => create(ETHSignTypedMessageRequest_MemberSchema, {
124
+ name: member.name,
125
+ type: parseType(member.type, types),
126
+ }));
127
+ return create(ETHSignTypedMessageRequest_StructTypeSchema, {
128
+ name,
129
+ members: protoMembers,
130
+ });
131
+ });
132
+ }
133
+ function decodeHex(s) {
134
+ try {
135
+ return hexToBytes(s.slice(2));
136
+ }
137
+ catch {
138
+ throw new TypedMessageError(`invalid hex: ${s}`);
139
+ }
140
+ }
141
+ function parseUintValue(value) {
142
+ if (typeof value === 'string') {
143
+ if (value.startsWith('0x') || value.startsWith('0X')) {
144
+ try {
145
+ return BigInt(value);
146
+ }
147
+ catch {
148
+ throw new TypedMessageError(`could not parse ${value} as hex`);
149
+ }
150
+ }
151
+ if (!/^\d+$/.test(value)) {
152
+ throw new TypedMessageError(`could not parse ${value} as uint`);
153
+ }
154
+ return BigInt(value);
155
+ }
156
+ if (typeof value === 'number') {
157
+ if (!Number.isInteger(value) || value < 0) {
158
+ throw new TypedMessageError('Number is not an uint');
159
+ }
160
+ return BigInt(value);
161
+ }
162
+ if (typeof value === 'bigint') {
163
+ if (value < 0n) {
164
+ throw new TypedMessageError('Number is not an uint');
165
+ }
166
+ return value;
167
+ }
168
+ throw new TypedMessageError('Wrong type for uint');
169
+ }
170
+ function parseIntValue(value) {
171
+ if (typeof value === 'string') {
172
+ if (!/^-?\d+$/.test(value)) {
173
+ throw new TypedMessageError(`could not parse ${value} as int`);
174
+ }
175
+ return BigInt(value);
176
+ }
177
+ if (typeof value === 'number') {
178
+ if (!Number.isInteger(value)) {
179
+ throw new TypedMessageError('Number is not an int');
180
+ }
181
+ return BigInt(value);
182
+ }
183
+ if (typeof value === 'bigint') {
184
+ return value;
185
+ }
186
+ throw new TypedMessageError('Wrong type for int');
187
+ }
188
+ export function encodeValue(typ, value) {
189
+ switch (typ.type) {
190
+ case DataType.BYTES: {
191
+ if (typeof value !== 'string') {
192
+ throw new TypedMessageError('Expected a string for bytes type');
193
+ }
194
+ if (value.startsWith('0x') || value.startsWith('0X')) {
195
+ return decodeHex(value);
196
+ }
197
+ return utf8ToBytes(value);
198
+ }
199
+ case DataType.UINT: {
200
+ const n = parseUintValue(value);
201
+ return bigUintToBytesBE(n);
202
+ }
203
+ case DataType.INT: {
204
+ const n = parseIntValue(value);
205
+ if (typeof value === 'number') {
206
+ const bytes = bigIntToSignedBytesBE(n);
207
+ if (bytes.length > 0 && bytes[0] === 0x00) {
208
+ return bytes.subarray(1);
209
+ }
210
+ return bytes;
211
+ }
212
+ return bigIntToSignedBytesBE(n);
213
+ }
214
+ case DataType.BOOL: {
215
+ if (typeof value !== 'boolean') {
216
+ throw new TypedMessageError('Expected a boolean value');
217
+ }
218
+ return new Uint8Array([value ? 1 : 0]);
219
+ }
220
+ case DataType.ADDRESS:
221
+ case DataType.STRING: {
222
+ if (typeof value !== 'string') {
223
+ throw new TypedMessageError('Expected a string value');
224
+ }
225
+ return utf8ToBytes(value);
226
+ }
227
+ case DataType.ARRAY: {
228
+ if (!Array.isArray(value)) {
229
+ throw new TypedMessageError('Expected an array');
230
+ }
231
+ const len = value.length;
232
+ const out = new Uint8Array(4);
233
+ const view = new DataView(out.buffer);
234
+ view.setUint32(0, len, false);
235
+ return out;
236
+ }
237
+ default:
238
+ throw new TypedMessageError('looked up value must not be a map or array');
239
+ }
240
+ }
241
+ export function getValue(what, msg) {
242
+ let currentValue;
243
+ let currentType;
244
+ switch (what.rootObject) {
245
+ case RootObject.DOMAIN:
246
+ currentValue = msg.domain;
247
+ currentType = parseType('EIP712Domain', msg.types);
248
+ break;
249
+ case RootObject.MESSAGE:
250
+ currentValue = msg.message;
251
+ currentType = parseType(msg.primaryType, msg.types);
252
+ break;
253
+ default:
254
+ throw new TypedMessageError('unknown root object');
255
+ }
256
+ for (const elementRaw of what.path) {
257
+ const element = elementRaw;
258
+ if (currentType.type === DataType.STRUCT) {
259
+ const struct = msg.types[currentType.structName];
260
+ if (struct === undefined) {
261
+ throw new TypedMessageError(`could not lookup type of name: ${currentType.structName}`);
262
+ }
263
+ const member = struct[element];
264
+ if (member === undefined) {
265
+ throw new TypedMessageError(`could not lookup member #${element} of type: ${currentType.structName}`);
266
+ }
267
+ if (typeof currentValue !== 'object' || currentValue === null) {
268
+ throw new TypedMessageError('expected object value at struct position');
269
+ }
270
+ const obj = currentValue;
271
+ if (!Object.prototype.hasOwnProperty.call(obj, member.name)) {
272
+ throw new TypedMessageError(`could not lookup: ${member.name}`);
273
+ }
274
+ currentValue = obj[member.name];
275
+ currentType = parseType(member.type, msg.types);
276
+ }
277
+ else if (currentType.type === DataType.ARRAY) {
278
+ if (!Array.isArray(currentValue)) {
279
+ throw new TypedMessageError('expected array at array position');
280
+ }
281
+ if (element >= currentValue.length) {
282
+ throw new TypedMessageError(`could not lookup array index: ${element}`);
283
+ }
284
+ currentValue = currentValue[element];
285
+ if (currentType.arrayType === undefined) {
286
+ throw new TypedMessageError('array type missing inner type');
287
+ }
288
+ currentType = currentType.arrayType;
289
+ }
290
+ else {
291
+ throw new TypedMessageError('path element does not point to struct or array');
292
+ }
293
+ }
294
+ if (currentType.type === DataType.STRUCT ||
295
+ (currentType.type === DataType.ARRAY && !Array.isArray(currentValue))) {
296
+ throw new TypedMessageError('path points to struct or array; value expected');
297
+ }
298
+ return {
299
+ value: encodeValue(currentType, currentValue),
300
+ dataType: currentType.type,
301
+ };
302
+ }
303
+ //# sourceMappingURL=eip712.js.map