@bsv/sdk 2.0.8 → 2.0.10
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/dist/cjs/package.json +1 -1
- package/dist/cjs/src/auth/certificates/Certificate.js +1 -1
- package/dist/cjs/src/auth/certificates/Certificate.js.map +1 -1
- package/dist/cjs/src/primitives/BigNumber.js +4 -5
- package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
- package/dist/cjs/src/primitives/index.js.map +1 -1
- package/dist/cjs/src/script/OP.js +22 -16
- package/dist/cjs/src/script/OP.js.map +1 -1
- package/dist/cjs/src/script/Spend.js +21 -78
- package/dist/cjs/src/script/Spend.js.map +1 -1
- package/dist/cjs/src/transaction/Transaction.js +45 -0
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/auth/certificates/Certificate.js +1 -1
- package/dist/esm/src/auth/certificates/Certificate.js.map +1 -1
- package/dist/esm/src/primitives/BigNumber.js +4 -5
- package/dist/esm/src/primitives/BigNumber.js.map +1 -1
- package/dist/esm/src/primitives/index.js.map +1 -1
- package/dist/esm/src/script/OP.js +22 -16
- package/dist/esm/src/script/OP.js.map +1 -1
- package/dist/esm/src/script/Spend.js +21 -78
- package/dist/esm/src/script/Spend.js.map +1 -1
- package/dist/esm/src/transaction/Transaction.js +45 -0
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/primitives/BigNumber.d.ts.map +1 -1
- package/dist/types/src/primitives/index.d.ts +1 -1
- package/dist/types/src/primitives/index.d.ts.map +1 -1
- package/dist/types/src/script/OP.d.ts +9 -7
- package/dist/types/src/script/OP.d.ts.map +1 -1
- package/dist/types/src/script/Spend.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts +9 -0
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +3 -3
- package/dist/umd/bundle.js.map +1 -1
- package/docs/reference/script.md +76 -0
- package/docs/reference/transaction.md +87 -7
- package/docs/reference/wallet.md +285 -1094
- package/package.json +1 -1
- package/src/auth/certificates/Certificate.ts +1 -1
- package/src/primitives/BigNumber.ts +4 -5
- package/src/primitives/__tests/BigNumber.constructor.test.ts +3 -3
- package/src/primitives/index.ts +1 -1
- package/src/script/OP.ts +21 -16
- package/src/script/Spend.ts +21 -28
- package/src/script/__tests/Chronicle.test.ts +39 -103
- package/src/script/__tests/ChronicleOpcodes.test.ts +548 -0
- package/src/transaction/Transaction.ts +46 -0
- package/src/transaction/__tests/Transaction.test.ts +170 -0
|
@@ -2054,4 +2054,174 @@ describe('Transaction', () => {
|
|
|
2054
2054
|
await expect(tx.verify('scripts only', new SatoshisPerKilobyte(1), 35)).resolves.toBe(true)
|
|
2055
2055
|
})
|
|
2056
2056
|
})
|
|
2057
|
+
|
|
2058
|
+
describe('#preimage', () => {
|
|
2059
|
+
let sourceTx: Transaction
|
|
2060
|
+
let spendTx: Transaction
|
|
2061
|
+
let privateKey: PrivateKey
|
|
2062
|
+
let publicKey: any
|
|
2063
|
+
let publicKeyHash: number[]
|
|
2064
|
+
let p2pkh: P2PKH
|
|
2065
|
+
|
|
2066
|
+
beforeEach(() => {
|
|
2067
|
+
privateKey = new PrivateKey(1)
|
|
2068
|
+
publicKey = new Curve().g.mul(privateKey)
|
|
2069
|
+
publicKeyHash = hash160(publicKey.encode(true))
|
|
2070
|
+
p2pkh = new P2PKH()
|
|
2071
|
+
sourceTx = new Transaction(
|
|
2072
|
+
1,
|
|
2073
|
+
[],
|
|
2074
|
+
[
|
|
2075
|
+
{
|
|
2076
|
+
lockingScript: p2pkh.lock(publicKeyHash),
|
|
2077
|
+
satoshis: 4000
|
|
2078
|
+
}
|
|
2079
|
+
],
|
|
2080
|
+
0
|
|
2081
|
+
)
|
|
2082
|
+
spendTx = new Transaction(
|
|
2083
|
+
1,
|
|
2084
|
+
[
|
|
2085
|
+
{
|
|
2086
|
+
sourceTransaction: sourceTx,
|
|
2087
|
+
sourceOutputIndex: 0,
|
|
2088
|
+
unlockingScript: new UnlockingScript(),
|
|
2089
|
+
sequence: 0xffffffff
|
|
2090
|
+
}
|
|
2091
|
+
],
|
|
2092
|
+
[
|
|
2093
|
+
{
|
|
2094
|
+
satoshis: 1000,
|
|
2095
|
+
lockingScript: p2pkh.lock(publicKeyHash)
|
|
2096
|
+
},
|
|
2097
|
+
{
|
|
2098
|
+
lockingScript: p2pkh.lock(publicKeyHash),
|
|
2099
|
+
change: true
|
|
2100
|
+
}
|
|
2101
|
+
],
|
|
2102
|
+
0
|
|
2103
|
+
)
|
|
2104
|
+
})
|
|
2105
|
+
|
|
2106
|
+
it('should generate preimage with default parameters', () => {
|
|
2107
|
+
const preimage = spendTx.preimage()
|
|
2108
|
+
expect(Array.isArray(preimage)).toBe(true)
|
|
2109
|
+
expect(preimage.length).toBeGreaterThan(0)
|
|
2110
|
+
expect(typeof preimage[0]).toBe('number')
|
|
2111
|
+
})
|
|
2112
|
+
|
|
2113
|
+
it('should generate preimage with custom inputIndex', () => {
|
|
2114
|
+
// Add another input to test inputIndex > 0
|
|
2115
|
+
const sourceTx2 = new Transaction(
|
|
2116
|
+
1,
|
|
2117
|
+
[],
|
|
2118
|
+
[
|
|
2119
|
+
{
|
|
2120
|
+
lockingScript: p2pkh.lock(publicKeyHash),
|
|
2121
|
+
satoshis: 2000
|
|
2122
|
+
}
|
|
2123
|
+
],
|
|
2124
|
+
0
|
|
2125
|
+
)
|
|
2126
|
+
spendTx.addInput({
|
|
2127
|
+
sourceTransaction: sourceTx2,
|
|
2128
|
+
sourceOutputIndex: 0,
|
|
2129
|
+
unlockingScript: new UnlockingScript(),
|
|
2130
|
+
sequence: 0xffffffff
|
|
2131
|
+
})
|
|
2132
|
+
|
|
2133
|
+
const preimage0 = spendTx.preimage(0)
|
|
2134
|
+
const preimage1 = spendTx.preimage(1)
|
|
2135
|
+
expect(Array.isArray(preimage0)).toBe(true)
|
|
2136
|
+
expect(Array.isArray(preimage1)).toBe(true)
|
|
2137
|
+
expect(preimage0).not.toEqual(preimage1)
|
|
2138
|
+
})
|
|
2139
|
+
|
|
2140
|
+
it('should generate preimage with custom signatureScope', () => {
|
|
2141
|
+
const defaultPreimage = spendTx.preimage()
|
|
2142
|
+
const customPreimage = spendTx.preimage(0, TransactionSignature.SIGHASH_FORKID | TransactionSignature.SIGHASH_NONE)
|
|
2143
|
+
expect(Array.isArray(customPreimage)).toBe(true)
|
|
2144
|
+
expect(customPreimage).not.toEqual(defaultPreimage)
|
|
2145
|
+
})
|
|
2146
|
+
|
|
2147
|
+
it('should generate preimage with custom subscript', () => {
|
|
2148
|
+
const customScript = LockingScript.fromASM(
|
|
2149
|
+
'OP_CHECKSIG'
|
|
2150
|
+
)
|
|
2151
|
+
const preimage = spendTx.preimage(0, undefined, customScript)
|
|
2152
|
+
expect(Array.isArray(preimage)).toBe(true)
|
|
2153
|
+
})
|
|
2154
|
+
|
|
2155
|
+
it('should throw error for invalid input index (negative)', () => {
|
|
2156
|
+
expect(() => {
|
|
2157
|
+
spendTx.preimage(-1)
|
|
2158
|
+
}).toThrow('Invalid input index')
|
|
2159
|
+
})
|
|
2160
|
+
|
|
2161
|
+
it('should throw error for invalid input index (out of bounds)', () => {
|
|
2162
|
+
expect(() => {
|
|
2163
|
+
spendTx.preimage(1)
|
|
2164
|
+
}).toThrow('Invalid input index')
|
|
2165
|
+
})
|
|
2166
|
+
|
|
2167
|
+
it('should throw error when sourceTransaction is missing', () => {
|
|
2168
|
+
const txWithoutSource = new Transaction(
|
|
2169
|
+
1,
|
|
2170
|
+
[
|
|
2171
|
+
{
|
|
2172
|
+
sourceTXID: '00'.repeat(32),
|
|
2173
|
+
sourceOutputIndex: 0,
|
|
2174
|
+
unlockingScript: new UnlockingScript(),
|
|
2175
|
+
sequence: 0xffffffff
|
|
2176
|
+
}
|
|
2177
|
+
],
|
|
2178
|
+
[
|
|
2179
|
+
{
|
|
2180
|
+
satoshis: 1000,
|
|
2181
|
+
lockingScript: p2pkh.lock(publicKeyHash)
|
|
2182
|
+
}
|
|
2183
|
+
],
|
|
2184
|
+
0
|
|
2185
|
+
)
|
|
2186
|
+
expect(() => {
|
|
2187
|
+
txWithoutSource.preimage()
|
|
2188
|
+
}).toThrow('Source transaction is required')
|
|
2189
|
+
})
|
|
2190
|
+
|
|
2191
|
+
it('should throw error when source transaction output is missing', () => {
|
|
2192
|
+
const tx = new Transaction(
|
|
2193
|
+
1,
|
|
2194
|
+
[
|
|
2195
|
+
{
|
|
2196
|
+
sourceTransaction: new Transaction(1, [], [], 0), // No outputs
|
|
2197
|
+
sourceOutputIndex: 0,
|
|
2198
|
+
unlockingScript: new UnlockingScript(),
|
|
2199
|
+
sequence: 0xffffffff
|
|
2200
|
+
}
|
|
2201
|
+
],
|
|
2202
|
+
[
|
|
2203
|
+
{
|
|
2204
|
+
satoshis: 1000,
|
|
2205
|
+
lockingScript: p2pkh.lock(publicKeyHash)
|
|
2206
|
+
}
|
|
2207
|
+
],
|
|
2208
|
+
0
|
|
2209
|
+
)
|
|
2210
|
+
expect(() => {
|
|
2211
|
+
tx.preimage()
|
|
2212
|
+
}).toThrow('Source transaction\'s output at index 0 is required')
|
|
2213
|
+
})
|
|
2214
|
+
|
|
2215
|
+
it('should throw error when FORKID is not set in signatureScope', () => {
|
|
2216
|
+
expect(() => {
|
|
2217
|
+
spendTx.preimage(0, TransactionSignature.SIGHASH_ALL)
|
|
2218
|
+
}).toThrow('FORKID must be set')
|
|
2219
|
+
})
|
|
2220
|
+
|
|
2221
|
+
it('should throw error for invalid signature coverage', () => {
|
|
2222
|
+
expect(() => {
|
|
2223
|
+
spendTx.preimage(0, TransactionSignature.SIGHASH_FORKID | 0x04) // Invalid coverage
|
|
2224
|
+
}).toThrow('Invalid signature coverage, must be all, none or single')
|
|
2225
|
+
})
|
|
2226
|
+
})
|
|
2057
2227
|
})
|