@bsv/wallet-toolbox 1.6.33 → 1.6.34
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/CHANGELOG.md +7 -0
- package/docs/client.md +340 -93
- package/docs/monitor.md +34 -12
- package/docs/storage.md +49 -1
- package/docs/wallet.md +340 -93
- package/mobile/out/src/monitor/tasks/TaskReorg.d.ts.map +1 -1
- package/mobile/out/src/monitor/tasks/TaskReorg.js +5 -1
- package/mobile/out/src/monitor/tasks/TaskReorg.js.map +1 -1
- package/mobile/out/src/sdk/WERR_errors.d.ts +8 -0
- package/mobile/out/src/sdk/WERR_errors.d.ts.map +1 -1
- package/mobile/out/src/sdk/WERR_errors.js +33 -0
- package/mobile/out/src/sdk/WERR_errors.js.map +1 -1
- package/mobile/out/src/sdk/WalletError.d.ts +19 -0
- package/mobile/out/src/sdk/WalletError.d.ts.map +1 -1
- package/mobile/out/src/sdk/WalletError.js +42 -1
- package/mobile/out/src/sdk/WalletError.js.map +1 -1
- package/mobile/out/src/sdk/WalletErrorFromJson.d.ts +12 -0
- package/mobile/out/src/sdk/WalletErrorFromJson.d.ts.map +1 -0
- package/mobile/out/src/sdk/WalletErrorFromJson.js +69 -0
- package/mobile/out/src/sdk/WalletErrorFromJson.js.map +1 -0
- package/mobile/out/src/sdk/index.d.ts +1 -0
- package/mobile/out/src/sdk/index.d.ts.map +1 -1
- package/mobile/out/src/sdk/index.js +1 -0
- package/mobile/out/src/sdk/index.js.map +1 -1
- package/mobile/out/src/services/chaintracker/ChaintracksChainTracker.js +1 -1
- package/mobile/out/src/services/chaintracker/ChaintracksChainTracker.js.map +1 -1
- package/mobile/out/src/services/chaintracker/chaintracks/util/ChaintracksFetch.d.ts.map +1 -1
- package/mobile/out/src/services/chaintracker/chaintracks/util/ChaintracksFetch.js +16 -9
- package/mobile/out/src/services/chaintracker/chaintracks/util/ChaintracksFetch.js.map +1 -1
- package/mobile/out/src/storage/WalletStorageManager.d.ts.map +1 -1
- package/mobile/out/src/storage/WalletStorageManager.js.map +1 -1
- package/mobile/out/src/storage/remoting/StorageClient.d.ts.map +1 -1
- package/mobile/out/src/storage/remoting/StorageClient.js +3 -5
- package/mobile/out/src/storage/remoting/StorageClient.js.map +1 -1
- package/mobile/package-lock.json +2 -2
- package/mobile/package.json +1 -1
- package/out/src/monitor/tasks/TaskReorg.d.ts.map +1 -1
- package/out/src/monitor/tasks/TaskReorg.js +5 -1
- package/out/src/monitor/tasks/TaskReorg.js.map +1 -1
- package/out/src/sdk/WERR_errors.d.ts +8 -0
- package/out/src/sdk/WERR_errors.d.ts.map +1 -1
- package/out/src/sdk/WERR_errors.js +33 -0
- package/out/src/sdk/WERR_errors.js.map +1 -1
- package/out/src/sdk/WalletError.d.ts +19 -0
- package/out/src/sdk/WalletError.d.ts.map +1 -1
- package/out/src/sdk/WalletError.js +42 -1
- package/out/src/sdk/WalletError.js.map +1 -1
- package/out/src/sdk/WalletErrorFromJson.d.ts +12 -0
- package/out/src/sdk/WalletErrorFromJson.d.ts.map +1 -0
- package/out/src/sdk/WalletErrorFromJson.js +69 -0
- package/out/src/sdk/WalletErrorFromJson.js.map +1 -0
- package/out/src/sdk/__test/WalletError.test.d.ts +2 -0
- package/out/src/sdk/__test/WalletError.test.d.ts.map +1 -0
- package/out/src/sdk/__test/WalletError.test.js +255 -0
- package/out/src/sdk/__test/WalletError.test.js.map +1 -0
- package/out/src/sdk/index.d.ts +1 -0
- package/out/src/sdk/index.d.ts.map +1 -1
- package/out/src/sdk/index.js +1 -0
- package/out/src/sdk/index.js.map +1 -1
- package/out/src/services/__tests/verifyBeef.test.js.map +1 -1
- package/out/src/services/chaintracker/ChaintracksChainTracker.js +1 -1
- package/out/src/services/chaintracker/ChaintracksChainTracker.js.map +1 -1
- package/out/src/services/chaintracker/chaintracks/util/ChaintracksFetch.d.ts.map +1 -1
- package/out/src/services/chaintracker/chaintracks/util/ChaintracksFetch.js +16 -9
- package/out/src/services/chaintracker/chaintracks/util/ChaintracksFetch.js.map +1 -1
- package/out/src/storage/WalletStorageManager.d.ts.map +1 -1
- package/out/src/storage/WalletStorageManager.js.map +1 -1
- package/out/src/storage/remoting/StorageClient.d.ts.map +1 -1
- package/out/src/storage/remoting/StorageClient.js +3 -5
- package/out/src/storage/remoting/StorageClient.js.map +1 -1
- package/out/src/storage/remoting/StorageServer.d.ts.map +1 -1
- package/out/src/storage/remoting/StorageServer.js +12 -10
- package/out/src/storage/remoting/StorageServer.js.map +1 -1
- package/out/test/Wallet/specOps/specOps.man.test.js +8 -6
- package/out/test/Wallet/specOps/specOps.man.test.js.map +1 -1
- package/out/tsconfig.all.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/monitor/tasks/TaskReorg.ts +5 -1
- package/src/sdk/WERR_errors.ts +34 -0
- package/src/sdk/WalletError.ts +42 -1
- package/src/sdk/WalletErrorFromJson.ts +80 -0
- package/src/sdk/WalletStorage.interfaces.ts +3 -3
- package/src/sdk/__test/WalletError.test.ts +318 -0
- package/src/sdk/index.ts +1 -0
- package/src/services/__tests/verifyBeef.test.ts +0 -1
- package/src/services/chaintracker/ChaintracksChainTracker.ts +1 -1
- package/src/services/chaintracker/chaintracks/util/ChaintracksFetch.ts +18 -11
- package/src/storage/WalletStorageManager.ts +16 -19
- package/src/storage/methods/internalizeAction.ts +1 -1
- package/src/storage/remoting/StorageClient.ts +4 -6
- package/src/storage/remoting/StorageServer.ts +13 -11
- package/test/Wallet/specOps/specOps.man.test.ts +6 -4
package/package.json
CHANGED
|
@@ -75,7 +75,11 @@ export class TaskReorg extends WalletMonitorTask {
|
|
|
75
75
|
log += ` maximum retries ${this.maxRetries} exceeded\n`
|
|
76
76
|
} else {
|
|
77
77
|
log += ` retrying...\n`
|
|
78
|
-
this.monitor.deactivatedHeaders.push({
|
|
78
|
+
this.monitor.deactivatedHeaders.push({
|
|
79
|
+
header: header.header,
|
|
80
|
+
whenMsecs: Date.now(),
|
|
81
|
+
tries: header.tries + 1
|
|
82
|
+
})
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
85
|
}
|
package/src/sdk/WERR_errors.ts
CHANGED
|
@@ -54,6 +54,11 @@ export class WERR_INVALID_PARAMETER extends WalletError {
|
|
|
54
54
|
) {
|
|
55
55
|
super('WERR_INVALID_PARAMETER', `The ${parameter} parameter must be ${mustBe ?? 'valid.'}`)
|
|
56
56
|
}
|
|
57
|
+
override toJson(): string {
|
|
58
|
+
const obj = JSON.parse(super.toJson())
|
|
59
|
+
obj.parameter = this.parameter
|
|
60
|
+
return JSON.stringify(obj)
|
|
61
|
+
}
|
|
57
62
|
}
|
|
58
63
|
|
|
59
64
|
/**
|
|
@@ -65,6 +70,11 @@ export class WERR_MISSING_PARAMETER extends WalletError {
|
|
|
65
70
|
constructor(public parameter: string) {
|
|
66
71
|
super('WERR_MISSING_PARAMETER', `The required ${parameter} parameter is missing.`)
|
|
67
72
|
}
|
|
73
|
+
override toJson(): string {
|
|
74
|
+
const obj = JSON.parse(super.toJson())
|
|
75
|
+
obj.parameter = this.parameter
|
|
76
|
+
return JSON.stringify(obj)
|
|
77
|
+
}
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
/**
|
|
@@ -127,6 +137,12 @@ export class WERR_INSUFFICIENT_FUNDS extends WalletError {
|
|
|
127
137
|
`Insufficient funds in the available inputs to cover the cost of the required outputs and the transaction fee (${moreSatoshisNeeded} more satoshis are needed, for a total of ${totalSatoshisNeeded}), plus whatever would be required in order to pay the fee to unlock and spend the outputs used to provide the additional satoshis.`
|
|
128
138
|
)
|
|
129
139
|
}
|
|
140
|
+
override toJson(): string {
|
|
141
|
+
const obj = JSON.parse(super.toJson())
|
|
142
|
+
obj.totalSatoshisNeeded = this.totalSatoshisNeeded
|
|
143
|
+
obj.moreSatoshisNeeded = this.moreSatoshisNeeded
|
|
144
|
+
return JSON.stringify(obj)
|
|
145
|
+
}
|
|
130
146
|
}
|
|
131
147
|
|
|
132
148
|
export class WERR_INVALID_PUBLIC_KEY extends WalletError {
|
|
@@ -144,6 +160,11 @@ export class WERR_INVALID_PUBLIC_KEY extends WalletError {
|
|
|
144
160
|
: `The provided public key is invalid or malformed.`
|
|
145
161
|
super('WERR_INVALID_PUBLIC_KEY', message)
|
|
146
162
|
}
|
|
163
|
+
protected override toJson(): string {
|
|
164
|
+
const obj = JSON.parse(super.toJson())
|
|
165
|
+
obj.key = this.key
|
|
166
|
+
return JSON.stringify(obj)
|
|
167
|
+
}
|
|
147
168
|
}
|
|
148
169
|
|
|
149
170
|
/**
|
|
@@ -166,4 +187,17 @@ export class WERR_REVIEW_ACTIONS extends WalletError {
|
|
|
166
187
|
) {
|
|
167
188
|
super('WERR_REVIEW_ACTIONS', 'Undelayed createAction or signAction results require review.')
|
|
168
189
|
}
|
|
190
|
+
override toJson(): string {
|
|
191
|
+
const obj = JSON.parse(super.toJson())
|
|
192
|
+
obj.reviewActionResults = this.reviewActionResults
|
|
193
|
+
obj.sendWithResults = this.sendWithResults
|
|
194
|
+
obj.txid = this.txid
|
|
195
|
+
obj.tx = this.tx
|
|
196
|
+
obj.noSendChange = this.noSendChange
|
|
197
|
+
return JSON.stringify(obj)
|
|
198
|
+
}
|
|
169
199
|
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* IF YOU ADD NEW ERRORS, ALSO UPDATE THE WalletError.fromJson METHOD IN src/sdk/WalletError.ts
|
|
203
|
+
*/
|
package/src/sdk/WalletError.ts
CHANGED
|
@@ -56,7 +56,7 @@ export class WalletError extends Error implements WalletErrorObject {
|
|
|
56
56
|
static fromUnknown(err: unknown): WalletError {
|
|
57
57
|
if (err instanceof WalletError) return err
|
|
58
58
|
let name = 'WERR_UNKNOWN'
|
|
59
|
-
let message = ''
|
|
59
|
+
let message = typeof err === 'string' ? err : typeof err === 'number' ? err.toString() : ''
|
|
60
60
|
let stack: string | undefined
|
|
61
61
|
const details: Record<string, string> = {}
|
|
62
62
|
if (err !== null && typeof err === 'object') {
|
|
@@ -116,4 +116,45 @@ export class WalletError extends Error implements WalletErrorObject {
|
|
|
116
116
|
description: this.message
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Base class default JSON serialization.
|
|
122
|
+
* Captures just the name and message properties.
|
|
123
|
+
*
|
|
124
|
+
* Override this method to safely (avoid deep, large, circular issues) serialize
|
|
125
|
+
* derived class properties.
|
|
126
|
+
*
|
|
127
|
+
* @returns stringified JSON representation of the WalletError.
|
|
128
|
+
*/
|
|
129
|
+
protected toJson(): string {
|
|
130
|
+
const e = new WalletError(this.name, this.message)
|
|
131
|
+
const json = JSON.stringify({
|
|
132
|
+
isError: true,
|
|
133
|
+
name: e.name,
|
|
134
|
+
message: e.message
|
|
135
|
+
})
|
|
136
|
+
return json
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Safely serializes a WalletError derived, WERR_REVIEW_ACTIONS (special case), Error or unknown error to JSON.
|
|
141
|
+
*
|
|
142
|
+
* Safely means avoiding deep, large, circular issues.
|
|
143
|
+
*
|
|
144
|
+
* @param error
|
|
145
|
+
* @returns stringified JSON representation of the error such that it can be desirialized to a WalletError.
|
|
146
|
+
*/
|
|
147
|
+
static unknownToJson(error: unknown | WalletError): string {
|
|
148
|
+
let json: string | undefined
|
|
149
|
+
let e: WalletError | undefined
|
|
150
|
+
if (error instanceof WalletError || (typeof error === 'object' && error?.constructor.name.startsWith('WERR_'))) {
|
|
151
|
+
e = error as WalletError
|
|
152
|
+
} else if (error instanceof Error) {
|
|
153
|
+
e = new WalletError(error.name, error.message)
|
|
154
|
+
} else {
|
|
155
|
+
e = new WalletError('WERR_UNKNOWN', String(error))
|
|
156
|
+
}
|
|
157
|
+
json = e.toJson()
|
|
158
|
+
return json
|
|
159
|
+
}
|
|
119
160
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { WalletError } from './WalletError'
|
|
2
|
+
import {
|
|
3
|
+
WERR_BAD_REQUEST,
|
|
4
|
+
WERR_BROADCAST_UNAVAILABLE,
|
|
5
|
+
WERR_INSUFFICIENT_FUNDS,
|
|
6
|
+
WERR_INTERNAL,
|
|
7
|
+
WERR_INVALID_OPERATION,
|
|
8
|
+
WERR_INVALID_PARAMETER,
|
|
9
|
+
WERR_INVALID_PUBLIC_KEY,
|
|
10
|
+
WERR_MISSING_PARAMETER,
|
|
11
|
+
WERR_NETWORK_CHAIN,
|
|
12
|
+
WERR_NOT_ACTIVE,
|
|
13
|
+
WERR_NOT_IMPLEMENTED,
|
|
14
|
+
WERR_REVIEW_ACTIONS,
|
|
15
|
+
WERR_UNAUTHORIZED
|
|
16
|
+
} from './WERR_errors'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Reconstruct the correct derived WalletError from a JSON object created by `WalletError.unknownToJson`.
|
|
20
|
+
*
|
|
21
|
+
* This function is implemented as a separate function instead of a WalletError class static
|
|
22
|
+
* to avoid circular dependencies.
|
|
23
|
+
*
|
|
24
|
+
* @param json
|
|
25
|
+
* @returns a WalletError derived error object, typically for re-throw.
|
|
26
|
+
*/
|
|
27
|
+
export function WalletErrorFromJson(json: object): WalletError {
|
|
28
|
+
let e: WalletError
|
|
29
|
+
const obj = json as any
|
|
30
|
+
switch (obj.name) {
|
|
31
|
+
case 'WERR_NOT_IMPLEMENTED':
|
|
32
|
+
e = new WERR_NOT_IMPLEMENTED(obj.message)
|
|
33
|
+
break
|
|
34
|
+
case 'WERR_INTERNAL':
|
|
35
|
+
e = new WERR_INTERNAL(obj.message)
|
|
36
|
+
break
|
|
37
|
+
case 'WERR_INVALID_OPERATION':
|
|
38
|
+
e = new WERR_INVALID_OPERATION(obj.message)
|
|
39
|
+
break
|
|
40
|
+
case 'WERR_BROADCAST_UNAVAILABLE':
|
|
41
|
+
e = new WERR_BROADCAST_UNAVAILABLE(obj.message)
|
|
42
|
+
break
|
|
43
|
+
case 'WERR_INVALID_PARAMETER':
|
|
44
|
+
e = new WERR_INVALID_PARAMETER(obj.parameter)
|
|
45
|
+
e.message = obj.message
|
|
46
|
+
break
|
|
47
|
+
case 'WERR_MISSING_PARAMETER':
|
|
48
|
+
e = new WERR_MISSING_PARAMETER(obj.parameter)
|
|
49
|
+
e.message = obj.message
|
|
50
|
+
break
|
|
51
|
+
case 'WERR_BAD_REQUEST':
|
|
52
|
+
e = new WERR_BAD_REQUEST(obj.message)
|
|
53
|
+
break
|
|
54
|
+
case 'WERR_NETWORK_CHAIN':
|
|
55
|
+
e = new WERR_NETWORK_CHAIN(obj.message)
|
|
56
|
+
break
|
|
57
|
+
case 'WERR_UNAUTHORIZED':
|
|
58
|
+
e = new WERR_UNAUTHORIZED(obj.message)
|
|
59
|
+
break
|
|
60
|
+
case 'WERR_NOT_ACTIVE':
|
|
61
|
+
e = new WERR_NOT_ACTIVE(obj.message)
|
|
62
|
+
break
|
|
63
|
+
case 'WERR_INSUFFICIENT_FUNDS':
|
|
64
|
+
e = new WERR_INSUFFICIENT_FUNDS(obj.totalSatoshisNeeded, obj.moreSatoshisNeeded)
|
|
65
|
+
break
|
|
66
|
+
case 'WERR_INVALID_PUBLIC_KEY':
|
|
67
|
+
e = new WERR_INVALID_PUBLIC_KEY(obj.key, 'mainnet')
|
|
68
|
+
e.message = obj.message
|
|
69
|
+
break
|
|
70
|
+
case 'WERR_REVIEW_ACTIONS':
|
|
71
|
+
e = new WERR_REVIEW_ACTIONS(obj.reviewActionResults, obj.sendWithResults, obj.txid, obj.tx, obj.noSendChange)
|
|
72
|
+
break
|
|
73
|
+
default:
|
|
74
|
+
{
|
|
75
|
+
e = new WalletError(obj.name || 'WERR_UNKNOWN', obj.message || '')
|
|
76
|
+
}
|
|
77
|
+
break
|
|
78
|
+
}
|
|
79
|
+
return e
|
|
80
|
+
}
|
|
@@ -572,7 +572,7 @@ export interface ReproveHeaderResult {
|
|
|
572
572
|
/**
|
|
573
573
|
* List of proven_txs records that were updated with new proof data.
|
|
574
574
|
*/
|
|
575
|
-
updated: { was: TableProvenTx; update: Partial<TableProvenTx
|
|
575
|
+
updated: { was: TableProvenTx; update: Partial<TableProvenTx>; logUpdate: string }[]
|
|
576
576
|
/**
|
|
577
577
|
* List of proven_txs records that were checked but currently available proof is unchanged.
|
|
578
578
|
*/
|
|
@@ -594,7 +594,7 @@ export interface ReproveProvenResult {
|
|
|
594
594
|
/**
|
|
595
595
|
* Valid if proof data for proven_txs record is available and has changed.
|
|
596
596
|
*/
|
|
597
|
-
updated?: { update: Partial<TableProvenTx
|
|
597
|
+
updated?: { update: Partial<TableProvenTx>; logUpdate: string }
|
|
598
598
|
/**
|
|
599
599
|
* True if proof data for proven_txs record was found to be unchanged.
|
|
600
600
|
*/
|
|
@@ -603,4 +603,4 @@ export interface ReproveProvenResult {
|
|
|
603
603
|
* True if proof data for proven_txs record is currently unavailable.
|
|
604
604
|
*/
|
|
605
605
|
unavailable: boolean
|
|
606
|
-
}
|
|
606
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { WalletError } from '../WalletError'
|
|
2
|
+
import { WalletErrorFromJson } from '../WalletErrorFromJson'
|
|
3
|
+
import {
|
|
4
|
+
WERR_NOT_IMPLEMENTED,
|
|
5
|
+
WERR_INTERNAL,
|
|
6
|
+
WERR_INVALID_PARAMETER,
|
|
7
|
+
WERR_REVIEW_ACTIONS,
|
|
8
|
+
WERR_INSUFFICIENT_FUNDS,
|
|
9
|
+
WERR_BROADCAST_UNAVAILABLE,
|
|
10
|
+
WERR_NETWORK_CHAIN,
|
|
11
|
+
WERR_INVALID_OPERATION,
|
|
12
|
+
WERR_MISSING_PARAMETER,
|
|
13
|
+
WERR_BAD_REQUEST,
|
|
14
|
+
WERR_UNAUTHORIZED,
|
|
15
|
+
WERR_NOT_ACTIVE,
|
|
16
|
+
WERR_INVALID_PUBLIC_KEY
|
|
17
|
+
} from '../WERR_errors'
|
|
18
|
+
|
|
19
|
+
// Mock WalletStorage interface
|
|
20
|
+
const mockWalletStorage = {
|
|
21
|
+
createAction: jest.fn().mockImplementation((args: any) => {
|
|
22
|
+
throw new WERR_REVIEW_ACTIONS(
|
|
23
|
+
[{ txid: 'txid123', status: 'doubleSpend', competingTxs: ['txid456'], competingBeef: [0, 1, 2, 3] }],
|
|
24
|
+
[{ txid: 'txid123', status: 'failed' }],
|
|
25
|
+
'txid123',
|
|
26
|
+
[5, 6, 7, 8],
|
|
27
|
+
['00'.repeat(32) + '.0']
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('WalletError tests', () => {
|
|
33
|
+
jest.setTimeout(99999999)
|
|
34
|
+
|
|
35
|
+
test('0 - WERR_REVIEW_ACTIONS from createAction failure', async () => {
|
|
36
|
+
try {
|
|
37
|
+
await mockWalletStorage.createAction({ someArgs: 'test' })
|
|
38
|
+
} catch (err) {
|
|
39
|
+
const werr = WalletError.fromUnknown(err)
|
|
40
|
+
expect(werr.name).toBe('WERR_REVIEW_ACTIONS')
|
|
41
|
+
expect(werr.message).toBe('Undelayed createAction or signAction results require review.')
|
|
42
|
+
|
|
43
|
+
const json = WalletError.unknownToJson(werr)
|
|
44
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
45
|
+
expect(werr2 instanceof WERR_REVIEW_ACTIONS).toBe(true)
|
|
46
|
+
const werr3 = werr2 as WERR_REVIEW_ACTIONS
|
|
47
|
+
expect(werr3.txid).toBe('txid123')
|
|
48
|
+
expect(werr3.reviewActionResults).toEqual([
|
|
49
|
+
{ txid: 'txid123', status: 'doubleSpend', competingTxs: ['txid456'], competingBeef: [0, 1, 2, 3] }
|
|
50
|
+
])
|
|
51
|
+
expect(werr3.sendWithResults).toEqual([{ txid: 'txid123', status: 'failed' }])
|
|
52
|
+
expect(werr3.noSendChange).toEqual(['00'.repeat(32) + '.0'])
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('1 - WERR_NOT_IMPLEMENTED basic test', async () => {
|
|
57
|
+
const werr = new WERR_NOT_IMPLEMENTED('Custom not implemented message')
|
|
58
|
+
expect(werr.name).toBe('WERR_NOT_IMPLEMENTED')
|
|
59
|
+
expect(werr.message).toBe('Custom not implemented message')
|
|
60
|
+
|
|
61
|
+
const json = WalletError.unknownToJson(werr)
|
|
62
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
63
|
+
expect(werr2.name).toBe('WERR_NOT_IMPLEMENTED')
|
|
64
|
+
expect(werr2.message).toBe('Custom not implemented message')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('2 - WERR_INTERNAL with default message', async () => {
|
|
68
|
+
const werr = new WERR_INTERNAL()
|
|
69
|
+
expect(werr.name).toBe('WERR_INTERNAL')
|
|
70
|
+
expect(werr.message).toBe('An internal error has occurred.')
|
|
71
|
+
|
|
72
|
+
const json = WalletError.unknownToJson(werr)
|
|
73
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
74
|
+
expect(werr2.name).toBe('WERR_INTERNAL')
|
|
75
|
+
expect(werr2.message).toBe('An internal error has occurred.')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('3 - WERR_INVALID_PARAMETER with custom parameter', async () => {
|
|
79
|
+
const werr = new WERR_INVALID_PARAMETER('amount', 'positive')
|
|
80
|
+
expect(werr.name).toBe('WERR_INVALID_PARAMETER')
|
|
81
|
+
expect(werr.message).toBe('The amount parameter must be positive')
|
|
82
|
+
expect(werr.parameter).toBe('amount')
|
|
83
|
+
|
|
84
|
+
const json = werr.toJson()
|
|
85
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
86
|
+
expect(werr2.name).toBe('WERR_INVALID_PARAMETER')
|
|
87
|
+
expect(werr2.message).toBe('The amount parameter must be positive')
|
|
88
|
+
expect((werr2 as WERR_INVALID_PARAMETER).parameter).toBe('amount')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('4 - WERR_INSUFFICIENT_FUNDS with numeric values', async () => {
|
|
92
|
+
const werr = new WERR_INSUFFICIENT_FUNDS(1000, 500)
|
|
93
|
+
expect(werr.name).toBe('WERR_INSUFFICIENT_FUNDS')
|
|
94
|
+
expect(werr.message).toContain('500 more satoshis are needed')
|
|
95
|
+
expect(werr.message).toContain('for a total of 1000')
|
|
96
|
+
expect(werr.totalSatoshisNeeded).toBe(1000)
|
|
97
|
+
expect(werr.moreSatoshisNeeded).toBe(500)
|
|
98
|
+
|
|
99
|
+
const json = werr.toJson()
|
|
100
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
101
|
+
expect(werr2.name).toBe('WERR_INSUFFICIENT_FUNDS')
|
|
102
|
+
expect((werr2 as WERR_INSUFFICIENT_FUNDS).totalSatoshisNeeded).toBe(1000)
|
|
103
|
+
expect((werr2 as WERR_INSUFFICIENT_FUNDS).moreSatoshisNeeded).toBe(500)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('5 - WERR_BROADCAST_UNAVAILABLE test', async () => {
|
|
107
|
+
const werr = new WERR_BROADCAST_UNAVAILABLE('Network issue')
|
|
108
|
+
expect(werr.name).toBe('WERR_BROADCAST_UNAVAILABLE')
|
|
109
|
+
expect(werr.message).toBe('Unable to broadcast transaction at this time.')
|
|
110
|
+
|
|
111
|
+
const json = WalletError.unknownToJson(werr)
|
|
112
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
113
|
+
expect(werr2.name).toBe('WERR_BROADCAST_UNAVAILABLE')
|
|
114
|
+
expect(werr2.message).toBe('Unable to broadcast transaction at this time.')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('6 - WERR_NETWORK_CHAIN test', async () => {
|
|
118
|
+
const werr = new WERR_NETWORK_CHAIN('Chain mismatch')
|
|
119
|
+
expect(werr.name).toBe('WERR_NETWORK_CHAIN')
|
|
120
|
+
expect(werr.message).toBe('Chain mismatch')
|
|
121
|
+
|
|
122
|
+
const json = WalletError.unknownToJson(werr)
|
|
123
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
124
|
+
expect(werr2.name).toBe('WERR_NETWORK_CHAIN')
|
|
125
|
+
expect(werr2.message).toBe('Chain mismatch')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('7 - WalletError.fromUnknown with plain Error', async () => {
|
|
129
|
+
const err = new Error('Test error')
|
|
130
|
+
const werr = WalletError.fromUnknown(err)
|
|
131
|
+
expect(werr.name).toBe('WERR_UNKNOWN')
|
|
132
|
+
expect(werr.message).toBe('Test error')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('8 - WalletError.unknownToJson with unknown object', async () => {
|
|
136
|
+
const obj = { custom: 'data', code: 404 }
|
|
137
|
+
const json = WalletError.unknownToJson(obj)
|
|
138
|
+
const werr = WalletErrorFromJson(JSON.parse(json))
|
|
139
|
+
expect(werr.name).toBe('WERR_UNKNOWN')
|
|
140
|
+
expect(werr.message).toBe('[object Object]')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('9 - WERR_INVALID_OPERATION basic test', async () => {
|
|
144
|
+
const werr = new WERR_INVALID_OPERATION('Custom invalid operation message')
|
|
145
|
+
expect(werr.name).toBe('WERR_INVALID_OPERATION')
|
|
146
|
+
expect(werr.message).toBe('Custom invalid operation message')
|
|
147
|
+
|
|
148
|
+
const json = WalletError.unknownToJson(werr)
|
|
149
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
150
|
+
expect(werr2.name).toBe('WERR_INVALID_OPERATION')
|
|
151
|
+
expect(werr2.message).toBe('Custom invalid operation message')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('10 - WERR_MISSING_PARAMETER with parameter', async () => {
|
|
155
|
+
const werr = new WERR_MISSING_PARAMETER('requiredField')
|
|
156
|
+
expect(werr.name).toBe('WERR_MISSING_PARAMETER')
|
|
157
|
+
expect(werr.message).toBe('The required requiredField parameter is missing.')
|
|
158
|
+
expect(werr.parameter).toBe('requiredField')
|
|
159
|
+
|
|
160
|
+
const json = werr.toJson()
|
|
161
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
162
|
+
expect(werr2.name).toBe('WERR_MISSING_PARAMETER')
|
|
163
|
+
expect(werr2.message).toBe('The required requiredField parameter is missing.')
|
|
164
|
+
expect((werr2 as WERR_MISSING_PARAMETER).parameter).toBe('requiredField')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('11 - WERR_BAD_REQUEST with custom message', async () => {
|
|
168
|
+
const werr = new WERR_BAD_REQUEST('Invalid request data')
|
|
169
|
+
expect(werr.name).toBe('WERR_BAD_REQUEST')
|
|
170
|
+
expect(werr.message).toBe('Invalid request data')
|
|
171
|
+
|
|
172
|
+
const json = WalletError.unknownToJson(werr)
|
|
173
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
174
|
+
expect(werr2.name).toBe('WERR_BAD_REQUEST')
|
|
175
|
+
expect(werr2.message).toBe('Invalid request data')
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('12 - WERR_UNAUTHORIZED with default message', async () => {
|
|
179
|
+
const werr = new WERR_UNAUTHORIZED()
|
|
180
|
+
expect(werr.name).toBe('WERR_UNAUTHORIZED')
|
|
181
|
+
expect(werr.message).toBe('Access is denied due to an authorization error.')
|
|
182
|
+
|
|
183
|
+
const json = WalletError.unknownToJson(werr)
|
|
184
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
185
|
+
expect(werr2.name).toBe('WERR_UNAUTHORIZED')
|
|
186
|
+
expect(werr2.message).toBe('Access is denied due to an authorization error.')
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('13 - WERR_NOT_ACTIVE with default message', async () => {
|
|
190
|
+
const werr = new WERR_NOT_ACTIVE()
|
|
191
|
+
expect(werr.name).toBe('WERR_NOT_ACTIVE')
|
|
192
|
+
expect(werr.message).toBe(
|
|
193
|
+
`WalletStorageManager is not accessing user's active storage or there are conflicting active stores configured.`
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
const json = WalletError.unknownToJson(werr)
|
|
197
|
+
const werr2 = WalletErrorFromJson(JSON.parse(json))
|
|
198
|
+
expect(werr2.name).toBe('WERR_NOT_ACTIVE')
|
|
199
|
+
expect(werr2.message).toBe(
|
|
200
|
+
`WalletStorageManager is not accessing user's active storage or there are conflicting active stores configured.`
|
|
201
|
+
)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('14 - WERR_INVALID_PUBLIC_KEY with key and mainnet network', async () => {
|
|
205
|
+
const werr = new WERR_INVALID_PUBLIC_KEY('invalidkey123', 'mainnet')
|
|
206
|
+
expect(werr.name).toBe('WERR_INVALID_PUBLIC_KEY')
|
|
207
|
+
expect(werr.message).toBe('The provided public key "invalidkey123" is invalid or malformed.')
|
|
208
|
+
expect((werr as WERR_INVALID_PUBLIC_KEY).key).toBe('invalidkey123')
|
|
209
|
+
|
|
210
|
+
const json = WalletError.unknownToJson(werr)
|
|
211
|
+
const parsedJson = JSON.parse(json)
|
|
212
|
+
const werr2 = WalletErrorFromJson(parsedJson)
|
|
213
|
+
expect(werr2.name).toBe('WERR_INVALID_PUBLIC_KEY')
|
|
214
|
+
expect(werr2.message).toBe('The provided public key "invalidkey123" is invalid or malformed.')
|
|
215
|
+
expect((werr2 as WERR_INVALID_PUBLIC_KEY).key).toBe('invalidkey123')
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('15 - WERR_INVALID_PUBLIC_KEY with key and testnet network', async () => {
|
|
219
|
+
const werr = new WERR_INVALID_PUBLIC_KEY('invalidkey123', 'testnet')
|
|
220
|
+
expect(werr.name).toBe('WERR_INVALID_PUBLIC_KEY')
|
|
221
|
+
expect(werr.message).toBe('The provided public key is invalid or malformed.')
|
|
222
|
+
expect((werr as WERR_INVALID_PUBLIC_KEY).key).toBe('invalidkey123')
|
|
223
|
+
|
|
224
|
+
const json = WalletError.unknownToJson(werr)
|
|
225
|
+
const parsedJson = JSON.parse(json)
|
|
226
|
+
const werr2 = WalletErrorFromJson(parsedJson)
|
|
227
|
+
expect(werr2.name).toBe('WERR_INVALID_PUBLIC_KEY')
|
|
228
|
+
expect(werr2.message).toBe('The provided public key is invalid or malformed.')
|
|
229
|
+
expect((werr2 as WERR_INVALID_PUBLIC_KEY).key).toBe('invalidkey123')
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('16 - WalletError basic constructor with details and stack', async () => {
|
|
233
|
+
const customStack = 'custom stack trace'
|
|
234
|
+
const werr = new WalletError('WERR_TEST', 'Test message', customStack, { detail1: 'value1', detail2: 'value2' })
|
|
235
|
+
expect(werr.isError).toBe(true)
|
|
236
|
+
expect(werr.name).toBe('WERR_TEST')
|
|
237
|
+
expect(werr.message).toBe('Test message')
|
|
238
|
+
expect(werr.stack).toBe(customStack)
|
|
239
|
+
expect(werr.details).toEqual({ detail1: 'value1', detail2: 'value2' })
|
|
240
|
+
expect(werr.code).toBe('WERR_TEST')
|
|
241
|
+
expect(werr.description).toBe('Test message')
|
|
242
|
+
|
|
243
|
+
werr.code = 'WERR_NEW_CODE'
|
|
244
|
+
werr.description = 'New description'
|
|
245
|
+
expect(werr.name).toBe('WERR_NEW_CODE')
|
|
246
|
+
expect(werr.message).toBe('New description')
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('17 - WalletError.fromUnknown with string', async () => {
|
|
250
|
+
const werr = WalletError.fromUnknown('String error message')
|
|
251
|
+
expect(werr.name).toBe('WERR_UNKNOWN')
|
|
252
|
+
expect(werr.message).toBe('String error message')
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('18 - WalletError.fromUnknown with number', async () => {
|
|
256
|
+
const werr = WalletError.fromUnknown(404)
|
|
257
|
+
expect(werr.name).toBe('WERR_UNKNOWN')
|
|
258
|
+
expect(werr.message).toBe('404')
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('19 - WalletError.fromUnknown with custom object', async () => {
|
|
262
|
+
const obj = { code: 'ERR_404', message: 'Not found', status: 404 }
|
|
263
|
+
const werr = WalletError.fromUnknown(obj)
|
|
264
|
+
expect(werr.name).toBe('ERR_404')
|
|
265
|
+
expect(werr.message).toBe('Not found')
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
test('20 - WalletError.fromUnknown with nested walletError', async () => {
|
|
269
|
+
const innerErr = new WERR_INTERNAL('Inner error')
|
|
270
|
+
const outerObj = { message: 'Outer error', walletError: innerErr }
|
|
271
|
+
const werr = WalletError.fromUnknown(outerObj)
|
|
272
|
+
expect(werr.name).toBe('WERR_UNKNOWN')
|
|
273
|
+
expect(werr.message).toBe('Outer error')
|
|
274
|
+
expect((werr as any).walletError).toBeInstanceOf(WERR_INTERNAL)
|
|
275
|
+
expect((werr as any).walletError.message).toBe('Inner error')
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
test('21 - WalletError.fromUnknown with SQL details', async () => {
|
|
279
|
+
const err = { name: 'DBError', message: 'Query failed', sql: 'SELECT * FROM table', sqlMessage: 'Syntax error' }
|
|
280
|
+
const werr = WalletError.fromUnknown(err)
|
|
281
|
+
expect(werr.name).toBe('DBError')
|
|
282
|
+
expect(werr.message).toBe('Query failed')
|
|
283
|
+
expect(werr.details).toEqual({ sql: 'SELECT * FROM table', sqlMessage: 'Syntax error' })
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
test('22 - WalletError.unknownToJson with WalletError', async () => {
|
|
287
|
+
const werr = new WalletError('WERR_TEST', 'Test message')
|
|
288
|
+
const json = WalletError.unknownToJson(werr)
|
|
289
|
+
const parsed = JSON.parse(json)
|
|
290
|
+
expect(parsed.name).toBe('WERR_TEST')
|
|
291
|
+
expect(parsed.message).toBe('Test message')
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
test('23 - WalletError.unknownToJson with standard Error', async () => {
|
|
295
|
+
const err = new Error('Standard error')
|
|
296
|
+
const json = WalletError.unknownToJson(err)
|
|
297
|
+
const werr = WalletErrorFromJson(JSON.parse(json))
|
|
298
|
+
expect(werr.name).toBe('Error')
|
|
299
|
+
expect(werr.message).toBe('Standard error')
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
test('24 - WalletError.unknownToJson with string', async () => {
|
|
303
|
+
const json = WalletError.unknownToJson('String error')
|
|
304
|
+
const werr = WalletErrorFromJson(JSON.parse(json))
|
|
305
|
+
expect(werr.name).toBe('WERR_UNKNOWN')
|
|
306
|
+
expect(werr.message).toBe('String error')
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
test('25 - WalletError asStatus method', async () => {
|
|
310
|
+
const werr = new WalletError('WERR_TEST', 'Test description')
|
|
311
|
+
const status = werr.asStatus()
|
|
312
|
+
expect(status).toEqual({
|
|
313
|
+
status: 'error',
|
|
314
|
+
code: 'WERR_TEST',
|
|
315
|
+
description: 'Test description'
|
|
316
|
+
})
|
|
317
|
+
})
|
|
318
|
+
})
|
package/src/sdk/index.ts
CHANGED
|
@@ -20,7 +20,7 @@ export class ChaintracksChainTracker implements ChainTracker {
|
|
|
20
20
|
chain ||= 'main'
|
|
21
21
|
this.chaintracks =
|
|
22
22
|
chaintracks ??
|
|
23
|
-
new ChaintracksServiceClient(chain, `https
|
|
23
|
+
new ChaintracksServiceClient(chain, `https://${chain}net-chaintracks.babbage.systems`)
|
|
24
24
|
this.cache = {}
|
|
25
25
|
this.options = options || {}
|
|
26
26
|
}
|
|
@@ -12,20 +12,27 @@ export class ChaintracksFetch implements ChaintracksFetchApi {
|
|
|
12
12
|
constructor() {}
|
|
13
13
|
|
|
14
14
|
async download(url: string): Promise<Uint8Array> {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
for (let retry = 0; ; retry++) {
|
|
16
|
+
const response = await fetch(url, {
|
|
17
|
+
method: 'GET',
|
|
18
|
+
headers: {
|
|
19
|
+
'Content-Type': 'application/octet-stream'
|
|
20
|
+
}
|
|
21
|
+
})
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
if (response.statusText === 'Too Many Requests' && retry < 3) {
|
|
25
|
+
// WhatsOnChain rate limits requests, so backoff and retry
|
|
26
|
+
await wait(1000 * (retry + 1))
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
throw new Error(`Failed to download from ${url}: ${response.statusText}`)
|
|
30
|
+
}
|
|
25
31
|
|
|
26
|
-
|
|
32
|
+
const data = await response.arrayBuffer()
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
return new Uint8Array(data)
|
|
35
|
+
}
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
async fetchJson<R>(url: string): Promise<R> {
|