Skip to content

Commit 631e7ea

Browse files
committed
add repeatable txids from createAction for test support
1 parent 29c58fd commit 631e7ea

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

src/Wallet.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export class Wallet implements WalletInterface, ProtoWallet {
119119

120120
pendingSignActions: Record<string, PendingSignAction>
121121

122+
/**
123+
* For repeatability testing, set to an array of random numbers from [0..1).
124+
*/
125+
randomVals?: number[] = undefined
126+
122127
constructor(
123128
argsOrSigner: WalletArgs | WalletSigner,
124129
services?: sdk.WalletServices,
@@ -632,6 +637,10 @@ export class Wallet implements WalletInterface, ProtoWallet {
632637
args,
633638
sdk.validateCreateActionArgs
634639
)
640+
if (this.randomVals && this.randomVals.length > 1) {
641+
vargs.randomVals = [...this.randomVals]
642+
}
643+
635644
const r = await createAction(this, auth, vargs)
636645

637646
if (r.signableTransaction) {

src/sdk/validationHelpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export interface ValidCreateActionArgs extends ValidProcessActionArgs {
403403
options: ValidCreateActionOptions
404404
// true if transaction creation completion will require a `signAction` call.
405405
isSignAction: boolean
406+
randomVals?: number[]
406407
}
407408

408409
export interface ValidSignActionArgs extends ValidProcessActionArgs {
@@ -428,7 +429,8 @@ export function validateCreateActionArgs(
428429
isDelayed: false,
429430
isNoSend: false,
430431
isNewTx: false,
431-
isSignAction: false
432+
isSignAction: false,
433+
randomVals: undefined
432434
}
433435
vargs.isSendWith = vargs.options.sendWith.length > 0
434436
vargs.isNewTx = vargs.inputs.length > 0 || vargs.outputs.length > 0

src/storage/methods/createAction.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
PrivateKey,
88
PubKeyHex,
99
PublicKey,
10-
Script
10+
Random,
11+
Script,
12+
Utils
1113
} from '@bsv/sdk'
1214
import {
1315
asArray,
@@ -781,7 +783,8 @@ async function fundNewTransactionSdk(
781783
changeLockingScriptLength: 25,
782784
changeUnlockingScriptLength: 107,
783785
targetNetCount:
784-
ctx.changeBasket.numberOfDesiredUTXOs - ctx.availableChangeCount
786+
ctx.changeBasket.numberOfDesiredUTXOs - ctx.availableChangeCount,
787+
randomVals: vargs.randomVals
785788
}
786789

787790
const noSendChange = [...ctx.noSendChangeIn]
@@ -845,8 +848,42 @@ async function fundNewTransactionSdk(
845848
releaseChangeInput
846849
)
847850

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++)
880+
val.push(rand(0, 255))
881+
}
882+
return Utils.toBase64(val)
883+
}
884+
848885
// Generate a derivation prefix for the payment
849-
const derivationPrefix = randomBytesBase64(16)
886+
const derivationPrefix = randomDerivation(16)
850887

851888
const r: {
852889
allocatedChange: table.Output[]
@@ -870,7 +907,7 @@ async function fundNewTransactionSdk(
870907
change: true,
871908
type: 'P2PKH',
872909
derivationPrefix,
873-
derivationSuffix: randomBytesBase64(16),
910+
derivationSuffix: randomDerivation(16),
874911
providedBy: 'storage',
875912
purpose: 'change',
876913
customInstructions: undefined,

test/wallet/action/createAction.test.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ describe('createAction test', () => {
1616
const env = _tu.getEnv('test')
1717
const testName = () => expect.getState().currentTestName || 'test'
1818

19-
const ctxs: TestWalletNoSetup[] = []
19+
let ctxs: TestWalletNoSetup[]
2020

21-
beforeAll(async () => {
21+
beforeEach(async () => {
22+
ctxs = []
2223
if (env.runMySQL)
2324
ctxs.push(await _tu.createLegacyWalletMySQLCopy('createActionTests'))
24-
ctxs.push(await _tu.createLegacyWalletSQLiteCopy('createActionTests'))
25+
ctxs.push(await _tu.createLegacyWalletSQLiteCopy(`${testName()}`))
2526
_tu.mockPostServicesAsSuccess(ctxs)
2627
})
2728

28-
afterAll(async () => {
29+
afterEach(async () => {
2930
for (const ctx of ctxs) {
3031
await ctx.storage.destroy()
3132
}
3233
})
33-
test('1_invalid_params', async () => {
34+
35+
test('0_invalid_params', async () => {
3436
for (const { wallet } of ctxs) {
3537
{
3638
const log = `\n${testName()}\n`
@@ -64,6 +66,32 @@ describe('createAction test', () => {
6466
}
6567
})
6668

69+
test('1_repeatable txid', async () => {
70+
for (const { wallet } of ctxs) {
71+
wallet.randomVals = [0.1, 0.2, 0.3, 0.7, 0.8, 0.9]
72+
const root = '02135476'
73+
const kp = _tu.getKeyPair(root.repeat(8))
74+
const createArgs: CreateActionArgs = {
75+
description: `repeatable`,
76+
outputs: [
77+
{
78+
satoshis: 45,
79+
lockingScript: _tu.getLockP2PKH(kp.address).toHex(),
80+
outputDescription: 'pay echo'
81+
}
82+
],
83+
options: {
84+
randomizeOutputs: false,
85+
signAndProcess: true,
86+
noSend: true
87+
}
88+
}
89+
90+
const cr = await wallet.createAction(createArgs)
91+
expect(cr.txid === '4f428a93c43c2d120204ecdc06f7916be8a5f4542cc8839a0fd79bd1b44582f3')
92+
}
93+
})
94+
6795
test('2_signableTransaction', async () => {
6896
for (const { wallet } of ctxs) {
6997
const root = '02135476'

0 commit comments

Comments
 (0)