@bsv/sdk 1.3.2 → 1.3.4

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 (75) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/certificates/Certificate.js +3 -4
  3. package/dist/cjs/src/auth/certificates/Certificate.js.map +1 -1
  4. package/dist/cjs/src/auth/certificates/MasterCertificate.js.map +1 -1
  5. package/dist/cjs/src/auth/utils/certificateHelpers.js +2 -2
  6. package/dist/cjs/src/auth/utils/certificateHelpers.js.map +1 -1
  7. package/dist/cjs/src/wallet/CachedKeyDeriver.js +11 -14
  8. package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -1
  9. package/dist/cjs/src/wallet/KeyDeriver.js +14 -11
  10. package/dist/cjs/src/wallet/KeyDeriver.js.map +1 -1
  11. package/dist/cjs/src/wallet/ProtoWallet.js +53 -111
  12. package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -1
  13. package/dist/cjs/src/wallet/WalletClient.js.map +1 -1
  14. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +1 -0
  15. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  16. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  17. package/dist/esm/src/auth/certificates/Certificate.js +4 -5
  18. package/dist/esm/src/auth/certificates/Certificate.js.map +1 -1
  19. package/dist/esm/src/auth/certificates/MasterCertificate.js.map +1 -1
  20. package/dist/esm/src/auth/utils/certificateHelpers.js +2 -2
  21. package/dist/esm/src/auth/utils/certificateHelpers.js.map +1 -1
  22. package/dist/esm/src/wallet/CachedKeyDeriver.js +10 -10
  23. package/dist/esm/src/wallet/CachedKeyDeriver.js.map +1 -1
  24. package/dist/esm/src/wallet/KeyDeriver.js +15 -12
  25. package/dist/esm/src/wallet/KeyDeriver.js.map +1 -1
  26. package/dist/esm/src/wallet/ProtoWallet.js +53 -76
  27. package/dist/esm/src/wallet/ProtoWallet.js.map +1 -1
  28. package/dist/esm/src/wallet/WalletClient.js.map +1 -1
  29. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js +1 -0
  30. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  31. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  32. package/dist/types/src/auth/certificates/Certificate.d.ts +2 -2
  33. package/dist/types/src/auth/certificates/Certificate.d.ts.map +1 -1
  34. package/dist/types/src/auth/certificates/MasterCertificate.d.ts +2 -2
  35. package/dist/types/src/auth/certificates/MasterCertificate.d.ts.map +1 -1
  36. package/dist/types/src/auth/utils/certificateHelpers.d.ts +5 -5
  37. package/dist/types/src/auth/utils/certificateHelpers.d.ts.map +1 -1
  38. package/dist/types/src/wallet/CachedKeyDeriver.d.ts +16 -15
  39. package/dist/types/src/wallet/CachedKeyDeriver.d.ts.map +1 -1
  40. package/dist/types/src/wallet/KeyDeriver.d.ts +74 -18
  41. package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -1
  42. package/dist/types/src/wallet/ProtoWallet.d.ts +31 -407
  43. package/dist/types/src/wallet/ProtoWallet.d.ts.map +1 -1
  44. package/dist/types/src/wallet/Wallet.interfaces.d.ts +201 -169
  45. package/dist/types/src/wallet/Wallet.interfaces.d.ts.map +1 -1
  46. package/dist/types/src/wallet/WalletClient.d.ts +2 -3
  47. package/dist/types/src/wallet/WalletClient.d.ts.map +1 -1
  48. package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts +1 -1
  49. package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts.map +1 -1
  50. package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts +1 -1
  51. package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts.map +1 -1
  52. package/dist/types/src/wallet/substrates/XDM.d.ts +2 -2
  53. package/dist/types/src/wallet/substrates/XDM.d.ts.map +1 -1
  54. package/dist/types/src/wallet/substrates/window.CWI.d.ts +2 -2
  55. package/dist/types/src/wallet/substrates/window.CWI.d.ts.map +1 -1
  56. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  57. package/dist/umd/bundle.js +1 -1
  58. package/docs/auth.md +12 -12
  59. package/docs/wallet-substrates.md +6 -6
  60. package/docs/wallet.md +897 -983
  61. package/package.json +1 -1
  62. package/src/auth/certificates/Certificate.ts +6 -6
  63. package/src/auth/certificates/MasterCertificate.ts +3 -2
  64. package/src/auth/utils/certificateHelpers.ts +5 -5
  65. package/src/wallet/CachedKeyDeriver.ts +22 -21
  66. package/src/wallet/KeyDeriver.ts +89 -22
  67. package/src/wallet/ProtoWallet.ts +105 -550
  68. package/src/wallet/Wallet.interfaces.ts +259 -176
  69. package/src/wallet/WalletClient.ts +2 -1
  70. package/src/wallet/__tests/ProtoWallet.test.ts +0 -53
  71. package/src/wallet/substrates/HTTPWalletJSON.ts +1 -1
  72. package/src/wallet/substrates/WalletWireTransceiver.ts +2 -1
  73. package/src/wallet/substrates/XDM.ts +2 -2
  74. package/src/wallet/substrates/__tests/WalletWire.integration.test.ts +22 -22
  75. package/src/wallet/substrates/window.CWI.ts +2 -2
@@ -1,277 +1,90 @@
1
+ import { KeyDeriver, KeyDeriverApi } from './KeyDeriver.js'
2
+ import { Hash, ECDSA, BigNumber, Signature, Schnorr, PublicKey, Point, PrivateKey } from '../primitives/index.js'
1
3
  import {
2
- Base64String,
3
- BasketStringUnder300Bytes,
4
- BEEF,
5
- BooleanDefaultFalse,
6
- BooleanDefaultTrue,
7
- Byte,
8
- CertificateFieldNameUnder50Bytes,
9
- DescriptionString5to50Bytes,
10
- EntityIconURLStringMax500Bytes,
11
- EntityNameStringMax100Bytes,
12
- HexString,
13
- ISOTimestampString,
14
- KeyIDStringUnder800Bytes,
15
- LabelStringUnder300Bytes,
4
+ AuthenticatedResult,
5
+ CreateHmacArgs,
6
+ CreateHmacResult,
7
+ CreateSignatureArgs,
8
+ CreateSignatureResult,
9
+ GetNetworkResult,
10
+ GetPublicKeyArgs,
11
+ GetVersionResult,
16
12
  OriginatorDomainNameStringUnder250Bytes,
17
- OutpointString,
18
- OutputTagStringUnder300Bytes,
19
- PositiveInteger,
20
- PositiveIntegerDefault10Max10000,
21
- PositiveIntegerMax10,
22
- PositiveIntegerOrZero,
23
- ProtocolString5To400Bytes,
13
+ ProtoWalletApi,
24
14
  PubKeyHex,
25
- SatoshiValue,
26
- SecurityLevel,
27
- TXIDHexString,
28
- VersionString7To30Bytes,
29
- Wallet
15
+ RevealCounterpartyKeyLinkageArgs,
16
+ RevealCounterpartyKeyLinkageResult,
17
+ RevealSpecificKeyLinkageArgs,
18
+ RevealSpecificKeyLinkageResult,
19
+ VerifyHmacArgs,
20
+ VerifyHmacResult,
21
+ VerifySignatureArgs,
22
+ VerifySignatureResult,
23
+ WalletDecryptArgs,
24
+ WalletDecryptResult,
25
+ WalletEncryptArgs,
26
+ WalletEncryptResult
30
27
  } from './Wallet.interfaces.js'
31
- import KeyDeriver from './KeyDeriver.js'
32
- import { PrivateKey, Hash, ECDSA, BigNumber, Signature, Schnorr, PublicKey, Point } from '../primitives/index.js'
33
- import walletErrors, { WalletError } from './WalletError.js'
28
+
29
+ const privilegedError = new Error('ProtoWallet is a single-keyring wallet, operating without context about whether its configured keyring is privileged.')
34
30
 
35
31
  /**
36
- * A ProtoWallet is a structure that fulfills the Wallet interface, capable of performing all foundational cryptographic operations. It can derive keys, create signatures, facilitate encryption and HMAC operations, and reveal key linkages. However, ProtoWallet does not create transactions, manage outputs, interact with the blockchain, enable the management of identity certificates, or store any data.
32
+ * A ProtoWallet is precursor to a full wallet, capable of performing all foundational cryptographic operations.
33
+ * It can derive keys, create signatures, facilitate encryption and HMAC operations, and reveal key linkages.
34
+ *
35
+ * However, ProtoWallet does not create transactions, manage outputs, interact with the blockchain,
36
+ * enable the management of identity certificates, or store any data.
37
37
  */
38
- export default class ProtoWallet implements Wallet {
39
- keyDeriver: KeyDeriver
40
- privilegedError: string =
41
- 'ProtoWallet is a single-keyring wallet, operating without context about whether its configured keyring is privileged.'
42
-
43
- constructor (rootKey: PrivateKey | 'anyone', KeyDeriverClass = KeyDeriver) {
44
- this.keyDeriver = new KeyDeriverClass(rootKey)
45
- }
46
-
47
- async createAction (
48
- args: {
49
- description: DescriptionString5to50Bytes
50
- inputs?: Array<{
51
- tx?: BEEF
52
- outpoint: OutpointString
53
- unlockingScript?: HexString
54
- unlockingScriptLength?: PositiveInteger
55
- inputDescription: DescriptionString5to50Bytes
56
- sequenceNumber?: PositiveIntegerOrZero
57
- }>
58
- outputs?: Array<{
59
- lockingScript: HexString
60
- satoshis: SatoshiValue
61
- outputDescription: DescriptionString5to50Bytes
62
- basket?: BasketStringUnder300Bytes
63
- customInstructions?: string
64
- tags?: OutputTagStringUnder300Bytes[]
65
- }>
66
- lockTime?: PositiveIntegerOrZero
67
- version?: PositiveIntegerOrZero
68
- labels?: LabelStringUnder300Bytes[]
69
- options?: {
70
- signAndProcess?: BooleanDefaultTrue
71
- acceptDelayedBroadcast?: BooleanDefaultTrue
72
- trustSelf?: 'known'
73
- knownTxids?: TXIDHexString[]
74
- returnTXIDOnly?: BooleanDefaultFalse
75
- noSend?: BooleanDefaultFalse
76
- noSendChange?: OutpointString[]
77
- sendWith?: TXIDHexString[]
78
- randomizeOutputs?: BooleanDefaultTrue
79
- }
80
- },
81
- originator?: OriginatorDomainNameStringUnder250Bytes
82
- ): Promise<{
83
- txid?: TXIDHexString
84
- tx?: BEEF
85
- noSendChange?: OutpointString[]
86
- sendWithResults?: Array<{
87
- txid: TXIDHexString
88
- status: 'unproven' | 'sending' | 'failed'
89
- }>
90
- signableTransaction?: {
91
- tx: BEEF
92
- reference: Base64String
93
- }
94
- }> {
95
- throw new WalletError('ProtoWallet does not support creating transactions.', walletErrors.unsupportedAction)
96
- }
38
+ export class ProtoWallet implements ProtoWalletApi {
39
+ keyDeriver: KeyDeriverApi
97
40
 
98
- async signAction (
99
- args: {
100
- spends: Record<
101
- PositiveIntegerOrZero,
102
- {
103
- unlockingScript: HexString
104
- sequenceNumber?: PositiveIntegerOrZero
105
- }
106
- >
107
- reference: Base64String
108
- options?: {
109
- acceptDelayedBroadcast?: BooleanDefaultTrue
110
- returnTXIDOnly?: BooleanDefaultFalse
111
- noSend?: BooleanDefaultFalse
112
- noSendChange?: OutpointString[]
113
- sendWith?: TXIDHexString[]
114
- }
115
- },
116
- originator?: OriginatorDomainNameStringUnder250Bytes
117
- ): Promise<{
118
- txid?: TXIDHexString
119
- tx?: BEEF
120
- noSendChange?: OutpointString[]
121
- sendWithResults?: Array<{
122
- txid: TXIDHexString
123
- status: 'unproven' | 'sending' | 'failed'
124
- }>
125
- }> {
126
- throw new WalletError('ProtoWallet does not support creating transactions.', walletErrors.unsupportedAction)
41
+ constructor(rootKeyOrKeyDeriver: PrivateKey | 'anyone' | KeyDeriverApi) {
42
+ if (typeof rootKeyOrKeyDeriver['identityKey'] !== 'string') {
43
+ rootKeyOrKeyDeriver = new KeyDeriver(rootKeyOrKeyDeriver as PrivateKey | 'anyone')
44
+ }
45
+ this.keyDeriver = rootKeyOrKeyDeriver as KeyDeriver
127
46
  }
128
47
 
129
- async abortAction (
130
- args: {
131
- reference: Base64String
132
- },
133
- originator?: OriginatorDomainNameStringUnder250Bytes
134
- ): Promise<{ aborted: true }> {
135
- throw new WalletError('ProtoWallet does not support aborting transactions.', walletErrors.unsupportedAction)
48
+ async isAuthenticated(args: {}, Originator?: OriginatorDomainNameStringUnder250Bytes): Promise<AuthenticatedResult> {
49
+ return { authenticated: true }
136
50
  }
137
51
 
138
- async listActions (
139
- args: {
140
- labels: LabelStringUnder300Bytes[]
141
- labelQueryMode?: 'any' | 'all'
142
- includeLabels?: BooleanDefaultFalse
143
- includeInputs?: BooleanDefaultFalse
144
- includeInputSourceLockingScripts?: BooleanDefaultFalse
145
- includeInputUnlockingScripts?: BooleanDefaultFalse
146
- includeOutputs?: BooleanDefaultFalse
147
- includeOutputLockingScripts?: BooleanDefaultFalse
148
- limit?: PositiveIntegerDefault10Max10000
149
- offset?: PositiveIntegerOrZero
150
- },
151
- originator?: OriginatorDomainNameStringUnder250Bytes
152
- ): Promise<{
153
- totalActions: PositiveIntegerOrZero
154
- actions: Array<{
155
- txid: TXIDHexString
156
- satoshis: SatoshiValue
157
- status:
158
- | 'completed'
159
- | 'unprocessed'
160
- | 'sending'
161
- | 'unproven'
162
- | 'unsigned'
163
- | 'nosend'
164
- | 'nonfinal'
165
- isOutgoing: boolean
166
- description: DescriptionString5to50Bytes
167
- labels?: LabelStringUnder300Bytes[]
168
- version: PositiveIntegerOrZero
169
- lockTime: PositiveIntegerOrZero
170
- inputs?: Array<{
171
- sourceOutpoint: OutpointString
172
- sourceSatoshis: SatoshiValue
173
- sourceLockingScript?: HexString
174
- unlockingScript?: HexString
175
- inputDescription: DescriptionString5to50Bytes
176
- sequenceNumber: PositiveIntegerOrZero
177
- }>
178
- outputs?: Array<{
179
- outputIndex: PositiveIntegerOrZero
180
- satoshis: SatoshiValue
181
- lockingScript?: HexString
182
- spendable: boolean
183
- outputDescription: DescriptionString5to50Bytes
184
- basket: BasketStringUnder300Bytes
185
- tags: OutputTagStringUnder300Bytes[]
186
- customInstructions?: string
187
- }>
188
- }>
189
- }> {
190
- throw new WalletError('ProtoWallet does not support retrieving transactions.', walletErrors.unsupportedAction)
52
+ async waitForAuthentication(args: {}, Originator?: OriginatorDomainNameStringUnder250Bytes): Promise<AuthenticatedResult> {
53
+ return { authenticated: true }
191
54
  }
192
55
 
193
- async internalizeAction (
194
- args: {
195
- tx: BEEF
196
- outputs: Array<{
197
- outputIndex: PositiveIntegerOrZero
198
- protocol: 'wallet payment' | 'basket insertion'
199
- paymentRemittance?: {
200
- derivationPrefix: Base64String
201
- derivationSuffix: Base64String
202
- senderIdentityKey: PubKeyHex
203
- }
204
- insertionRemittance?: {
205
- basket: BasketStringUnder300Bytes
206
- customInstructions?: string
207
- tags?: OutputTagStringUnder300Bytes[]
208
- }
209
- }>
210
- description: DescriptionString5to50Bytes
211
- labels?: LabelStringUnder300Bytes[]
212
- },
213
- originator?: OriginatorDomainNameStringUnder250Bytes
214
- ): Promise<{ accepted: true }> {
215
- throw new WalletError('ProtoWallet does not support internalizing transactions.', walletErrors.unsupportedAction)
56
+ async getNetwork(args: {}, Originator?: OriginatorDomainNameStringUnder250Bytes): Promise<GetNetworkResult> {
57
+ return { network: 'mainnet' }
216
58
  }
217
59
 
218
- async listOutputs (
219
- args: {
220
- basket: BasketStringUnder300Bytes
221
- tags?: OutputTagStringUnder300Bytes[]
222
- tagQueryMode?: 'all' | 'any'
223
- include?: 'locking scripts' | 'entire transactions'
224
- includeCustomInstructions?: BooleanDefaultFalse
225
- includeTags?: BooleanDefaultFalse
226
- includeLabels?: BooleanDefaultFalse
227
- limit?: PositiveIntegerDefault10Max10000
228
- offset?: PositiveIntegerOrZero
229
- },
230
- originator?: OriginatorDomainNameStringUnder250Bytes
231
- ): Promise<{
232
- totalOutputs: PositiveIntegerOrZero
233
- outputs: Array<{
234
- outpoint: OutpointString
235
- satoshis: SatoshiValue
236
- lockingScript?: HexString
237
- tx?: BEEF
238
- spendable: true
239
- customInstructions?: string
240
- tags?: OutputTagStringUnder300Bytes[]
241
- labels?: LabelStringUnder300Bytes[]
242
- }>
243
- }> {
244
- throw new WalletError('ProtoWallet does not support retrieving outputs.', walletErrors.unsupportedAction)
60
+ async getVersion(args: {}, Originator?: OriginatorDomainNameStringUnder250Bytes): Promise<GetVersionResult> {
61
+ return { version: 'proto-1.0.0' }
245
62
  }
246
63
 
247
- async relinquishOutput (
248
- args: {
249
- basket: BasketStringUnder300Bytes
250
- output: OutpointString
251
- },
64
+ /**
65
+ * Convenience method to obtain the identityKey.
66
+ * @param originator
67
+ * @returns `await this.getPublicKey({ identityKey: true }, originator)`
68
+ */
69
+ async getIdentityKey(
252
70
  originator?: OriginatorDomainNameStringUnder250Bytes
253
- ): Promise<{ relinquished: true }> {
254
- throw new WalletError('ProtoWallet does not support deleting outputs.', walletErrors.unsupportedAction)
71
+ ): Promise<{ publicKey: PubKeyHex }> {
72
+ return await this.getPublicKey({ identityKey: true }, originator)
255
73
  }
256
74
 
257
- async getPublicKey (
258
- args: {
259
- identityKey?: true
260
- protocolID?: [SecurityLevel, ProtocolString5To400Bytes]
261
- keyID?: KeyIDStringUnder800Bytes
262
- privileged?: BooleanDefaultFalse
263
- privilegedReason?: DescriptionString5to50Bytes
264
- counterparty?: PubKeyHex | 'self' | 'anyone'
265
- forSelf?: BooleanDefaultFalse
266
- },
75
+ async getPublicKey(
76
+ args: GetPublicKeyArgs,
267
77
  originator?: OriginatorDomainNameStringUnder250Bytes
268
78
  ): Promise<{ publicKey: PubKeyHex }> {
269
79
  if (args.privileged) {
270
- throw new WalletError(this.privilegedError)
80
+ throw privilegedError
271
81
  }
272
82
  if (args.identityKey) {
273
83
  return { publicKey: this.keyDeriver.rootKey.toPublicKey().toString() }
274
84
  } else {
85
+ if (!args.protocolID || !args.keyID) {
86
+ throw new Error('protocolID and keyID are required if identityKey is false or undefined.')
87
+ }
275
88
  return {
276
89
  publicKey: this.keyDeriver
277
90
  .derivePublicKey(
@@ -285,24 +98,12 @@ export default class ProtoWallet implements Wallet {
285
98
  }
286
99
  }
287
100
 
288
- async revealCounterpartyKeyLinkage (
289
- args: {
290
- counterparty: PubKeyHex
291
- verifier: PubKeyHex
292
- privilegedReason?: DescriptionString5to50Bytes
293
- privileged?: BooleanDefaultFalse
294
- },
101
+ async revealCounterpartyKeyLinkage(
102
+ args: RevealCounterpartyKeyLinkageArgs,
295
103
  originator?: OriginatorDomainNameStringUnder250Bytes
296
- ): Promise<{
297
- prover: PubKeyHex
298
- verifier: PubKeyHex
299
- counterparty: PubKeyHex
300
- revelationTime: ISOTimestampString
301
- encryptedLinkage: Byte[]
302
- encryptedLinkageProof: Byte[]
303
- }> {
104
+ ): Promise<RevealCounterpartyKeyLinkageResult> {
304
105
  if (args.privileged) {
305
- throw new WalletError(this.privilegedError)
106
+ throw privilegedError
306
107
  }
307
108
  const { publicKey: identityKey } = await this.getPublicKey({ identityKey: true })
308
109
  const linkage = this.keyDeriver.revealCounterpartySecret(args.counterparty)
@@ -335,28 +136,12 @@ export default class ProtoWallet implements Wallet {
335
136
  }
336
137
  }
337
138
 
338
- async revealSpecificKeyLinkage (
339
- args: {
340
- counterparty: PubKeyHex
341
- verifier: PubKeyHex
342
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
343
- keyID: KeyIDStringUnder800Bytes
344
- privilegedReason?: DescriptionString5to50Bytes
345
- privileged?: BooleanDefaultFalse
346
- },
139
+ async revealSpecificKeyLinkage(
140
+ args: RevealSpecificKeyLinkageArgs,
347
141
  originator?: OriginatorDomainNameStringUnder250Bytes
348
- ): Promise<{
349
- prover: PubKeyHex
350
- verifier: PubKeyHex
351
- counterparty: PubKeyHex
352
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
353
- keyID: KeyIDStringUnder800Bytes
354
- encryptedLinkage: Byte[]
355
- encryptedLinkageProof: Byte[]
356
- proofType: Byte
357
- }> {
142
+ ): Promise<RevealSpecificKeyLinkageResult> {
358
143
  if (args.privileged) {
359
- throw new WalletError(this.privilegedError)
144
+ throw privilegedError
360
145
  }
361
146
  const { publicKey: identityKey } = await this.getPublicKey({ identityKey: true })
362
147
  const linkage = this.keyDeriver.revealSpecificSecret(
@@ -388,19 +173,12 @@ export default class ProtoWallet implements Wallet {
388
173
  }
389
174
  }
390
175
 
391
- async encrypt (
392
- args: {
393
- plaintext: Byte[]
394
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
395
- keyID: KeyIDStringUnder800Bytes
396
- privilegedReason?: DescriptionString5to50Bytes
397
- counterparty?: PubKeyHex | 'self' | 'anyone'
398
- privileged?: BooleanDefaultFalse
399
- },
176
+ async encrypt(
177
+ args: WalletEncryptArgs,
400
178
  originator?: OriginatorDomainNameStringUnder250Bytes
401
- ): Promise<{ ciphertext: Byte[] }> {
179
+ ): Promise<WalletEncryptResult> {
402
180
  if (args.privileged) {
403
- throw new WalletError(this.privilegedError)
181
+ throw privilegedError
404
182
  }
405
183
  const key = this.keyDeriver.deriveSymmetricKey(
406
184
  args.protocolID,
@@ -410,19 +188,12 @@ export default class ProtoWallet implements Wallet {
410
188
  return { ciphertext: key.encrypt(args.plaintext) as number[] }
411
189
  }
412
190
 
413
- async decrypt (
414
- args: {
415
- ciphertext: Byte[]
416
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
417
- keyID: KeyIDStringUnder800Bytes
418
- privilegedReason?: DescriptionString5to50Bytes
419
- counterparty?: PubKeyHex | 'self' | 'anyone'
420
- privileged?: BooleanDefaultFalse
421
- },
191
+ async decrypt(
192
+ args: WalletDecryptArgs,
422
193
  originator?: OriginatorDomainNameStringUnder250Bytes
423
- ): Promise<{ plaintext: Byte[] }> {
194
+ ): Promise<WalletDecryptResult> {
424
195
  if (args.privileged) {
425
- throw new WalletError(this.privilegedError)
196
+ throw privilegedError
426
197
  }
427
198
  const key = this.keyDeriver.deriveSymmetricKey(
428
199
  args.protocolID,
@@ -432,19 +203,12 @@ export default class ProtoWallet implements Wallet {
432
203
  return { plaintext: key.decrypt(args.ciphertext) as number[] }
433
204
  }
434
205
 
435
- async createHmac (
436
- args: {
437
- data: Byte[]
438
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
439
- keyID: KeyIDStringUnder800Bytes
440
- privilegedReason?: DescriptionString5to50Bytes
441
- counterparty?: PubKeyHex | 'self' | 'anyone'
442
- privileged?: BooleanDefaultFalse
443
- },
206
+ async createHmac(
207
+ args: CreateHmacArgs,
444
208
  originator?: OriginatorDomainNameStringUnder250Bytes
445
- ): Promise<{ hmac: Byte[] }> {
209
+ ): Promise<CreateHmacResult> {
446
210
  if (args.privileged) {
447
- throw new WalletError(this.privilegedError)
211
+ throw privilegedError
448
212
  }
449
213
  const key = this.keyDeriver.deriveSymmetricKey(
450
214
  args.protocolID,
@@ -454,20 +218,12 @@ export default class ProtoWallet implements Wallet {
454
218
  return { hmac: Hash.sha256hmac(key.toArray(), args.data) }
455
219
  }
456
220
 
457
- async verifyHmac (
458
- args: {
459
- data: Byte[]
460
- hmac: Byte[]
461
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
462
- keyID: KeyIDStringUnder800Bytes
463
- privilegedReason?: DescriptionString5to50Bytes
464
- counterparty?: PubKeyHex | 'self' | 'anyone'
465
- privileged?: BooleanDefaultFalse
466
- },
221
+ async verifyHmac(
222
+ args: VerifyHmacArgs,
467
223
  originator?: OriginatorDomainNameStringUnder250Bytes
468
- ): Promise<{ valid: true }> {
224
+ ): Promise<VerifyHmacResult> {
469
225
  if (args.privileged) {
470
- throw new WalletError(this.privilegedError)
226
+ throw privilegedError
471
227
  }
472
228
  const key = this.keyDeriver.deriveSymmetricKey(
473
229
  args.protocolID,
@@ -476,30 +232,24 @@ export default class ProtoWallet implements Wallet {
476
232
  )
477
233
  const valid = Hash.sha256hmac(key.toArray(), args.data).toString() === args.hmac.toString()
478
234
  if (!valid) {
479
- throw new WalletError('HMAC is not valid', walletErrors.invalidHmac)
235
+ const e = new Error('HMAC is not valid');
236
+ (e as any).code = 'ERR_INVALID_HMAC'
237
+ throw e
480
238
  }
481
239
  return { valid }
482
240
  }
483
241
 
484
- async createSignature (
485
- args: {
486
- data?: Byte[]
487
- hashToDirectlySign?: Byte[]
488
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
489
- keyID: KeyIDStringUnder800Bytes
490
- privilegedReason?: DescriptionString5to50Bytes
491
- counterparty?: PubKeyHex | 'self' | 'anyone'
492
- privileged?: BooleanDefaultFalse
493
- },
242
+ async createSignature(
243
+ args: CreateSignatureArgs,
494
244
  originator?: OriginatorDomainNameStringUnder250Bytes
495
- ): Promise<{ signature: Byte[] }> {
245
+ ): Promise<CreateSignatureResult> {
496
246
  if (args.privileged) {
497
- throw new WalletError(this.privilegedError)
247
+ throw privilegedError
498
248
  }
499
- let hash: number[] = args.hashToDirectlySign
500
- if (!hash) {
501
- hash = Hash.sha256(args.data)
249
+ if (!args.hashToDirectlySign && !args.data) {
250
+ throw new Error('args.data or args.hashToDirectlySign must be valid')
502
251
  }
252
+ const hash: number[] = args.hashToDirectlySign || Hash.sha256(args.data!)
503
253
  const key = this.keyDeriver.derivePrivateKey(
504
254
  args.protocolID,
505
255
  args.keyID,
@@ -508,27 +258,17 @@ export default class ProtoWallet implements Wallet {
508
258
  return { signature: ECDSA.sign(new BigNumber(hash), key, true).toDER() as number[] }
509
259
  }
510
260
 
511
- async verifySignature (
512
- args: {
513
- data?: Byte[]
514
- hashToDirectlyVerify?: Byte[]
515
- signature: Byte[]
516
- protocolID: [SecurityLevel, ProtocolString5To400Bytes]
517
- keyID: KeyIDStringUnder800Bytes
518
- privilegedReason?: DescriptionString5to50Bytes
519
- counterparty?: PubKeyHex | 'self' | 'anyone'
520
- forSelf?: BooleanDefaultFalse
521
- privileged?: BooleanDefaultFalse
522
- },
261
+ async verifySignature(
262
+ args: VerifySignatureArgs,
523
263
  originator?: OriginatorDomainNameStringUnder250Bytes
524
- ): Promise<{ valid: true }> {
264
+ ): Promise<VerifySignatureResult> {
525
265
  if (args.privileged) {
526
- throw new WalletError(this.privilegedError)
266
+ throw privilegedError
527
267
  }
528
- let hash: number[] = args.hashToDirectlyVerify
529
- if (!hash) {
530
- hash = Hash.sha256(args.data)
268
+ if (!args.hashToDirectlyVerify && !args.data) {
269
+ throw new Error('args.data or args.hashToDirectlyVerify must be valid')
531
270
  }
271
+ const hash: number[] = args.hashToDirectlyVerify || Hash.sha256(args.data!)
532
272
  const key = this.keyDeriver.derivePublicKey(
533
273
  args.protocolID,
534
274
  args.keyID,
@@ -537,197 +277,12 @@ export default class ProtoWallet implements Wallet {
537
277
  )
538
278
  const valid = ECDSA.verify(new BigNumber(hash), Signature.fromDER(args.signature), key)
539
279
  if (!valid) {
540
- throw new WalletError('Signature is not valid', walletErrors.invalidSignature)
280
+ const e = new Error('Signature is not valid');
281
+ (e as any).code = 'ERR_INVALID_SIGNATURE'
282
+ throw e
541
283
  }
542
284
  return { valid }
543
285
  }
544
-
545
- async acquireCertificate (
546
- args: {
547
- type: Base64String
548
- certifier: PubKeyHex
549
- acquisitionProtocol: 'direct' | 'issuance'
550
- fields: Record<CertificateFieldNameUnder50Bytes, string>
551
- serialNumber?: Base64String
552
- revocationOutpoint?: OutpointString
553
- signature?: HexString
554
- certifierUrl?: string
555
- keyringRevealer?: PubKeyHex | 'certifier'
556
- keyringForSubject?: Record<CertificateFieldNameUnder50Bytes, Base64String>
557
- privileged?: BooleanDefaultFalse
558
- privilegedReason?: DescriptionString5to50Bytes
559
- },
560
- originator?: OriginatorDomainNameStringUnder250Bytes
561
- ): Promise<{
562
- type: Base64String
563
- subject: PubKeyHex
564
- serialNumber: Base64String
565
- certifier: PubKeyHex
566
- revocationOutpoint: OutpointString
567
- signature: HexString
568
- fields: Record<CertificateFieldNameUnder50Bytes, string>
569
- }> {
570
- throw new WalletError('ProtoWallet does not support acquiring certificates.', walletErrors.unsupportedAction)
571
- }
572
-
573
- async listCertificates (
574
- args: {
575
- certifiers: PubKeyHex[]
576
- types: Base64String[]
577
- limit?: PositiveIntegerDefault10Max10000
578
- offset?: PositiveIntegerOrZero
579
- privileged?: BooleanDefaultFalse
580
- privilegedReason?: DescriptionString5to50Bytes
581
- },
582
- originator?: OriginatorDomainNameStringUnder250Bytes
583
- ): Promise<{
584
- totalCertificates: PositiveIntegerOrZero
585
- certificates: Array<{
586
- type: Base64String
587
- subject: PubKeyHex
588
- serialNumber: Base64String
589
- certifier: PubKeyHex
590
- revocationOutpoint: OutpointString
591
- signature: HexString
592
- fields: Record<CertificateFieldNameUnder50Bytes, string>
593
- }>
594
- }> {
595
- throw new WalletError('ProtoWallet does not support retrieving certificates.', walletErrors.unsupportedAction)
596
- }
597
-
598
- async proveCertificate (
599
- args: {
600
- certificate: {
601
- type: Base64String
602
- subject: PubKeyHex
603
- serialNumber: Base64String
604
- certifier: PubKeyHex
605
- revocationOutpoint: OutpointString
606
- signature: HexString
607
- fields: Record<CertificateFieldNameUnder50Bytes, string>
608
- }
609
- fieldsToReveal: CertificateFieldNameUnder50Bytes[]
610
- verifier: PubKeyHex
611
- privileged?: BooleanDefaultFalse
612
- privilegedReason?: DescriptionString5to50Bytes
613
- },
614
- originator?: OriginatorDomainNameStringUnder250Bytes
615
- ): Promise<{
616
- keyringForVerifier: Record<CertificateFieldNameUnder50Bytes, Base64String>
617
- }> {
618
- throw new WalletError('ProtoWallet does not support proving certificates.', walletErrors.unsupportedAction)
619
- }
620
-
621
- async relinquishCertificate (
622
- args: {
623
- type: Base64String
624
- serialNumber: Base64String
625
- certifier: PubKeyHex
626
- },
627
- originator?: OriginatorDomainNameStringUnder250Bytes
628
- ): Promise<{ relinquished: true }> {
629
- throw new WalletError('ProtoWallet does not support deleting certificates.', walletErrors.unsupportedAction)
630
- }
631
-
632
- async discoverByIdentityKey (
633
- args: {
634
- identityKey: PubKeyHex
635
- limit?: PositiveIntegerDefault10Max10000
636
- offset?: PositiveIntegerOrZero
637
- },
638
- originator?: OriginatorDomainNameStringUnder250Bytes
639
- ): Promise<{
640
- totalCertificates: PositiveIntegerOrZero
641
- certificates: Array<{
642
- type: Base64String
643
- subject: PubKeyHex
644
- serialNumber: Base64String
645
- certifier: PubKeyHex
646
- revocationOutpoint: OutpointString
647
- signature: HexString
648
- fields: Record<CertificateFieldNameUnder50Bytes, Base64String>
649
- certifierInfo: {
650
- name: EntityNameStringMax100Bytes
651
- iconUrl: EntityIconURLStringMax500Bytes
652
- description: DescriptionString5to50Bytes
653
- trust: PositiveIntegerMax10
654
- }
655
- publiclyRevealedKeyring: Record<CertificateFieldNameUnder50Bytes, Base64String>
656
- decryptedFields: Record<CertificateFieldNameUnder50Bytes, string>
657
- }>
658
- }> {
659
- throw new WalletError('ProtoWallet does not support resolving identities.', walletErrors.unsupportedAction)
660
- }
661
-
662
- async discoverByAttributes (
663
- args: {
664
- attributes: Record<CertificateFieldNameUnder50Bytes, string>
665
- limit?: PositiveIntegerDefault10Max10000
666
- offset?: PositiveIntegerOrZero
667
- },
668
- originator?: OriginatorDomainNameStringUnder250Bytes
669
- ): Promise<{
670
- totalCertificates: PositiveIntegerOrZero
671
- certificates: Array<{
672
- type: Base64String
673
- subject: PubKeyHex
674
- serialNumber: Base64String
675
- certifier: PubKeyHex
676
- revocationOutpoint: OutpointString
677
- signature: HexString
678
- fields: Record<CertificateFieldNameUnder50Bytes, Base64String>
679
- certifierInfo: {
680
- name: EntityNameStringMax100Bytes
681
- iconUrl: EntityIconURLStringMax500Bytes
682
- description: DescriptionString5to50Bytes
683
- trust: PositiveIntegerMax10
684
- }
685
- publiclyRevealedKeyring: Record<CertificateFieldNameUnder50Bytes, Base64String>
686
- decryptedFields: Record<CertificateFieldNameUnder50Bytes, string>
687
- }>
688
- }> {
689
- throw new WalletError('ProtoWallet does not support resolving identities.', walletErrors.unsupportedAction)
690
- }
691
-
692
- async isAuthenticated (
693
- args: {},
694
- originator?: OriginatorDomainNameStringUnder250Bytes
695
- ): Promise<{ authenticated: boolean }> {
696
- return { authenticated: true }
697
- }
698
-
699
- async waitForAuthentication (
700
- args: {},
701
- originator?: OriginatorDomainNameStringUnder250Bytes
702
- ): Promise<{ authenticated: true }> {
703
- return { authenticated: true }
704
- }
705
-
706
- async getHeight (
707
- args: {},
708
- originator?: OriginatorDomainNameStringUnder250Bytes
709
- ): Promise<{ height: PositiveInteger }> {
710
- throw new WalletError('ProtoWallet does not support blockchain tracking.', walletErrors.unsupportedAction)
711
- }
712
-
713
- async getHeaderForHeight (
714
- args: { height: PositiveInteger },
715
- originator?: OriginatorDomainNameStringUnder250Bytes
716
- ): Promise<{ header: HexString }> {
717
- throw new WalletError('ProtoWallet does not support blockchain tracking.', walletErrors.unsupportedAction)
718
- }
719
-
720
- async getNetwork (
721
- args: {},
722
- originator?: OriginatorDomainNameStringUnder250Bytes
723
- ): Promise<{ network: 'mainnet' | 'testnet' }> {
724
- return { network: 'mainnet' }
725
- }
726
-
727
- async getVersion (
728
- args: {},
729
- originator?: OriginatorDomainNameStringUnder250Bytes
730
- ): Promise<{ version: VersionString7To30Bytes }> {
731
- return { version: 'proto-1.0.0' }
732
- }
733
286
  }
287
+
288
+ export default ProtoWallet