react-native-pirate-wallet 0.3.2 → 0.3.3
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/README.md +79 -0
- package/package.json +6 -6
- package/src/index.d.ts +57 -0
- package/src/index.js +83 -0
- package/test/smoke.js +91 -0
package/README.md
CHANGED
|
@@ -240,6 +240,85 @@ active wallet between running wallets.
|
|
|
240
240
|
- `coinType`
|
|
241
241
|
- `rpcPort`
|
|
242
242
|
- `defaultBirthday`
|
|
243
|
+
|
|
244
|
+
### Lightwalletd endpoints and failover pools
|
|
245
|
+
|
|
246
|
+
Endpoint configuration is wallet-scoped. The recommended integration flow is
|
|
247
|
+
to test candidate servers, save one primary plus its alternates, and then start
|
|
248
|
+
or restart that wallet's synchronizer:
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
const primary = 'https://lightd1.pirate.black:443'
|
|
252
|
+
const alternates = [
|
|
253
|
+
'https://lightwalletd1.cryptoforge.cc:443',
|
|
254
|
+
'https://pirate.mathnodes.com:443'
|
|
255
|
+
]
|
|
256
|
+
|
|
257
|
+
const tests = await Promise.all(
|
|
258
|
+
[primary, ...alternates].map(url => sdk.testLightdEndpoint({ url }))
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
if (tests.every(result => result.success)) {
|
|
262
|
+
await sdk.setLightdEndpointPool({
|
|
263
|
+
walletId,
|
|
264
|
+
url: primary,
|
|
265
|
+
failoverEndpoints: alternates
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const saved = await sdk.getLightdEndpointConfig(walletId)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
- `getLightdEndpoint(walletId)`
|
|
273
|
+
- RPC: `get_lightd_endpoint`
|
|
274
|
+
- returns the effective primary endpoint URL
|
|
275
|
+
- `getLightdEndpointConfig(walletId)`
|
|
276
|
+
- RPC: `get_lightd_endpoint_config`
|
|
277
|
+
- returns `LightdEndpointConfig`:
|
|
278
|
+
- `host`
|
|
279
|
+
- `port`
|
|
280
|
+
- `useTls`
|
|
281
|
+
- `tlsPin`
|
|
282
|
+
- `label`
|
|
283
|
+
- `automaticFailover`
|
|
284
|
+
- `failoverEndpoints`
|
|
285
|
+
- `isConfigured`
|
|
286
|
+
- `testLightdEndpoint({ url, tlsPin? })`
|
|
287
|
+
- RPC: `test_node`
|
|
288
|
+
- also accepts `testLightdEndpoint(url, tlsPin?)`
|
|
289
|
+
- tests through the currently selected Direct, Tor, SOCKS5, or I2P transport
|
|
290
|
+
- reports success, height, latency, transport, TLS/pin information, server
|
|
291
|
+
version, chain name, and any connection error
|
|
292
|
+
- `setLightdEndpoint({ walletId, url, tlsPin? })`
|
|
293
|
+
- RPC: `set_lightd_endpoint`
|
|
294
|
+
- also accepts `setLightdEndpoint(walletId, url, tlsPin?)`
|
|
295
|
+
- saves one primary and clears any previously configured failover pool
|
|
296
|
+
- `setLightdEndpointPool({ walletId, url, failoverEndpoints, tlsPin? })`
|
|
297
|
+
- RPC: `set_lightd_endpoint_pool`
|
|
298
|
+
- also accepts
|
|
299
|
+
`setLightdEndpointPool(walletId, url, failoverEndpoints, tlsPin?)`
|
|
300
|
+
- saves the primary and up to 16 explicit alternates
|
|
301
|
+
- an empty `failoverEndpoints` array disables automatic failover
|
|
302
|
+
|
|
303
|
+
Pool membership is validated by the backend before anything is persisted.
|
|
304
|
+
Every member must resolve to the same recognized Pirate network, use the same
|
|
305
|
+
clearnet, onion, or I2P route, and use the same HTTP/TLS security mode as the
|
|
306
|
+
primary. The primary is removed from the alternate list and duplicate
|
|
307
|
+
alternates are collapsed. A pinned primary cannot use automatic failover,
|
|
308
|
+
because one server's SPKI pin cannot authenticate unrelated servers; use
|
|
309
|
+
`setLightdEndpoint()` when pinning a single server.
|
|
310
|
+
|
|
311
|
+
Saving either endpoint form cancels an existing sync session for that wallet so
|
|
312
|
+
it cannot continue against stale connection state. Restart the synchronizer
|
|
313
|
+
after the setter resolves. Pool candidates are still checked for compatible
|
|
314
|
+
chain identity and history before failover or historical work is assigned; the
|
|
315
|
+
array order is not a request to trust a candidate blindly.
|
|
316
|
+
|
|
317
|
+
`testLightdEndpoint()` returns a structured failure result for connection-level
|
|
318
|
+
failures. Invalid setter input or a rejected pool throws through the normal SDK
|
|
319
|
+
promise, so applications should show the error and retain the previous saved
|
|
320
|
+
configuration.
|
|
321
|
+
|
|
243
322
|
- `formatAmount(arrrtoshis)`
|
|
244
323
|
- RPC: `format_amount`
|
|
245
324
|
- returns formatted string
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-pirate-wallet",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "React Native wrapper for the Pirate Unified Wallet native SDK surfaces",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
"react-native": ">=0.71.0 <1.0.0"
|
|
53
53
|
},
|
|
54
54
|
"optionalDependencies": {
|
|
55
|
-
"react-native-pirate-wallet-android": "0.3.
|
|
56
|
-
"react-native-pirate-wallet-android-x86_64": "0.3.
|
|
57
|
-
"react-native-pirate-wallet-ios-device": "0.3.
|
|
58
|
-
"react-native-pirate-wallet-ios-simulator-arm64": "0.3.
|
|
59
|
-
"react-native-pirate-wallet-ios-simulator-x86_64": "0.3.
|
|
55
|
+
"react-native-pirate-wallet-android": "0.3.3",
|
|
56
|
+
"react-native-pirate-wallet-android-x86_64": "0.3.3",
|
|
57
|
+
"react-native-pirate-wallet-ios-device": "0.3.3",
|
|
58
|
+
"react-native-pirate-wallet-ios-simulator-arm64": "0.3.3",
|
|
59
|
+
"react-native-pirate-wallet-ios-simulator-x86_64": "0.3.3"
|
|
60
60
|
},
|
|
61
61
|
"publishConfig": {
|
|
62
62
|
"access": "public"
|
package/src/index.d.ts
CHANGED
|
@@ -42,6 +42,50 @@ export interface PirateWalletAccountStorageConfig {
|
|
|
42
42
|
storagePath?: string | null
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
export interface LightdEndpointConfig {
|
|
46
|
+
host: string
|
|
47
|
+
port: number
|
|
48
|
+
useTls: boolean
|
|
49
|
+
tlsPin: string | null
|
|
50
|
+
label: string | null
|
|
51
|
+
automaticFailover: boolean
|
|
52
|
+
failoverEndpoints: string[]
|
|
53
|
+
isConfigured: boolean
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SetLightdEndpointRequest {
|
|
57
|
+
walletId: string
|
|
58
|
+
url: string
|
|
59
|
+
tlsPin?: string | null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SetLightdEndpointPoolRequest extends SetLightdEndpointRequest {
|
|
63
|
+
failoverEndpoints: string[]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface TestLightdEndpointRequest {
|
|
67
|
+
url: string
|
|
68
|
+
tlsPin?: string | null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface NodeTestResult {
|
|
72
|
+
success: boolean
|
|
73
|
+
latestBlockHeight: number | null
|
|
74
|
+
transportMode: string
|
|
75
|
+
tlsEnabled: boolean
|
|
76
|
+
tlsPinMatched: boolean | null
|
|
77
|
+
expectedPin: string | null
|
|
78
|
+
actualPin: string | null
|
|
79
|
+
errorMessage: string | null
|
|
80
|
+
responseTimeMs: number
|
|
81
|
+
serverVersion: string | null
|
|
82
|
+
chainName: string | null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface Acknowledgement {
|
|
86
|
+
acknowledged: true
|
|
87
|
+
}
|
|
88
|
+
|
|
45
89
|
export interface SynchronizerSnapshot {
|
|
46
90
|
walletId: string
|
|
47
91
|
alias: string
|
|
@@ -209,6 +253,19 @@ export class PirateWalletSdk {
|
|
|
209
253
|
isValidShieldedAddr(address: string): Promise<boolean>
|
|
210
254
|
validateAddress(address: string): Promise<any>
|
|
211
255
|
validateConsensusBranch(walletId: string): Promise<any>
|
|
256
|
+
getLightdEndpoint(walletId: string): Promise<string>
|
|
257
|
+
getLightdEndpointConfig(walletId: string): Promise<LightdEndpointConfig>
|
|
258
|
+
setLightdEndpoint(request: SetLightdEndpointRequest): Promise<Acknowledgement>
|
|
259
|
+
setLightdEndpoint(walletId: string, url: string, tlsPin?: string | null): Promise<Acknowledgement>
|
|
260
|
+
setLightdEndpointPool(request: SetLightdEndpointPoolRequest): Promise<Acknowledgement>
|
|
261
|
+
setLightdEndpointPool(
|
|
262
|
+
walletId: string,
|
|
263
|
+
url: string,
|
|
264
|
+
failoverEndpoints: string[],
|
|
265
|
+
tlsPin?: string | null
|
|
266
|
+
): Promise<Acknowledgement>
|
|
267
|
+
testLightdEndpoint(request: TestLightdEndpointRequest): Promise<NodeTestResult>
|
|
268
|
+
testLightdEndpoint(url: string, tlsPin?: string | null): Promise<NodeTestResult>
|
|
212
269
|
formatAmount(arrrtoshis: AmountInput): Promise<string>
|
|
213
270
|
parseAmount(arrr: string): Promise<AmountString>
|
|
214
271
|
getCurrentReceiveAddress(walletId: string): Promise<string>
|
package/src/index.js
CHANGED
|
@@ -83,6 +83,33 @@ function buildRequest(method, params = {}) {
|
|
|
83
83
|
return JSON.stringify(request)
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
function requireNonEmptyString(value, name) {
|
|
87
|
+
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
88
|
+
throw new Error(`${name} must be a non-empty string.`)
|
|
89
|
+
}
|
|
90
|
+
return value
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function optionalNonEmptyString(value, name) {
|
|
94
|
+
if (value === undefined || value === null) {
|
|
95
|
+
return null
|
|
96
|
+
}
|
|
97
|
+
return requireNonEmptyString(value, name)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function validateFailoverEndpoints(value) {
|
|
101
|
+
if (!Array.isArray(value)) {
|
|
102
|
+
throw new Error('failoverEndpoints must be an array of endpoint URL strings.')
|
|
103
|
+
}
|
|
104
|
+
if (value.length > 16) {
|
|
105
|
+
throw new Error('failoverEndpoints may contain at most 16 endpoint URLs.')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return value.map((endpoint, index) =>
|
|
109
|
+
requireNonEmptyString(endpoint, `failoverEndpoints[${index}]`)
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
86
113
|
function unwrapEnvelope(responseJson, method, options = {}) {
|
|
87
114
|
const { camelizeResult = true } = options
|
|
88
115
|
let envelope
|
|
@@ -667,6 +694,62 @@ class PirateWalletSdk {
|
|
|
667
694
|
return this._call('validate_consensus_branch', { wallet_id: walletId })
|
|
668
695
|
}
|
|
669
696
|
|
|
697
|
+
getLightdEndpoint(walletId) {
|
|
698
|
+
return this._call('get_lightd_endpoint', {
|
|
699
|
+
wallet_id: requireNonEmptyString(walletId, 'walletId')
|
|
700
|
+
})
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
getLightdEndpointConfig(walletId) {
|
|
704
|
+
return this._call('get_lightd_endpoint_config', {
|
|
705
|
+
wallet_id: requireNonEmptyString(walletId, 'walletId')
|
|
706
|
+
})
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
setLightdEndpoint(requestOrWalletId, url = null, tlsPin = null) {
|
|
710
|
+
const request =
|
|
711
|
+
typeof requestOrWalletId === 'object' && requestOrWalletId !== null
|
|
712
|
+
? requestOrWalletId
|
|
713
|
+
: { walletId: requestOrWalletId, url, tlsPin }
|
|
714
|
+
|
|
715
|
+
return this._call('set_lightd_endpoint', {
|
|
716
|
+
wallet_id: requireNonEmptyString(request.walletId, 'walletId'),
|
|
717
|
+
url: requireNonEmptyString(request.url, 'url'),
|
|
718
|
+
tls_pin_opt: optionalNonEmptyString(request.tlsPin, 'tlsPin')
|
|
719
|
+
})
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
setLightdEndpointPool(
|
|
723
|
+
requestOrWalletId,
|
|
724
|
+
url = null,
|
|
725
|
+
failoverEndpoints = [],
|
|
726
|
+
tlsPin = null
|
|
727
|
+
) {
|
|
728
|
+
const request =
|
|
729
|
+
typeof requestOrWalletId === 'object' && requestOrWalletId !== null
|
|
730
|
+
? requestOrWalletId
|
|
731
|
+
: { walletId: requestOrWalletId, url, failoverEndpoints, tlsPin }
|
|
732
|
+
|
|
733
|
+
return this._call('set_lightd_endpoint_pool', {
|
|
734
|
+
wallet_id: requireNonEmptyString(request.walletId, 'walletId'),
|
|
735
|
+
url: requireNonEmptyString(request.url, 'url'),
|
|
736
|
+
tls_pin_opt: optionalNonEmptyString(request.tlsPin, 'tlsPin'),
|
|
737
|
+
failover_endpoints: validateFailoverEndpoints(request.failoverEndpoints)
|
|
738
|
+
})
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
testLightdEndpoint(requestOrUrl, tlsPin = null) {
|
|
742
|
+
const request =
|
|
743
|
+
typeof requestOrUrl === 'object' && requestOrUrl !== null
|
|
744
|
+
? requestOrUrl
|
|
745
|
+
: { url: requestOrUrl, tlsPin }
|
|
746
|
+
|
|
747
|
+
return this._call('test_node', {
|
|
748
|
+
url: requireNonEmptyString(request.url, 'url'),
|
|
749
|
+
tls_pin: optionalNonEmptyString(request.tlsPin, 'tlsPin')
|
|
750
|
+
})
|
|
751
|
+
}
|
|
752
|
+
|
|
670
753
|
formatAmount(arrrtoshis) {
|
|
671
754
|
return this._call('format_amount', { arrrtoshis })
|
|
672
755
|
}
|
package/test/smoke.js
CHANGED
|
@@ -48,6 +48,51 @@ function createMockNativeModule() {
|
|
|
48
48
|
])
|
|
49
49
|
case 'get_active_wallet':
|
|
50
50
|
return ok('wallet-1')
|
|
51
|
+
case 'get_lightd_endpoint':
|
|
52
|
+
assert.strictEqual(request.wallet_id, 'wallet-1')
|
|
53
|
+
return ok('https://lightd1.pirate.black:443')
|
|
54
|
+
case 'get_lightd_endpoint_config':
|
|
55
|
+
assert.strictEqual(request.wallet_id, 'wallet-1')
|
|
56
|
+
return ok({
|
|
57
|
+
host: 'lightd1.pirate.black',
|
|
58
|
+
port: 443,
|
|
59
|
+
use_tls: true,
|
|
60
|
+
tls_pin: null,
|
|
61
|
+
label: 'Primary',
|
|
62
|
+
automatic_failover: true,
|
|
63
|
+
failover_endpoints: ['https://lightwalletd1.cryptoforge.cc:443'],
|
|
64
|
+
is_configured: true
|
|
65
|
+
})
|
|
66
|
+
case 'set_lightd_endpoint':
|
|
67
|
+
assert.strictEqual(request.wallet_id, 'wallet-1')
|
|
68
|
+
assert.strictEqual(request.url, 'https://lightd1.pirate.black:443')
|
|
69
|
+
assert.strictEqual(request.tls_pin_opt, 'base64-spki-pin')
|
|
70
|
+
return ok({ acknowledged: true })
|
|
71
|
+
case 'set_lightd_endpoint_pool':
|
|
72
|
+
assert.strictEqual(request.wallet_id, 'wallet-1')
|
|
73
|
+
assert.strictEqual(request.url, 'https://lightd1.pirate.black:443')
|
|
74
|
+
assert.strictEqual(request.tls_pin_opt, undefined)
|
|
75
|
+
assert.deepStrictEqual(request.failover_endpoints, [
|
|
76
|
+
'https://lightwalletd1.cryptoforge.cc:443',
|
|
77
|
+
'https://pirate.mathnodes.com:443'
|
|
78
|
+
])
|
|
79
|
+
return ok({ acknowledged: true })
|
|
80
|
+
case 'test_node':
|
|
81
|
+
assert.strictEqual(request.url, 'https://lightwalletd1.cryptoforge.cc:443')
|
|
82
|
+
assert.strictEqual(request.tls_pin, undefined)
|
|
83
|
+
return ok({
|
|
84
|
+
success: true,
|
|
85
|
+
latest_block_height: 4200000,
|
|
86
|
+
transport_mode: 'Direct',
|
|
87
|
+
tls_enabled: true,
|
|
88
|
+
tls_pin_matched: null,
|
|
89
|
+
expected_pin: null,
|
|
90
|
+
actual_pin: 'observed-pin',
|
|
91
|
+
error_message: null,
|
|
92
|
+
response_time_ms: 95,
|
|
93
|
+
server_version: 'lightwalletd',
|
|
94
|
+
chain_name: 'main'
|
|
95
|
+
})
|
|
51
96
|
case 'current_receive_address':
|
|
52
97
|
assert.strictEqual(request.wallet_id, 'wallet-1')
|
|
53
98
|
return ok('pirate1current')
|
|
@@ -164,6 +209,52 @@ async function main() {
|
|
|
164
209
|
const latestBirthdayHeight = await sdk.getLatestBirthdayHeight('wallet-1')
|
|
165
210
|
assert.strictEqual(latestBirthdayHeight, 345678)
|
|
166
211
|
|
|
212
|
+
const endpoint = await sdk.getLightdEndpoint('wallet-1')
|
|
213
|
+
assert.strictEqual(endpoint, 'https://lightd1.pirate.black:443')
|
|
214
|
+
|
|
215
|
+
const endpointConfig = await sdk.getLightdEndpointConfig('wallet-1')
|
|
216
|
+
assert.strictEqual(endpointConfig.automaticFailover, true)
|
|
217
|
+
assert.deepStrictEqual(endpointConfig.failoverEndpoints, [
|
|
218
|
+
'https://lightwalletd1.cryptoforge.cc:443'
|
|
219
|
+
])
|
|
220
|
+
|
|
221
|
+
const endpointTest = await sdk.testLightdEndpoint(
|
|
222
|
+
'https://lightwalletd1.cryptoforge.cc:443'
|
|
223
|
+
)
|
|
224
|
+
assert.strictEqual(endpointTest.latestBlockHeight, 4200000)
|
|
225
|
+
assert.strictEqual(endpointTest.responseTimeMs, 95)
|
|
226
|
+
|
|
227
|
+
const endpointAck = await sdk.setLightdEndpoint({
|
|
228
|
+
walletId: 'wallet-1',
|
|
229
|
+
url: 'https://lightd1.pirate.black:443',
|
|
230
|
+
tlsPin: 'base64-spki-pin'
|
|
231
|
+
})
|
|
232
|
+
assert.strictEqual(endpointAck.acknowledged, true)
|
|
233
|
+
|
|
234
|
+
const poolAck = await sdk.setLightdEndpointPool({
|
|
235
|
+
walletId: 'wallet-1',
|
|
236
|
+
url: 'https://lightd1.pirate.black:443',
|
|
237
|
+
failoverEndpoints: [
|
|
238
|
+
'https://lightwalletd1.cryptoforge.cc:443',
|
|
239
|
+
'https://pirate.mathnodes.com:443'
|
|
240
|
+
]
|
|
241
|
+
})
|
|
242
|
+
assert.strictEqual(poolAck.acknowledged, true)
|
|
243
|
+
|
|
244
|
+
assert.throws(
|
|
245
|
+
() =>
|
|
246
|
+
sdk.setLightdEndpointPool({
|
|
247
|
+
walletId: 'wallet-1',
|
|
248
|
+
url: 'https://lightd1.pirate.black:443',
|
|
249
|
+
failoverEndpoints: 'not-an-array'
|
|
250
|
+
}),
|
|
251
|
+
/failoverEndpoints must be an array/
|
|
252
|
+
)
|
|
253
|
+
assert.throws(
|
|
254
|
+
() => sdk.testLightdEndpoint(' '),
|
|
255
|
+
/url must be a non-empty string/
|
|
256
|
+
)
|
|
257
|
+
|
|
167
258
|
assert.strictEqual(await sdk.getCurrentAddress('wallet-1'), 'pirate1current')
|
|
168
259
|
assert.strictEqual(await sdk.getNextAddress('wallet-1'), 'pirate1next')
|
|
169
260
|
|