@smartledger/bsv 9.7.0 → 9.8.0
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/CHANGELOG.md +123 -0
- package/README.md +19 -19
- package/bsv-gdaf.min.js +4 -4
- package/bsv-smartcontract.min.js +1 -1
- package/bsv.bundle.js +4 -4
- package/bsv.d.ts +301 -54
- package/bsv.min.js +4 -4
- package/docs/AUDIT_SCOPE.md +39 -13
- package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
- package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
- package/docs/audit-rfq/cure53.txt +24 -10
- package/docs/audit-rfq/ncc-group.txt +24 -10
- package/docs/audit-rfq/trail-of-bits.txt +24 -10
- package/docs/getting-started/INSTALLATION.md +23 -23
- package/docs/getting-started/QUICK_START.md +7 -7
- package/docs/migration/FROM_BSV_1_5_6.md +5 -5
- package/docs/preimage.md +151 -87
- package/package.json +7 -3
- package/version.js +1 -1
package/docs/preimage.md
CHANGED
|
@@ -1,126 +1,190 @@
|
|
|
1
|
-
|
|
1
|
+
# The BIP-143 sighash preimage
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The preimage is the byte string that is double-SHA256'd and then signed. Its layout is
|
|
4
|
+
what makes covenants possible on BSV: a script that can prove it was handed *this*
|
|
5
|
+
transaction's preimage can read the transaction's own fields out of it, which is why
|
|
6
|
+
BSV needs no `OP_TXLOCKTIME` or `OP_INPUTVALUE` opcode.
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
Every offset and every script fragment on this page is asserted in
|
|
9
|
+
`test/covenant/preimage_doc.js` against a preimage this library actually produces. If
|
|
10
|
+
you change the layout, that test fails before this page goes stale.
|
|
6
11
|
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
> **Two digest algorithms, not one.** BIP-143 with `SIGHASH_FORKID` is what this page
|
|
13
|
+
> describes and what almost everything uses. Chronicle restored the **Original
|
|
14
|
+
> Transaction Digest Algorithm**, selected by `SIGHASH_CHRONICLE` (`0x20`), which has a
|
|
15
|
+
> different layout entirely — see `Signature.SIGHASH_CHRONICLE` and
|
|
16
|
+
> `lib/transaction/sighash.js`. Do not assume a signature is BIP-143 without checking
|
|
17
|
+
> its sighash byte.
|
|
9
18
|
|
|
10
19
|
---
|
|
11
20
|
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
|
|
|
15
|
-
|
|
|
16
|
-
|
|
|
17
|
-
|
|
|
18
|
-
|
|
|
19
|
-
|
|
|
20
|
-
|
|
|
21
|
-
|
|
|
22
|
-
|
|
|
23
|
-
|
|
|
24
|
-
|
|
|
25
|
-
|
|
|
26
|
-
|
|
21
|
+
## Field layout
|
|
22
|
+
|
|
23
|
+
| # | Field | Bytes | Encoding |
|
|
24
|
+
| --: | --- | --: | --- |
|
|
25
|
+
| 1 | `nVersion` | 4 | little-endian int32 |
|
|
26
|
+
| 2 | `hashPrevouts` | 32 | HASH256 digest, as the hash function returns it |
|
|
27
|
+
| 3 | `hashSequence` | 32 | HASH256 digest, as the hash function returns it |
|
|
28
|
+
| 4 | `outpoint` | 36 | 32-byte txid in **serialized order** + 4-byte LE index |
|
|
29
|
+
| 5 | `scriptCode` | varint + N | length prefix then the script |
|
|
30
|
+
| 6 | `amount` | 8 | little-endian **uint64** — satoshis in the output being spent |
|
|
31
|
+
| 7 | `nSequence` | 4 | little-endian **uint32** |
|
|
32
|
+
| 8 | `hashOutputs` | 32 | HASH256 digest, as the hash function returns it |
|
|
33
|
+
| 9 | `nLockTime` | 4 | little-endian **uint32** |
|
|
34
|
+
| 10 | `sighashType` | 4 | little-endian uint32 (`0x41` for `ALL|FORKID`) |
|
|
35
|
+
|
|
36
|
+
**The three 32-byte hashes have no endianness worth naming.** They are the bytes
|
|
37
|
+
`HASH256()` returns, inserted as-is. Calling them "big-endian" invites someone to
|
|
38
|
+
reverse them; the only field in the preimage that is genuinely reversed relative to how
|
|
39
|
+
you normally read it is the **txid inside the outpoint**, which appears here in
|
|
40
|
+
serialized order — the reverse of the string an explorer shows.
|
|
41
|
+
|
|
42
|
+
### Length
|
|
27
43
|
|
|
28
|
-
|
|
44
|
+
```
|
|
45
|
+
4 + 32 + 32 + 36 + 8 + 4 + 32 + 4 + 4 = 156 fixed bytes
|
|
46
|
+
+ varint(len(scriptCode)) + len(scriptCode)
|
|
47
|
+
```
|
|
29
48
|
|
|
30
|
-
|
|
49
|
+
A 25-byte P2PKH `scriptCode` needs a 1-byte length prefix, so the whole preimage is
|
|
50
|
+
**182 bytes** — not ~108, and not 181. There is no "typical" length beyond that,
|
|
51
|
+
because `scriptCode` is whatever locking script is being spent.
|
|
52
|
+
|
|
53
|
+
### Offsets, for a 25-byte scriptCode
|
|
54
|
+
|
|
55
|
+
| Field | From start | From end |
|
|
56
|
+
| --- | --: | --: |
|
|
57
|
+
| `nVersion` | 0 | — |
|
|
58
|
+
| `hashPrevouts` | 4 | — |
|
|
59
|
+
| `hashSequence` | 36 | — |
|
|
60
|
+
| `outpoint` | 68 | — |
|
|
61
|
+
| `scriptCode` (len + data) | 104 | — |
|
|
62
|
+
| `amount` | 130 | **52** |
|
|
63
|
+
| `nSequence` | 138 | **44** |
|
|
64
|
+
| `hashOutputs` | 142 | **40** |
|
|
65
|
+
| `nLockTime` | 174 | **8** |
|
|
66
|
+
| `sighashType` | 178 | **4** |
|
|
67
|
+
|
|
68
|
+
**Use the from-end column in scripts.** Everything before `amount` sits behind a
|
|
69
|
+
variable-length `scriptCode`, so its absolute offset changes with the contract. The
|
|
70
|
+
five tail fields are at fixed distances from the end whatever the script contains,
|
|
71
|
+
which is why every extraction this library ships is written with `OP_RIGHT`.
|
|
31
72
|
|
|
32
|
-
|
|
73
|
+
---
|
|
33
74
|
|
|
34
|
-
|
|
35
|
-
| ----------------------- | ------------- | ------------------------ |
|
|
36
|
-
| nVersion | 4 | `01000000` |
|
|
37
|
-
| hashPrevouts | 32 | `e3...4a` |
|
|
38
|
-
| hashSequence | 32 | `00...00` |
|
|
39
|
-
| outpoint | 36 | `00..00 00000000` |
|
|
40
|
-
| scriptCode (len + data) | 25 | `1976a914...88ac` |
|
|
41
|
-
| amount | 8 | `1027000000000000` |
|
|
42
|
-
| nSequence | 4 | `ffffffff` |
|
|
43
|
-
| hashOutputs | 32 | `d2...1e` |
|
|
44
|
-
| nLockTime | 4 | `e8030000` *(= 1000 LE)* |
|
|
45
|
-
| sighashType | 4 | `41000000` *(= 0x41)* |
|
|
75
|
+
## When the hashes are zero
|
|
46
76
|
|
|
47
|
-
|
|
77
|
+
`hashPrevouts`, `hashSequence` and `hashOutputs` are not always populated. Measured:
|
|
48
78
|
|
|
49
|
-
|
|
79
|
+
| Sighash type | `hashPrevouts` | `hashSequence` | `hashOutputs` |
|
|
80
|
+
| --- | --- | --- | --- |
|
|
81
|
+
| `ALL|FORKID` (`0x41`) | set | set | set |
|
|
82
|
+
| `NONE|FORKID` (`0x42`) | set | **zero** | **zero** |
|
|
83
|
+
| `SINGLE|FORKID` (`0x43`) | set | **zero** | set (the one matching output) |
|
|
84
|
+
| `ALL|ANYONECANPAY|FORKID` (`0xc1`) | **zero** | **zero** | set |
|
|
85
|
+
| `SINGLE|ANYONECANPAY|FORKID` (`0xc3`) | **zero** | **zero** | set |
|
|
50
86
|
|
|
51
|
-
|
|
52
|
-
|
|
87
|
+
A covenant that reads `hashOutputs` is only meaningful if it *also* pins the sighash
|
|
88
|
+
type — otherwise a spender signs with `NONE` and the field it is checking is 32 zero
|
|
89
|
+
bytes. `PushTx.assertSighashType()` exists for that.
|
|
53
90
|
|
|
54
91
|
---
|
|
55
92
|
|
|
56
|
-
##
|
|
93
|
+
## Reading fields in Script
|
|
57
94
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
95
|
+
`OP_RIGHT n` keeps the last `n` bytes; `OP_LEFT n` keeps the first `n`. Together they
|
|
96
|
+
take a window at a fixed distance from the end. These are the fragments this library
|
|
97
|
+
ships:
|
|
61
98
|
|
|
62
|
-
|
|
99
|
+
| Field | Script |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `sighashType` | `OP_DUP 4 OP_RIGHT` |
|
|
102
|
+
| `nLockTime` | `OP_DUP 8 OP_RIGHT 4 OP_LEFT` |
|
|
103
|
+
| `hashOutputs` | `OP_DUP 40 OP_RIGHT 32 OP_LEFT` |
|
|
104
|
+
| `nSequence` | `OP_DUP 44 OP_RIGHT 4 OP_LEFT` |
|
|
105
|
+
| `amount` | `OP_DUP 52 OP_RIGHT 8 OP_LEFT` |
|
|
106
|
+
| `nVersion` | `OP_DUP 4 OP_LEFT` |
|
|
107
|
+
|
|
108
|
+
`OP_DUP` first, because these consume the preimage and you usually want it back.
|
|
109
|
+
|
|
110
|
+
### `OP_BIN2NUM` will silently corrupt three of these
|
|
63
111
|
|
|
64
|
-
|
|
112
|
+
`amount`, `nSequence` and `nLockTime` are **unsigned**. `OP_BIN2NUM` produces a
|
|
113
|
+
**signed** script number. Feeding a raw field straight into it is wrong, and wrong in
|
|
114
|
+
the direction that fails quietly:
|
|
65
115
|
|
|
66
116
|
```
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
[0064-007e] script length + scriptPubKey (varies)
|
|
73
|
-
[007f-0086] amount (8 LE)
|
|
74
|
-
[0087-008a] nSequence (4 LE)
|
|
75
|
-
[008b-00aa] hashOutputs (32)
|
|
76
|
-
[00ab-00ae] nLockTime (4 LE)
|
|
77
|
-
[00af-00b2] sighashType (4 LE)
|
|
117
|
+
nLockTime e8030000 -> 1000 correct
|
|
118
|
+
ffffff7f -> 2147483647 correct — last value before the sign bit
|
|
119
|
+
00000080 -> 0 WRONG, should be 2147483648 (19 Jan 2038)
|
|
120
|
+
01000080 -> -1 WRONG, should be 2147483649
|
|
121
|
+
ffffffff -> -2147483647 WRONG, should be 4294967295 (year 2106)
|
|
78
122
|
```
|
|
79
123
|
|
|
80
|
-
|
|
124
|
+
A `nLockTime >= deadline` check written this way stops working on **19 January 2038**,
|
|
125
|
+
and a `<=` check starts passing when it should not. The same applies to `amount` above
|
|
126
|
+
2³¹ satoshis (21.47 BSV) and to `nSequence` above `0x7fffffff` — which includes
|
|
127
|
+
`0xffffffff`, the most common value there is.
|
|
81
128
|
|
|
82
|
-
|
|
129
|
+
**Sign-pad with a zero byte first:**
|
|
83
130
|
|
|
84
|
-
|
|
131
|
+
```
|
|
132
|
+
OP_DUP 8 OP_RIGHT 4 OP_LEFT <00> OP_CAT OP_BIN2NUM
|
|
133
|
+
```
|
|
85
134
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
135
|
+
```
|
|
136
|
+
nLockTime 00000080 -> 2147483648 correct
|
|
137
|
+
ffffffff -> 4294967295 correct
|
|
138
|
+
e8030000 -> 1000 correct — OP_BIN2NUM re-minimises the pad
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
That five-byte push is only legal because `OP_BIN2NUM` honours the era's script-number
|
|
142
|
+
width — 4 bytes before Genesis, 750,000 after, 32,000,000 after Chronicle. Verify with
|
|
143
|
+
`Interpreter.mainnetFlags()` or no flags at all; a hand-assembled flag word with no era
|
|
144
|
+
bit applies the 4-byte pre-Genesis cap and rejects the padded value.
|
|
92
145
|
|
|
93
146
|
---
|
|
94
147
|
|
|
95
|
-
##
|
|
148
|
+
## Hashing the preimage does not authenticate it
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
<preimage> OP_HASH256
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
gives you `HASH256(preimage)` and **nothing else**. It does not establish that the
|
|
155
|
+
bytes on the stack are this transaction's preimage. A spender can hand you any
|
|
156
|
+
well-formed 182 bytes, satisfy every field check you wrote, and spend the output on a
|
|
157
|
+
transaction that does something entirely different.
|
|
96
158
|
|
|
97
|
-
The
|
|
98
|
-
|
|
159
|
+
The binding has to come from a signature check. `OP_PUSH_TX` — `lib/covenant/pushtx.js`
|
|
160
|
+
— gets it by constructing a signature *from the preimage hash itself* and verifying it
|
|
161
|
+
with `OP_CHECKSIG` against the generator point:
|
|
99
162
|
|
|
100
163
|
```
|
|
101
|
-
|
|
164
|
+
OP_HASH256 z = HASH256(preimage)
|
|
165
|
+
<reverse to LE> e
|
|
166
|
+
<Gx> OP_ADD <n> OP_MOD s = (e + Gx) mod n
|
|
167
|
+
<assemble DER: r = Gx, s> a signature that is valid for P = G
|
|
168
|
+
only if e is this input's sighash
|
|
169
|
+
<02||Gx> OP_CHECKSIG the node checks it against the REAL sighash
|
|
102
170
|
```
|
|
103
171
|
|
|
104
|
-
|
|
172
|
+
`OP_CHECKSIG` computes the sighash itself, from the transaction being validated. The
|
|
173
|
+
signature only verifies if the `e` derived from the supplied preimage equals it. That
|
|
174
|
+
is the whole trick, and it is the step that makes every field check downstream mean
|
|
175
|
+
something.
|
|
105
176
|
|
|
106
|
-
|
|
107
|
-
|
|
177
|
+
Use `PushTx.pushTxCore()` or `SmartContract.policy()` rather than assembling this by
|
|
178
|
+
hand.
|
|
108
179
|
|
|
109
180
|
---
|
|
110
181
|
|
|
111
|
-
##
|
|
112
|
-
|
|
113
|
-
| Field | Bytes | Endian | Covenant Extraction Example |
|
|
114
|
-
| ------------ | -------- | ------ | ------------------------------------------------------- |
|
|
115
|
-
| nVersion | 4 | LE | `4 OP_SPLIT OP_DROP OP_BIN2NUM` |
|
|
116
|
-
| hashPrevouts | 32 | — | `36 OP_SPLIT ...` |
|
|
117
|
-
| hashSequence | 32 | — | next 32 |
|
|
118
|
-
| outpoint | 36 | mixed | — |
|
|
119
|
-
| scriptCode | variable | — | rarely enforced directly |
|
|
120
|
-
| amount | 8 | LE | `40 OP_SPLIT OP_NIP 8 OP_SPLIT OP_DROP OP_BIN2NUM` |
|
|
121
|
-
| nSequence | 4 | LE | `12 OP_SPLIT OP_NIP 4 OP_SPLIT OP_DROP OP_BIN2NUM` |
|
|
122
|
-
| hashOutputs | 32 | — | — |
|
|
123
|
-
| nLockTime | 4 | LE | `<len-8> OP_SPLIT OP_NIP 4 OP_SPLIT OP_DROP OP_BIN2NUM` |
|
|
124
|
-
| sighashType | 4 | LE | `<len-4> OP_SPLIT OP_NIP OP_BIN2NUM` |
|
|
182
|
+
## `scriptCode` is not "the scriptPubKey"
|
|
125
183
|
|
|
126
|
-
|
|
184
|
+
It is the script the signature commits to. For a plain P2PKH spend that happens to be
|
|
185
|
+
the 25-byte locking script, but nothing in the format requires it: it is
|
|
186
|
+
length-prefixed and variable, `OP_CODESEPARATOR` changes where it starts, and for the
|
|
187
|
+
covenants this library builds it is hundreds of bytes.
|
|
188
|
+
|
|
189
|
+
Any parser that assumes 25 bytes, or that reads fields at absolute offsets past the
|
|
190
|
+
`scriptCode`, breaks on the first real contract. Read from the end.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartledger/bsv",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.8.0",
|
|
4
4
|
"description": "Bitcoin SV library with an interpreter-verified script engine: OP_PUSH_TX covenants, BIP-143 preimage tooling, and consensus flags that match what miners actually enforce. Also ships DID:web / W3C VC-JWT credentials and the Legal Token Protocol.",
|
|
5
5
|
"author": "SmartLedger Technology <hello@smartledger.technology> (https://smartledger.technology)",
|
|
6
6
|
"homepage": "https://github.com/codenlighten/smartledger-bsv#readme",
|
|
@@ -135,13 +135,17 @@
|
|
|
135
135
|
"test:browser": "echo 'Open tests/standalone-modules-test.html in browser for comprehensive testing'",
|
|
136
136
|
"test:bundle": "echo 'Open tests/bundle-completeness-test.html in browser to verify bundle completeness'",
|
|
137
137
|
"preimage:extract": "node examples/preimage/extract_preimage_bidirectional.js",
|
|
138
|
-
"prepublishOnly": "npm run build-all && npm run check:readme && npm run check:api && npm run check:types",
|
|
138
|
+
"prepublishOnly": "npm run build-all && npm run check:readme && npm run check:api && npm run check:types && npm run check:dts",
|
|
139
139
|
"sync-cdn": "node scripts/sync-cdn-urls.js",
|
|
140
140
|
"version": "node scripts/sync-cdn-urls.js && node scripts/sync-version.js && node scripts/gen-esm-wrapper.js && git add README.md docs version.js index.mjs",
|
|
141
141
|
"test:browser:ci": "node tests/browser-smoke-runner.js",
|
|
142
142
|
"api:snapshot": "node scripts/api-surface.js",
|
|
143
143
|
"check:api": "mocha test/api_surface.js --no-config",
|
|
144
|
-
"check:types": "node scripts/check-types.js"
|
|
144
|
+
"check:types": "node scripts/check-types.js",
|
|
145
|
+
"check:dts": "node scripts/check-dts-coverage.js",
|
|
146
|
+
"check:audit-scope": "node scripts/check-audit-scope.js",
|
|
147
|
+
"check:audit-scope:fix": "node scripts/check-audit-scope.js --fix",
|
|
148
|
+
"check:dts:update": "node scripts/check-dts-coverage.js --update"
|
|
145
149
|
},
|
|
146
150
|
"unpkg": "bsv.min.js",
|
|
147
151
|
"jsdelivr": "bsv.min.js",
|
package/version.js
CHANGED