@bsv/wallet-toolbox 1.1.5 → 1.1.6
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/docs/README.md +1 -1
- package/docs/client.md +698 -442
- package/docs/monitor.md +54 -37
- package/docs/services.md +53 -34
- package/docs/setup.md +295 -38
- package/docs/storage.md +115 -103
- package/docs/wallet.md +697 -442
- package/out/src/Setup.d.ts +3 -3
- package/out/src/Setup.js +5 -5
- package/out/src/SetupClient.d.ts +105 -12
- package/out/src/SetupClient.d.ts.map +1 -1
- package/out/src/SetupClient.js +3 -4
- package/out/src/SetupClient.js.map +1 -1
- package/out/src/Wallet.d.ts +4 -0
- package/out/src/Wallet.d.ts.map +1 -1
- package/out/src/Wallet.js +7 -0
- package/out/src/Wallet.js.map +1 -1
- package/out/src/sdk/WalletError.d.ts +0 -1
- package/out/src/sdk/WalletError.d.ts.map +1 -1
- package/out/src/sdk/WalletError.js +0 -1
- package/out/src/sdk/WalletError.js.map +1 -1
- package/out/src/sdk/validationHelpers.d.ts +1 -0
- package/out/src/sdk/validationHelpers.d.ts.map +1 -1
- package/out/src/sdk/validationHelpers.js +2 -1
- package/out/src/sdk/validationHelpers.js.map +1 -1
- package/out/src/storage/StorageProvider.d.ts +1 -4
- package/out/src/storage/StorageProvider.d.ts.map +1 -1
- package/out/src/storage/StorageProvider.js +1 -4
- package/out/src/storage/StorageProvider.js.map +1 -1
- package/out/src/storage/methods/createAction.d.ts.map +1 -1
- package/out/src/storage/methods/createAction.js +42 -11
- package/out/src/storage/methods/createAction.js.map +1 -1
- package/out/src/storage/methods/generateChange.js +1 -1
- package/out/src/storage/methods/generateChange.js.map +1 -1
- package/out/src/storage/schema/tables/ProvenTxReq.d.ts +4 -4
- package/out/test/examples/README.man.test.js +1 -1
- package/out/test/utils/TestUtilsWalletStorage.d.ts +2 -1
- package/out/test/utils/TestUtilsWalletStorage.d.ts.map +1 -1
- package/out/test/utils/TestUtilsWalletStorage.js +112 -27
- package/out/test/utils/TestUtilsWalletStorage.js.map +1 -1
- package/out/test/wallet/action/createAction.test.js +31 -5
- package/out/test/wallet/action/createAction.test.js.map +1 -1
- package/out/test/wallet/action/createAction2.test.d.ts +37 -0
- package/out/test/wallet/action/createAction2.test.d.ts.map +1 -0
- package/out/test/wallet/action/createAction2.test.js +1145 -0
- package/out/test/wallet/action/createAction2.test.js.map +1 -0
- package/out/tsconfig.all.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/Setup.ts +5 -5
- package/src/SetupClient.ts +107 -15
- package/src/Wallet.ts +9 -0
- package/src/sdk/WalletError.ts +0 -1
- package/src/sdk/validationHelpers.ts +3 -1
- package/src/storage/StorageProvider.ts +1 -4
- package/src/storage/methods/createAction.ts +48 -12
- package/src/storage/methods/generateChange.ts +1 -1
- package/src/storage/schema/tables/ProvenTxReq.ts +4 -4
- package/test/examples/README.man.test.ts +1 -1
- package/test/utils/TestUtilsWalletStorage.ts +131 -35
- package/test/wallet/action/createAction.test.ts +36 -5
- package/test/wallet/action/createAction2.test.ts +1400 -0
- package/ts2md.json +6 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bsv/wallet-toolbox",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"description": "BRC100 conforming wallet, wallet storage and wallet signer components",
|
|
5
5
|
"main": "./out/src/index.js",
|
|
6
6
|
"types": "./out/src/index.d.ts",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"standard": "^17.1.0",
|
|
58
58
|
"ts-jest": "^29.0.5",
|
|
59
59
|
"ts-node": "^10.9.1",
|
|
60
|
-
"ts2md": "^0.2.
|
|
60
|
+
"ts2md": "^0.2.6",
|
|
61
61
|
"typescript": "^5.2.2"
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/Setup.ts
CHANGED
|
@@ -33,7 +33,7 @@ export abstract class Setup extends SetupClient {
|
|
|
33
33
|
*
|
|
34
34
|
* @publicbody
|
|
35
35
|
*/
|
|
36
|
-
static async
|
|
36
|
+
static async createWalletKnex(
|
|
37
37
|
args: SetupWalletKnexArgs
|
|
38
38
|
): Promise<SetupWalletKnex> {
|
|
39
39
|
const wo = await Setup.createWallet(args)
|
|
@@ -91,10 +91,10 @@ export abstract class Setup extends SetupClient {
|
|
|
91
91
|
/**
|
|
92
92
|
* @publicbody
|
|
93
93
|
*/
|
|
94
|
-
static async
|
|
94
|
+
static async createWalletMySQL(
|
|
95
95
|
args: SetupWalletMySQLArgs
|
|
96
96
|
): Promise<SetupWalletKnex> {
|
|
97
|
-
return await this.
|
|
97
|
+
return await this.createWalletKnex({
|
|
98
98
|
...args,
|
|
99
99
|
knex: Setup.createMySQLKnex(args.env.mySQLConnection, args.databaseName)
|
|
100
100
|
})
|
|
@@ -103,10 +103,10 @@ export abstract class Setup extends SetupClient {
|
|
|
103
103
|
/**
|
|
104
104
|
* @publicbody
|
|
105
105
|
*/
|
|
106
|
-
static async
|
|
106
|
+
static async createWalletSQLite(
|
|
107
107
|
args: SetupWalletSQLiteArgs
|
|
108
108
|
): Promise<SetupWalletKnex> {
|
|
109
|
-
return await this.
|
|
109
|
+
return await this.createWalletKnex({
|
|
110
110
|
...args,
|
|
111
111
|
knex: Setup.createSQLiteKnex(args.filePath)
|
|
112
112
|
})
|
package/src/SetupClient.ts
CHANGED
|
@@ -132,12 +132,11 @@ export abstract class SetupClient {
|
|
|
132
132
|
* @publicbody
|
|
133
133
|
*/
|
|
134
134
|
static async createWallet(args: SetupWalletArgs): Promise<SetupWallet> {
|
|
135
|
-
|
|
135
|
+
const chain = args.env.chain
|
|
136
136
|
args.rootKeyHex ||= args.env.devKeys[args.env.identityKey]
|
|
137
137
|
const rootKey = PrivateKey.fromHex(args.rootKeyHex)
|
|
138
138
|
const identityKey = rootKey.toPublicKey().toString()
|
|
139
139
|
const keyDeriver = new KeyDeriver(rootKey)
|
|
140
|
-
const chain = args.chain
|
|
141
140
|
const storage = new WalletStorageManager(
|
|
142
141
|
identityKey,
|
|
143
142
|
args.active,
|
|
@@ -146,7 +145,7 @@ export abstract class SetupClient {
|
|
|
146
145
|
if (storage.stores.length > 0) await storage.makeAvailable()
|
|
147
146
|
const serviceOptions = Services.createDefaultOptions(chain)
|
|
148
147
|
serviceOptions.taalApiKey = args.env.taalApiKey
|
|
149
|
-
const services = new Services(
|
|
148
|
+
const services = new Services(chain)
|
|
150
149
|
const monopts = Monitor.createDefaultWalletMonitorOptions(
|
|
151
150
|
chain,
|
|
152
151
|
storage,
|
|
@@ -183,7 +182,7 @@ export abstract class SetupClient {
|
|
|
183
182
|
/**
|
|
184
183
|
* @publicBody
|
|
185
184
|
*/
|
|
186
|
-
static async
|
|
185
|
+
static async createWalletClient(
|
|
187
186
|
args: SetupWalletClientArgs
|
|
188
187
|
): Promise<SetupWalletClient> {
|
|
189
188
|
const wo = await SetupClient.createWallet(args)
|
|
@@ -333,54 +332,147 @@ export type KeyPairAddress = {
|
|
|
333
332
|
}
|
|
334
333
|
|
|
335
334
|
/**
|
|
335
|
+
* `SetupEnv` provides a starting point for managing secrets that
|
|
336
|
+
* must not appear in source code.
|
|
336
337
|
*
|
|
338
|
+
* The `makeEnv` and `getEnv` functions of the `Setup` and `SetupClient` classes
|
|
339
|
+
* provide an easy way to create and import these secrets and related properties.
|
|
337
340
|
*/
|
|
338
341
|
export interface SetupEnv {
|
|
342
|
+
/**
|
|
343
|
+
* The chan being accessed: 'main' for mainnet, 'test' for 'testnet'.
|
|
344
|
+
*/
|
|
339
345
|
chain: sdk.Chain
|
|
346
|
+
/**
|
|
347
|
+
* The user's primary identity key (public key).
|
|
348
|
+
*/
|
|
340
349
|
identityKey: string
|
|
350
|
+
/**
|
|
351
|
+
* A secondary identity key (public key), used to test exchanges with other users.
|
|
352
|
+
*/
|
|
341
353
|
identityKey2: string
|
|
354
|
+
/**
|
|
355
|
+
* A vaild TAAL API key for use by `Services`
|
|
356
|
+
*/
|
|
342
357
|
taalApiKey: string
|
|
358
|
+
/**
|
|
359
|
+
* A map of public keys (identity keys, hex strings) to private keys (hex strings).
|
|
360
|
+
*/
|
|
343
361
|
devKeys: Record<string, string>
|
|
362
|
+
/**
|
|
363
|
+
* A MySQL connection string including user and password properties.
|
|
364
|
+
* Must be valid to make use of MySQL `Setup` class support.
|
|
365
|
+
*/
|
|
344
366
|
mySQLConnection: string
|
|
345
367
|
}
|
|
346
368
|
|
|
347
369
|
/**
|
|
348
|
-
* Arguments used to construct a `
|
|
370
|
+
* Arguments used by `createWallet` to construct a `SetupWallet`.
|
|
371
|
+
*
|
|
372
|
+
* Extension `SetupWalletClientArgs` used by `createWalletClient` to construct a `SetupWalletClient`.
|
|
373
|
+
*
|
|
374
|
+
* Extension `SetupWalletKnexArgs` used by `createWalletKnex` to construct a `SetupWalletKnex`.
|
|
349
375
|
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
* @param rootKeyHex Optional. The non-privileged private key used to initialize the `KeyDeriver` and determine the `identityKey`.
|
|
354
|
-
* Defaults to `env.devKeys[env.identityKey]
|
|
355
|
-
* @param privKeyHex Optional. The privileged private key used to initialize the `PrivilegedKeyManager`.
|
|
356
|
-
* Defaults to undefined.
|
|
357
|
-
* @param active. Optional. Active wallet storage. Can be added later.
|
|
358
|
-
* @param backups. Optional. One or more storage providers managed as backup destinations. Can be added later.
|
|
376
|
+
* Extension `SetupWalletMySQLArgs` used by `createWalletMySQL` to construct a `SetupWalletKnex`.
|
|
377
|
+
*
|
|
378
|
+
* Extension `SetupWalletSQLiteArgs` used by `createWalletSQLite` to construct a `SetupWalletKnex`.
|
|
359
379
|
*/
|
|
360
380
|
export interface SetupWalletArgs {
|
|
381
|
+
/**
|
|
382
|
+
* Configuration "secrets" typically obtained by `Setup.makeEnv` and `Setup.getEnv` functions.
|
|
383
|
+
*/
|
|
361
384
|
env: SetupEnv
|
|
362
|
-
|
|
385
|
+
/**
|
|
386
|
+
* Optional. The non-privileged private key used to initialize the `KeyDeriver` and determine the `identityKey`.
|
|
387
|
+
* Defaults to `env.devKeys[env.identityKey]
|
|
388
|
+
*/
|
|
363
389
|
rootKeyHex?: string
|
|
390
|
+
/**
|
|
391
|
+
* Optional. The privileged private key used to initialize the `PrivilegedKeyManager`.
|
|
392
|
+
* Defaults to undefined.
|
|
393
|
+
*/
|
|
364
394
|
privKeyHex?: string
|
|
395
|
+
/**
|
|
396
|
+
* Optional. Active wallet storage. Can be added later.
|
|
397
|
+
*/
|
|
365
398
|
active?: sdk.WalletStorageProvider
|
|
399
|
+
/**
|
|
400
|
+
* Optional. One or more storage providers managed as backup destinations. Can be added later.
|
|
401
|
+
*/
|
|
366
402
|
backups?: sdk.WalletStorageProvider[]
|
|
367
403
|
}
|
|
368
404
|
|
|
405
|
+
/**
|
|
406
|
+
* When creating a BRC-100 compatible `Wallet`, many components come into play.
|
|
407
|
+
*
|
|
408
|
+
* All of the `createWallet` functions in the `Setup` and `SetupClient` classes return
|
|
409
|
+
* an object with direct access to each component to facilitate experimentation, testing
|
|
410
|
+
* and customization.
|
|
411
|
+
*/
|
|
369
412
|
export interface SetupWallet {
|
|
413
|
+
/**
|
|
414
|
+
* The rootKey of the `KeyDeriver`. The private key from which other keys are derived.
|
|
415
|
+
*/
|
|
370
416
|
rootKey: PrivateKey
|
|
417
|
+
/**
|
|
418
|
+
* The pubilc key associated with the `rootKey` which also serves as the wallet's identity.
|
|
419
|
+
*/
|
|
371
420
|
identityKey: string
|
|
421
|
+
/**
|
|
422
|
+
* The `KeyDeriver` component used by the wallet for key derivation and cryptographic functions.
|
|
423
|
+
*/
|
|
372
424
|
keyDeriver: KeyDeriver
|
|
425
|
+
/**
|
|
426
|
+
* The chain ('main' or 'test') which the wallet accesses.
|
|
427
|
+
*/
|
|
373
428
|
chain: sdk.Chain
|
|
429
|
+
/**
|
|
430
|
+
* The `WalletStorageManager` that manages all the configured storage providers (active and backups)
|
|
431
|
+
* accessed by the wallet.
|
|
432
|
+
*/
|
|
374
433
|
storage: WalletStorageManager
|
|
434
|
+
/**
|
|
435
|
+
* The network `Services` component which provides the wallet with access to external services hosted
|
|
436
|
+
* on the public network.
|
|
437
|
+
*/
|
|
375
438
|
services: Services
|
|
439
|
+
/**
|
|
440
|
+
* The background task `Monitor` component available to the wallet to offload tasks
|
|
441
|
+
* that speed up wallet operations and maintain data integrity.
|
|
442
|
+
*/
|
|
376
443
|
monitor: Monitor
|
|
444
|
+
/**
|
|
445
|
+
* The actual BRC-100 `Wallet` to which all the other properties and components contribute.
|
|
446
|
+
*
|
|
447
|
+
* Note that internally, the wallet is itself linked to all these properties and components.
|
|
448
|
+
* They are included in this interface to facilitate access after wallet construction for
|
|
449
|
+
* experimentation, testing and customization. Any changes made to the configuration of these
|
|
450
|
+
* components after construction may disrupt the normal operation of the wallet.
|
|
451
|
+
*/
|
|
377
452
|
wallet: Wallet
|
|
378
453
|
}
|
|
379
454
|
|
|
455
|
+
/**
|
|
456
|
+
* Extension `SetupWalletClientArgs` of `SetupWalletArgs` is used by `createWalletClient`
|
|
457
|
+
* to construct a `SetupWalletClient`.
|
|
458
|
+
*/
|
|
380
459
|
export interface SetupWalletClientArgs extends SetupWalletArgs {
|
|
460
|
+
/**
|
|
461
|
+
* The endpoint URL of a service hosting the `StorageServer` JSON-RPC service to
|
|
462
|
+
* which a `StorageClient` instance should connect to function as
|
|
463
|
+
* the active storage provider of the newly created wallet.
|
|
464
|
+
*/
|
|
381
465
|
endpointUrl?: string
|
|
382
466
|
}
|
|
383
467
|
|
|
468
|
+
/**
|
|
469
|
+
* Extension `SetupWalletClient` of `SetupWallet` is returned by `createWalletClient`
|
|
470
|
+
*/
|
|
384
471
|
export interface SetupWalletClient extends SetupWallet {
|
|
472
|
+
/**
|
|
473
|
+
* The endpoint URL of the service hosting the `StorageServer` JSON-RPC service to
|
|
474
|
+
* which a `StorageClient` instance is connected to function as
|
|
475
|
+
* the active storage provider of the wallet.
|
|
476
|
+
*/
|
|
385
477
|
endpointUrl: string
|
|
386
478
|
}
|
package/src/Wallet.ts
CHANGED
|
@@ -119,6 +119,11 @@ export class Wallet implements WalletInterface, ProtoWallet {
|
|
|
119
119
|
|
|
120
120
|
pendingSignActions: Record<string, PendingSignAction>
|
|
121
121
|
|
|
122
|
+
/**
|
|
123
|
+
* For repeatability testing, set to an array of random numbers from [0..1).
|
|
124
|
+
*/
|
|
125
|
+
randomVals?: number[] = undefined
|
|
126
|
+
|
|
122
127
|
constructor(
|
|
123
128
|
argsOrSigner: WalletArgs | WalletSigner,
|
|
124
129
|
services?: sdk.WalletServices,
|
|
@@ -632,6 +637,10 @@ export class Wallet implements WalletInterface, ProtoWallet {
|
|
|
632
637
|
args,
|
|
633
638
|
sdk.validateCreateActionArgs
|
|
634
639
|
)
|
|
640
|
+
if (this.randomVals && this.randomVals.length > 1) {
|
|
641
|
+
vargs.randomVals = [...this.randomVals]
|
|
642
|
+
}
|
|
643
|
+
|
|
635
644
|
const r = await createAction(this, auth, vargs)
|
|
636
645
|
|
|
637
646
|
if (r.signableTransaction) {
|
package/src/sdk/WalletError.ts
CHANGED
|
@@ -56,7 +56,6 @@ export class WalletError extends Error implements WalletErrorObject {
|
|
|
56
56
|
/**
|
|
57
57
|
* Recovers all public fields from WalletError derived error classes and relevant Error derived errors.
|
|
58
58
|
*
|
|
59
|
-
* Critical client data fields are preserved across HTTP DojoExpress / DojoExpressClient encoding.
|
|
60
59
|
*/
|
|
61
60
|
static fromUnknown(err: unknown): WalletError {
|
|
62
61
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -403,6 +403,7 @@ export interface ValidCreateActionArgs extends ValidProcessActionArgs {
|
|
|
403
403
|
options: ValidCreateActionOptions
|
|
404
404
|
// true if transaction creation completion will require a `signAction` call.
|
|
405
405
|
isSignAction: boolean
|
|
406
|
+
randomVals?: number[]
|
|
406
407
|
}
|
|
407
408
|
|
|
408
409
|
export interface ValidSignActionArgs extends ValidProcessActionArgs {
|
|
@@ -428,7 +429,8 @@ export function validateCreateActionArgs(
|
|
|
428
429
|
isDelayed: false,
|
|
429
430
|
isNoSend: false,
|
|
430
431
|
isNewTx: false,
|
|
431
|
-
isSignAction: false
|
|
432
|
+
isSignAction: false,
|
|
433
|
+
randomVals: undefined
|
|
432
434
|
}
|
|
433
435
|
vargs.isSendWith = vargs.options.sendWith.length > 0
|
|
434
436
|
vargs.isNewTx = vargs.inputs.length > 0 || vargs.outputs.length > 0
|
|
@@ -217,7 +217,7 @@ export abstract class StorageProvider
|
|
|
217
217
|
|
|
218
218
|
/**
|
|
219
219
|
* Given an array of transaction txids with current ProvenTxReq ready-to-share status,
|
|
220
|
-
* lookup their
|
|
220
|
+
* lookup their ProvenTxReqApi req records.
|
|
221
221
|
* For the txids with reqs and status still ready to send construct a single merged beef.
|
|
222
222
|
*
|
|
223
223
|
* @param txids
|
|
@@ -408,9 +408,6 @@ export abstract class StorageProvider
|
|
|
408
408
|
*
|
|
409
409
|
* For 'status' of 'failed', attempts to make outputs previously allocated as inputs to this transaction usable again.
|
|
410
410
|
*
|
|
411
|
-
* @throws ERR_DOJO_COMPLETED_TX if current status is 'completed' and new status is not 'completed.
|
|
412
|
-
* @throws ERR_DOJO_PROVEN_TX if transaction has proof or provenTxId and new status is not 'completed'.
|
|
413
|
-
*
|
|
414
411
|
* @param status
|
|
415
412
|
* @param transactionId
|
|
416
413
|
* @param userId
|
|
@@ -7,7 +7,9 @@ import {
|
|
|
7
7
|
PrivateKey,
|
|
8
8
|
PubKeyHex,
|
|
9
9
|
PublicKey,
|
|
10
|
-
|
|
10
|
+
Random,
|
|
11
|
+
Script,
|
|
12
|
+
Utils
|
|
11
13
|
} from '@bsv/sdk'
|
|
12
14
|
import {
|
|
13
15
|
asArray,
|
|
@@ -711,7 +713,7 @@ async function validateRequiredInputs(
|
|
|
711
713
|
}
|
|
712
714
|
|
|
713
715
|
async function validateNoSendChange(
|
|
714
|
-
|
|
716
|
+
storage: StorageProvider,
|
|
715
717
|
userId: number,
|
|
716
718
|
vargs: sdk.ValidCreateActionArgs,
|
|
717
719
|
changeBasket: table.OutputBasket
|
|
@@ -725,7 +727,7 @@ async function validateNoSendChange(
|
|
|
725
727
|
if (noSendChange && noSendChange.length > 0) {
|
|
726
728
|
for (const op of noSendChange) {
|
|
727
729
|
const output = verifyOneOrNone(
|
|
728
|
-
await
|
|
730
|
+
await storage.findOutputs({
|
|
729
731
|
partial: { userId, txid: op.txid, vout: op.vout }
|
|
730
732
|
})
|
|
731
733
|
)
|
|
@@ -754,7 +756,7 @@ async function validateNoSendChange(
|
|
|
754
756
|
}
|
|
755
757
|
|
|
756
758
|
async function fundNewTransactionSdk(
|
|
757
|
-
|
|
759
|
+
storage: StorageProvider,
|
|
758
760
|
userId: number,
|
|
759
761
|
vargs: sdk.ValidCreateActionArgs,
|
|
760
762
|
ctx: CreateTransactionSdkContext
|
|
@@ -781,7 +783,8 @@ async function fundNewTransactionSdk(
|
|
|
781
783
|
changeLockingScriptLength: 25,
|
|
782
784
|
changeUnlockingScriptLength: 107,
|
|
783
785
|
targetNetCount:
|
|
784
|
-
ctx.changeBasket.numberOfDesiredUTXOs - ctx.availableChangeCount
|
|
786
|
+
ctx.changeBasket.numberOfDesiredUTXOs - ctx.availableChangeCount,
|
|
787
|
+
randomVals: vargs.randomVals
|
|
785
788
|
}
|
|
786
789
|
|
|
787
790
|
const noSendChange = [...ctx.noSendChangeIn]
|
|
@@ -796,7 +799,7 @@ async function fundNewTransactionSdk(
|
|
|
796
799
|
const o = noSendChange.pop()!
|
|
797
800
|
outputs[o.outputId!] = o
|
|
798
801
|
// allocate the output in storage, noSendChange is by definition spendable false and part of noSpend transaction batch.
|
|
799
|
-
await
|
|
802
|
+
await storage.updateOutput(o.outputId!, {
|
|
800
803
|
spendable: false,
|
|
801
804
|
spentBy: ctx.transactionId
|
|
802
805
|
})
|
|
@@ -810,7 +813,7 @@ async function fundNewTransactionSdk(
|
|
|
810
813
|
}
|
|
811
814
|
|
|
812
815
|
const basketId = ctx.changeBasket.basketId!
|
|
813
|
-
const o = await
|
|
816
|
+
const o = await storage.allocateChangeInput(
|
|
814
817
|
userId,
|
|
815
818
|
basketId,
|
|
816
819
|
targetSatoshis,
|
|
@@ -833,7 +836,7 @@ async function fundNewTransactionSdk(
|
|
|
833
836
|
noSendChange.push(nsco)
|
|
834
837
|
return
|
|
835
838
|
}
|
|
836
|
-
await
|
|
839
|
+
await storage.updateOutput(outputId, {
|
|
837
840
|
spendable: true,
|
|
838
841
|
spentBy: undefined
|
|
839
842
|
})
|
|
@@ -845,8 +848,41 @@ async function fundNewTransactionSdk(
|
|
|
845
848
|
releaseChangeInput
|
|
846
849
|
)
|
|
847
850
|
|
|
851
|
+
const nextRandomVal = (): number => {
|
|
852
|
+
let val = 0
|
|
853
|
+
if (!vargs.randomVals || vargs.randomVals.length === 0) {
|
|
854
|
+
val = Math.random()
|
|
855
|
+
} else {
|
|
856
|
+
val = vargs.randomVals.shift() || 0
|
|
857
|
+
vargs.randomVals.push(val)
|
|
858
|
+
}
|
|
859
|
+
return val
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* @returns a random integer betweenn min and max, inclussive.
|
|
864
|
+
*/
|
|
865
|
+
const rand = (min: number, max: number): number => {
|
|
866
|
+
if (max < min)
|
|
867
|
+
throw new sdk.WERR_INVALID_PARAMETER(
|
|
868
|
+
'max',
|
|
869
|
+
`less than min (${min}). max is (${max})`
|
|
870
|
+
)
|
|
871
|
+
return Math.floor(nextRandomVal() * (max - min + 1) + min)
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const randomDerivation = (count: number): string => {
|
|
875
|
+
let val: number[] = []
|
|
876
|
+
if (!vargs.randomVals || vargs.randomVals.length === 0) {
|
|
877
|
+
val = Random(count)
|
|
878
|
+
} else {
|
|
879
|
+
for (let i = 0; i < count; i++) val.push(rand(0, 255))
|
|
880
|
+
}
|
|
881
|
+
return Utils.toBase64(val)
|
|
882
|
+
}
|
|
883
|
+
|
|
848
884
|
// Generate a derivation prefix for the payment
|
|
849
|
-
const derivationPrefix =
|
|
885
|
+
const derivationPrefix = randomDerivation(16)
|
|
850
886
|
|
|
851
887
|
const r: {
|
|
852
888
|
allocatedChange: table.Output[]
|
|
@@ -870,7 +906,7 @@ async function fundNewTransactionSdk(
|
|
|
870
906
|
change: true,
|
|
871
907
|
type: 'P2PKH',
|
|
872
908
|
derivationPrefix,
|
|
873
|
-
derivationSuffix:
|
|
909
|
+
derivationSuffix: randomDerivation(16),
|
|
874
910
|
providedBy: 'storage',
|
|
875
911
|
purpose: 'change',
|
|
876
912
|
customInstructions: undefined,
|
|
@@ -910,7 +946,7 @@ function trimInputBeef(
|
|
|
910
946
|
}
|
|
911
947
|
|
|
912
948
|
async function mergeAllocatedChangeBeefs(
|
|
913
|
-
|
|
949
|
+
storage: StorageProvider,
|
|
914
950
|
userId: number,
|
|
915
951
|
vargs: sdk.ValidCreateActionArgs,
|
|
916
952
|
allocatedChange: table.Output[],
|
|
@@ -931,7 +967,7 @@ async function mergeAllocatedChangeBeefs(
|
|
|
931
967
|
!beef.findTxid(o.txid!) &&
|
|
932
968
|
!vargs.options.knownTxids.find(txid => txid === o.txid)
|
|
933
969
|
) {
|
|
934
|
-
await
|
|
970
|
+
await storage.getBeefForTransaction(o.txid!, options)
|
|
935
971
|
}
|
|
936
972
|
}
|
|
937
973
|
return trimInputBeef(beef, vargs)
|
|
@@ -331,7 +331,7 @@ export async function generateChangeSdk(
|
|
|
331
331
|
return r
|
|
332
332
|
} catch (eu: unknown) {
|
|
333
333
|
const e = sdk.WalletError.fromUnknown(eu)
|
|
334
|
-
if (e.code === '
|
|
334
|
+
if (e.code === 'WERR_INSUFFICIENT_FUNDS') throw eu
|
|
335
335
|
|
|
336
336
|
// Capture the params in cloud run log which has a 100k text length limit per line.
|
|
337
337
|
// logGenerateChangeSdkParams(params, eu)
|
|
@@ -21,12 +21,12 @@ export interface ProvenTxReq extends ProvenTxReqDynamics {
|
|
|
21
21
|
batch?: string
|
|
22
22
|
/**
|
|
23
23
|
* JSON string of processing history.
|
|
24
|
-
* Parses to `
|
|
24
|
+
* Parses to `ProvenTxReqHistoryApi`.
|
|
25
25
|
*/
|
|
26
26
|
history: string
|
|
27
27
|
/**
|
|
28
28
|
* JSON string of data to drive notifications when this request completes.
|
|
29
|
-
* Parses to `
|
|
29
|
+
* Parses to `ProvenTxReqNotifyApi`.
|
|
30
30
|
*/
|
|
31
31
|
notify: string
|
|
32
32
|
rawTx: number[]
|
|
@@ -54,12 +54,12 @@ export interface ProvenTxReqDynamics extends sdk.EntityTimeStamp {
|
|
|
54
54
|
batch?: string
|
|
55
55
|
/**
|
|
56
56
|
* JSON string of processing history.
|
|
57
|
-
* Parses to `
|
|
57
|
+
* Parses to `ProvenTxReqHistoryApi`.
|
|
58
58
|
*/
|
|
59
59
|
history: string
|
|
60
60
|
/**
|
|
61
61
|
* JSON string of data to drive notifications when this request completes.
|
|
62
|
-
* Parses to `
|
|
62
|
+
* Parses to `ProvenTxReqNotifyApi`.
|
|
63
63
|
*/
|
|
64
64
|
notify: string
|
|
65
65
|
}
|
|
@@ -11,7 +11,7 @@ describe('examples README tests', () => {
|
|
|
11
11
|
it('1 internalize wallet payment', async () => {
|
|
12
12
|
const env = Setup.getEnv('test')
|
|
13
13
|
|
|
14
|
-
const { wallet } = await Setup.
|
|
14
|
+
const { wallet } = await Setup.createWalletSQLite({
|
|
15
15
|
env,
|
|
16
16
|
filePath: './myTestWallet.sqlite',
|
|
17
17
|
databaseName: 'myTestWallet'
|