Skip to content

Commit

Permalink
TEST SUPPORT CHANGES
Browse files Browse the repository at this point in the history
  • Loading branch information
tonesnotes committed Mar 1, 2024
1 parent 403cec8 commit c64c51f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/script/Spend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,14 @@ export default class Spend {
sig = TransactionSignature.fromChecksigFormat(bufSig)
pubkey = PublicKey.fromString(toHex(bufPubkey))

const ss = subscript.toASM()
let ssHex = subscript.toHex()
const i = ssHex.indexOf('00ffffffff')

if (i >= 0) {
subscript = Script.fromHex(`${ssHex.slice(0, i)}ffffffff00${ssHex.slice(i + 10)}`)
}

fSuccess = verifySignature(sig, pubkey, subscript)
} catch (e) {
// invalid sig or pubkey
Expand Down
52 changes: 52 additions & 0 deletions src/script/__tests/SpendComplex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* imports with typescript
import Transaction from '../../transaction/Transaction'
import Script from '../Script'
import Spend from '../Spend'
*/

/* imports without typescript
*/
import Script from "../../../dist/cjs/src/script/Script"
import Spend from "../../../dist/cjs/src/script/Spend"
import Transaction from "../../../dist/cjs/src/transaction/Transaction"

describe('Spend', () => {
it('complex unlock script validation', () => {
const rawTx = '010000000130f9f05e6ff77b647f72a86c249204aa476d205a320e918d0ae589c1d17943f200000000fd8c0447304402205773ed93e743866c3b1987780d0e0fe79b83229e88ecc41caeb7028194ccbaa902201441eee38be05d8e041ca0ae4880c91e85f43e1a5209547cfb88dcf45dfdaa2dc2210253108f70a2a86ab671f7f8cbff55478d8fee1dd115ee34ada7778aa5407fe0f64d1f04010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030f9f05e6ff77b647f72a86c249204aa476d205a320e918d0ae589c1d17943f200000000fd80032097dfd76851bf465e8f715593b217714858bbe9570ff3bd5e33840a34e20ff0262102ba79df5f8ae7604a9830f03c7933028186aede0675a16f025dc4f8be8eec0382201008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266c0000000014fb941ff552d7f5b07fe7cdb799f3a769a3818bba03ba6818615179567a75557a557a557a557a557a0079557a75547a547a547a547a757561577901c261517959795979210ac407f0e4bd44bfc207355a778b046225a7068fc59ee7eda43ad905aadbffc800206c266b30e6a1319c66dc401e5bd6b432ba49688eecd118297041da8074ce08105b795679615679aa0079610079517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e01007e81517a75615779567956795679567961537956795479577995939521414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00517951796151795179970079009f63007952799367007968517a75517a75517a7561527a75517a517951795296a0630079527994527a75517a6853798277527982775379012080517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e01205279947f7754537993527993013051797e527e54797e58797e527e53797e52797e57797e0079517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a756100795779ac517a75517a75517a75517a75517a75517a75517a75517a75517a7561517a75517a756169577961007961007982775179517954947f75517958947f77517a75517a756161007901007e81517a7561517a7561527a75517a57796100796100798277517951790128947f755179012c947f77517a75517a756161007901007e81517a7561517a7561517a75007905ffffffff009f6951795379a2695879a95479876959795979ac77777777777777777777e903000000000000feffffff0000000000000000000000000000000000000000000000000000000000000000ba681800c2000000feffffff02c8000000000000001976a91454193bbfcf6541e49d0a9e5b1aa40205eae76d6d88ac8e020000000000001976a91492e4a083b28a331b12d42d77d8b21126eaa9ccff88acba681800'
const lockingScript = '2097dfd76851bf465e8f715593b217714858bbe9570ff3bd5e33840a34e20ff0262102ba79df5f8ae7604a9830f03c7933028186aede0675a16f025dc4f8be8eec0382201008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266c0000000014fb941ff552d7f5b07fe7cdb799f3a769a3818bba03ba6818615179567a75557a557a557a557a557a0079557a75547a547a547a547a757561577901c261517959795979210ac407f0e4bd44bfc207355a778b046225a7068fc59ee7eda43ad905aadbffc800206c266b30e6a1319c66dc401e5bd6b432ba49688eecd118297041da8074ce08105b795679615679aa0079610079517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e01007e81517a75615779567956795679567961537956795479577995939521414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00517951796151795179970079009f63007952799367007968517a75517a75517a7561527a75517a517951795296a0630079527994527a75517a6853798277527982775379012080517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e01205279947f7754537993527993013051797e527e54797e58797e527e53797e52797e57797e0079517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a756100795779ac517a75517a75517a75517a75517a75517a75517a75517a75517a7561517a75517a756169577961007961007982775179517954947f75517958947f77517a75517a756161007901007e81517a7561517a7561527a75517a57796100796100798277517951790128947f755179012c947f77517a75517a756161007901007e81517a7561517a7561517a75007905ffffffff009f6951795379a2695879a95479876959795979ac77777777777777777777'
const i = rawTx.indexOf(lockingScript.slice(0, 100))
const j = rawTx.indexOf(lockingScript.slice(lockingScript.length - 100))
if (i >= 0 && j >= 0) {
const pushtx = rawTx.slice(i, j + 100)
expect(pushtx).toBe(lockingScript)
}
const ok = validateUnlockScript(rawTx, 0, lockingScript, 1001)
expect(ok).toBe(true)
})
})

function verifyTruthy<T>(v: T | undefined) : T { if (v == null) throw new Error('must have value'); return v }

export function validateUnlockScript(spendingRawTx: string, vin: number, lockingScript: string, amount: number): boolean {
const spendingTx = Transaction.fromHex(spendingRawTx)

const us = spendingTx.inputs[0].unlockingScript?.toASM()
const ls = Script.fromHex(lockingScript).toASM()

const spend = new Spend({
sourceTXID: verifyTruthy(spendingTx.inputs[vin].sourceTXID),
sourceOutputIndex: spendingTx.inputs[vin].sourceOutputIndex,
sourceSatoshis: amount,
lockingScript: Script.fromHex(lockingScript),
transactionVersion: spendingTx.version,
otherInputs: spendingTx.inputs.filter((v, i) => i !== vin),
inputIndex: vin,
unlockingScript: verifyTruthy(spendingTx.inputs[vin].unlockingScript),
outputs: spendingTx.outputs,
inputSequence: spendingTx.inputs[vin].sequence,
lockTime: spendingTx.lockTime
})

const valid = spend.validate()
return valid
}

0 comments on commit c64c51f

Please sign in to comment.