@smartledger/bsv 9.8.0 → 9.10.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.
@@ -34,14 +34,14 @@ module sounds. That is a deliberate revision: an earlier version of this documen
34
34
  scoped Tier 1 as "the cryptographic core" and would have excluded four of the six real
35
35
  defects this codebase has produced. See §2.1.
36
36
 
37
- ### Tier 1 — 11,384 lines
37
+ ### Tier 1 — 11,976 lines
38
38
 
39
39
  | Module | Lines | Why it matters |
40
40
  | --- | ---: | --- |
41
41
  | `lib/transaction/` | 2,779 | Sighash construction and signing — both BIP-143 and the Original Transaction Digest Algorithm. |
42
42
  | `lib/script/interpreter.js` | 2,821 | **Scoped to the flag and era surface, not opcode execution.** Consensus-flag selection and defaults, era derivation (Genesis/Chronicle), the limits derived from them, and the semantics of the exported `verify()`. Opcode execution is excluded — see §2.2. |
43
43
  | `lib/crypto/` | 2,519 | ECDSA, nonce derivation, signature encoding, the script-number type. **Scope this for an architectural judgement as well as for bugs** — see §2.3. |
44
- | `lib/notaryhash/` | 1,436 | BRC-220 signing and verification. Publicly reachable and relied on downstream. |
44
+ | `lib/notaryhash/` | 2,028 | BRC-220 signing and verification. Publicly reachable and relied on downstream. |
45
45
  | `lib/privatekey.js`, `lib/publickey.js` | 843 | Key construction, serialisation, WIF. Recent defects here produced a *different* key without error. |
46
46
  | `lib/smart_contract/` (targeted) | 472 | `locks.js` and the covenant-facing entrypoints in `index.js`: CLTV and HTLC locking semantics, flag plumbing, and any wrapper claiming mainnet-equivalent verification. Not the whole 6,908-line module. |
47
47
  | `lib/covenant/` | 409 | The verification harness. Its flag word is what made covenants verify under 2019 rules while claiming to mirror mainnet. |
@@ -150,14 +150,14 @@ this repository.
150
150
  | `lib/address.js`, `lib/networks.js`, `lib/opcode.js`, `lib/hdprivatekey.js`, `lib/hdpublickey.js` (2,391 lines) | Cut to pay for §2.1 | Formatting, network constants and BIP-32 derivation. No defect has originated here, and `networks.js` in particular defines addressing constants — pubkey hashes, xpub prefixes, ports, DNS seeds — and contains **no consensus-flag logic at all**. |
151
151
  | The rest of the application layer — `lib/gdaf/`, `lib/ltp/`, `lib/ordinals/`, `lib/block/`, `lib/didweb/`, `lib/vcjwt/`, `lib/statuslist/`, most of `lib/smart_contract/`, plus assorted top-level files | Excluded | ~26,000 lines. Worth a separate engagement; including it here would blur the question in §1. Note the parts of it with a demonstrated defect history have been pulled *into* Tier 1 rather than left here — see §2.1. |
152
152
 
153
- Totals reconcile against `lib/`, which is 39,068 lines across 131 files:
153
+ Totals reconcile against `lib/`, which is 39,660 lines across 131 files:
154
154
 
155
155
  ```
156
- tier 1 11,384
156
+ tier 1 11,976
157
157
  tier 2 1,607
158
158
  excluded 26,077
159
159
  ------
160
- total 39,068
160
+ total 39,660
161
161
  ```
162
162
 
163
163
  Measured 2026-08-29 at `a954c27`. These figures drift as the library changes — an
@@ -228,7 +228,7 @@ seeking a quote for an independent security review.
228
228
 
229
229
  Scope, and we would like these priced separately:
230
230
 
231
- Tier 1 — 11,384 lines. Sighash construction and signing; ECDSA, nonce
231
+ Tier 1 — 11,976 lines. Sighash construction and signing; ECDSA, nonce
232
232
  derivation and signature encoding; the consensus-flag and era-derivation
233
233
  surface of the script interpreter; BRC-220 signing and verification;
234
234
  key construction and serialisation; the covenant verification harness and
@@ -3,8 +3,18 @@
3
3
  §On-chain record specifies *which* Merkle tree batch mode uses, but never says what goes
4
4
  in a leaf. This proposes the definition, with the reasoning that led to it.
5
5
 
6
- Prepared 2026-08-17 while implementing BRC-220 in `@smartledger/bsv`. The second gap of
7
- this kind, after `encoding` — see [BRC220_ENCODING_AMENDMENT.md](BRC220_ENCODING_AMENDMENT.md).
6
+ Prepared 2026-08-17 while implementing BRC-220 in `@smartledger/bsv`.
7
+
8
+ **Status: filed** as [bsv-blockchain/BRCs#246](https://github.com/bsv-blockchain/BRCs/pull/246)
9
+ on 2026-09-11, together with a clarification of the ECDSA digest convention. Its review
10
+ asked that `leafIndex` be stated as counted from 0 and that the vector say how `i` is
11
+ written; both are in #246 and below. Before filing
12
+ it was checked against the BRC-220 reference implementation, whose batcher uses exactly
13
+ this leaf (`leaves = batch.map(e => e.proofHash)`), and the vector below was rebuilt
14
+ without any of this library's code — the root from the reference's own RFC 6962 tree.
15
+
16
+ A companion draft on the `encoding` field was **wrong and has been withdrawn**; see
17
+ [BRC220_ENCODING_AMENDMENT.md](BRC220_ENCODING_AMENDMENT.md).
8
18
 
9
19
  ---
10
20
 
@@ -56,7 +66,7 @@ Insert into **§On-chain record**, replacing the batch bullet's parenthetical.
56
66
  > The leaf datum `d` for a proof is its **`proofHash`** — the 32 bytes defined in
57
67
  > §Canonical proof bytes — so a leaf is `SHA-256(0x00 ‖ proofHash)` and an internal node
58
68
  > is `SHA-256(0x01 ‖ l ‖ r)`. Leaves are ordered as the batch was assembled, and
59
- > `leafIndex` in the certificate's `merkle` object is that position. The tree splits at
69
+ > `leafIndex` in the certificate's `merkle` object is that position, counted from 0. The tree splits at
60
70
  > the largest power of two `< n` and the last leaf is never duplicated, per RFC 6962
61
71
  > §2.1.
62
72
  >
@@ -104,9 +114,10 @@ states it in `lib/notaryhash/index.js` and enforces it in
104
114
  `test/notaryhash/batch_leaf.js` — including a test that a tree built over `canonicalBytes`
105
115
  is rejected, so the choice is checked rather than merely intended.
106
116
 
107
- No deployed batch certificates are known to use the other reading. If any exist, they were
108
- built against an ambiguous sentence and would need reissuing; batch mode is the least-used
109
- of the three and this is the moment to fix it, before that stops being true.
117
+ The reference implementation uses the same reading, so certificates its service has
118
+ issued in batch mode already conform. An implementation that chose `d = canonicalBytes`
119
+ built against an ambiguous sentence and would need to reissue; the second root published
120
+ below makes that diagnosable in one line.
110
121
 
111
122
  ---
112
123
 
@@ -126,10 +137,14 @@ up in the audit-path lengths: `[3, 3, 3, 3, 1]`.
126
137
  Every input is derived from a labelled preimage rather than chosen, so the file
127
138
  regenerates byte-identically and any implementation can rebuild it from scratch:
128
139
 
129
- - private key `i` = `SHA-256("BRC-220/batch-vector/key/" + i)`
140
+ - private key `i` = `SHA-256("BRC-220/batch-vector/key/" + i)`, read as a big-endian integer
130
141
  - `payloadHash` `i` = `SHA-256("BRC-220/batch-vector/payload/" + i)`
131
142
  - `createdAt` `i` = `2026-01-0(i+1)T00:00:00.000Z`
132
- - `algorithm` = `ECDSA-secp256k1`, `hashAlgorithm` = `SHA-256`, `encoding` = `"raw"`
143
+ - `algorithm` = `ECDSA-secp256k1`, `hashAlgorithm` = `SHA-256`, a 64-byte `r ‖ s`
144
+ signature normalised to low-S, and a 33-byte compressed public key
145
+
146
+ where `i` is written as one ASCII decimal digit, `"0"` to `"4"`, so the first key's
147
+ preimage is the 26-byte string `BRC-220/batch-vector/key/0`.
133
148
 
134
149
  **Signing.** The signer signs the 32-byte `payloadHash` directly — the digest *is* the
135
150
  scalar, big-endian. RFC 6979 makes the nonce deterministic, so the signatures, and every
@@ -0,0 +1,246 @@
1
+ # Proposed BRC-220 amendment: define the certificate's field values
2
+
3
+ §Certificate names the twelve fields a certificate must carry and says it is JSON. It
4
+ defines none of their values. This proposes the values the BRC-220 reference
5
+ implementation writes, and two rules for reading them. With both, any implementation
6
+ produces certificates the reference verifies, and reads certificates the way the reference
7
+ does.
8
+
9
+ Prepared 2026-09-11, with the open points decided [below](#decisions). **Status: filed**
10
+ as [bsv-blockchain/BRCs#247](https://github.com/bsv-blockchain/BRCs/pull/247) on 2026-09-11. It is meant to be a separate pull request from
11
+ [bsv-blockchain/BRCs#246](https://github.com/bsv-blockchain/BRCs/pull/246), which is under
12
+ review and deliberately narrow; this text builds on it for the batch leaf datum.
13
+
14
+ ---
15
+
16
+ ## Why it matters
17
+
18
+ Two implementations of BRC-220 filled the gap differently. `@smartledger/bsv` 8.3.0–9.8.0
19
+ and the reference implementation computed identical `proofHash` values, signature digests,
20
+ Merkle trees and on-chain records, and **could not verify a single one of each other's
21
+ certificates**:
22
+
23
+ | field | `@smartledger/bsv` 8.3.0–9.8.0 | reference implementation |
24
+ | --- | --- | --- |
25
+ | `version` | `1` | `"1.0"` |
26
+ | `mode` | `0` / `1` / `2` | `"full"` / `"hybrid"` |
27
+ | a batch | `mode: 2` | `anchor.type: "batch"` |
28
+ | `encoding` | `"raw"` / `"der"` — the signature's byte form | `"hex"` / `"base64"` — how two fields are written |
29
+ | `anchor` | `{ txid, blockHeight }` | `{ type, network, txid, vout, blockHeight, blockTime }` |
30
+ | `merkle.path` | bare hashes | `{ hash, side }` |
31
+
32
+ Every reading on the left is defensible from the current text. None of it is defined there.
33
+ The cryptography is fully specified and the object carrying it is not, so two conformant
34
+ implementations fail each other silently, on the field names a verifier reads first.
35
+
36
+ ## Where the values come from
37
+
38
+ Every value below is what the reference implementation writes, read from its source at
39
+ commit `b926e3b`: the certificate assembly, the batcher, the confirmation poller that adds
40
+ the SPV envelope, and its request schema. They agree with the reference's own protocol
41
+ document, whose §5 carries the same example shapes.
42
+
43
+ The two reader rules go further than the reference's verifier did at `b926e3b`. It accepted
44
+ a certificate of any `version`, and decoded base64 by skipping whatever it did not
45
+ recognise. The reference implementation adopts both rules, and `@smartledger/bsv` applies
46
+ both, so the spec does not state a rule its own reference breaks.
47
+
48
+ The two examples are certificates the reference's own code produced, reproduced in
49
+ `test/data/notaryhash-reference-certs.json`, where each was accepted by the reference's
50
+ schema and verifier. `test/notaryhash/fields_amendment.js` reads them out of this document
51
+ and checks them against that fixture, so the examples cannot drift from what the reference
52
+ actually wrote.
53
+
54
+ ---
55
+
56
+ ## What the spec currently says
57
+
58
+ §Certificate, in full:
59
+
60
+ > A self-contained JSON object, canonicalised via RFC 8785 (JCS) for hashing and transport.
61
+ > Required fields: `protocol`, `version`, `mode`, `algorithm`, `hashAlgorithm`,
62
+ > `payloadHash`, `publicKey`, `signature`, `encoding`, `proofHash`, `createdAt`, `anchor`.
63
+ > A batched certificate additionally carries a `merkle` inclusion proof
64
+ > `{root, leafIndex, leafCount, path[]}` whose folding (leaf → root) must equal the
65
+ > on-chain batch root.
66
+
67
+ That leaves undefined:
68
+
69
+ - `version`: a string or a number, and which one. The canonical bytes carry `u8(version=1)`
70
+ and the domain separator `"NotaryHash/1.0"`, which invite both.
71
+ - `mode`: the on-chain byte (`0`/`1`/`2`) or a name. The on-chain record also has a `kind = 2`
72
+ for batches, which invites treating batch as a third mode.
73
+ - `encoding`: whether it names the bytes' format (raw, DER) or their spelling in JSON.
74
+ - how `payloadHash`, `proofHash`, `publicKey` and `signature` are written.
75
+ - the format of `createdAt`, and how it relates to `createdAtUnix` in the canonical bytes.
76
+ - every member of `anchor`. The spec never lists them.
77
+ - the elements of `merkle.path`. With bare hashes a verifier needs `leafIndex` and
78
+ `leafCount` to fold; with sides it does not.
79
+ - the byte order of `txid`, `spv.blockHash` and `spv.merkleProof.nodes`.
80
+ - what a verifier does with a `version` it does not know, or a field that does not decode.
81
+
82
+ ---
83
+
84
+ ## Proposed text
85
+
86
+ Generated from the text as committed for filing, so the two cannot differ. The ECDSA
87
+ byte forms sit in §Certificate, under the fields they describe, rather than in
88
+ §Algorithms, which #246 edits; a trial merge of #246 on top of this change is clean.
89
+
90
+ ### 1. §Certificate — replace the paragraph
91
+
92
+ > ### Certificate
93
+ >
94
+ > A self-contained JSON object, canonicalised via [RFC 8785 (JCS)](https://www.rfc-editor.org/rfc/rfc8785) for hashing and transport, carrying the twelve required fields below. Hex is written **lowercase, without a `0x` prefix**; a reader MAY accept upper case and the prefix. Numbers are JSON integers. Verifiers ignore members they do not recognise, which is how the SPV envelope is added to a certificate already issued.
95
+ >
96
+ > | field | value |
97
+ > |-------|-------|
98
+ > | `protocol` | the string `"NotaryHash"` |
99
+ > | `version` | the string `"1.0"`: the certificate format version. It corresponds to `u8(version=1)` in the canonical proof bytes and the on-chain record, and to the domain separator `"NotaryHash/1.0"`, but is written as a string. A verifier MUST reject a certificate whose `version` it does not implement. |
100
+ > | `mode` | `"full"` or `"hybrid"`: how the proof is recorded on chain, corresponding to the on-chain `mode` byte `0` or `1`. Batching is marked by `anchor.type`, not by `mode`. |
101
+ > | `algorithm` | an identifier from §Algorithms, e.g. `"ECDSA-secp256k1"` |
102
+ > | `hashAlgorithm` | `"SHA-256"` |
103
+ > | `payloadHash` | the 32-byte payload hash, hex |
104
+ > | `publicKey` | the **full** public key in every mode (hybrid puts only its SHA-256 on chain), written per `encoding` |
105
+ > | `signature` | the **full** signature, as the signer produced it, in every mode, written per `encoding` |
106
+ > | `encoding` | how `publicKey` and `signature` are written: `"hex"`, or `"base64"` (RFC 4648 §4: standard alphabet, padded). It applies to those two fields only, and says nothing about the format of the bytes themselves. A reader MUST reject a value that is not valid in its encoding rather than decode what remains of it — for base64, a character outside the RFC 4648 §4 and §5 alphabets, padding anywhere but the end, or a length no byte string encodes. A reader MAY accept the §5 (URL-safe) alphabet and missing padding. |
107
+ > | `proofHash` | the 32-byte `SHA-256(canonicalBytes)`, hex |
108
+ > | `createdAt` | `createdAtUnix` as an ISO 8601 UTC timestamp with milliseconds, e.g. `"2026-01-01T00:00:00.000Z"`. The milliseconds are always `000`, because only whole seconds enter the canonical bytes; a verifier recovers `createdAtUnix` as the whole seconds the timestamp denotes. Advisory only — see §Verification. |
109
+ > | `anchor` | the object below |
110
+ >
111
+ > For `ECDSA-secp256k1`, `publicKey` is 33 bytes (compressed) or 65 (uncompressed), and `signature` is 64 bytes (`r ‖ s`, each a 32-byte big-endian integer) or DER. A verifier tells them apart by the bytes: DER begins with `0x30` and is not 64 bytes long. `S` is not normalised: the certificate commits, through `proofHash`, to the exact bytes the signer produced, so the malleated form of a signature is a different certificate rather than a forgery of this one.
112
+ >
113
+ > `anchor` locates the on-chain record:
114
+ >
115
+ > | member | value |
116
+ > |--------|-------|
117
+ > | `type` | `"direct"` if the record carries this proof (`mode` `0` or `1`); `"batch"` if it carries a Merkle root (`kind = 2`) |
118
+ > | `network` | the chain the anchoring transaction is on: `"bsv-mainnet"` for BSV mainnet, `"bsv-testnet"` for BSV testnet. Other values are not interoperable. The field is descriptive: which chain the anchor is on is established by the block header the verifier obtains, not by this value. |
119
+ > | `txid` | the anchoring transaction's id, hex, in display order: `reverse(SHA256(SHA256(rawTx)))` |
120
+ > | `vout` | the index of the `OP_RETURN` output within that transaction |
121
+ > | `blockHeight` | the height of the block that mined the transaction, or `null` until it is mined |
122
+ > | `blockTime` | that block's timestamp in Unix seconds, or `null` until it is mined |
123
+ >
124
+ > A batched certificate (`anchor.type` `"batch"`) additionally carries `merkle`, the proof that this proof is one of the batch's leaves:
125
+ >
126
+ > | member | value |
127
+ > |--------|-------|
128
+ > | `root` | the 32-byte batch root, hex; equal to `merkleRoot` in the on-chain batch record |
129
+ > | `leafIndex` | this proof's position in the batch, from `0` |
130
+ > | `leafCount` | the number of proofs in the batch; equal to `leafCount` in the on-chain batch record |
131
+ > | `path` | the audit path, leaf → root, as an array of `{ "hash": <32-byte hex>, "side": "left" or "right" }`. `side` is the sibling's position relative to the running hash. Starting from `SHA256(0x00 ‖ proofHash)`, a `"left"` sibling folds as `SHA256(0x01 ‖ hash ‖ running)` and a `"right"` one as `SHA256(0x01 ‖ running ‖ hash)`. The result must equal `root`. |
132
+ >
133
+ > In a batched certificate `mode` is not checked against the chain: the batch record carries neither the proof nor a mode byte.
134
+ >
135
+ > A direct-anchored ECDSA certificate, before its SPV envelope is attached:
136
+ >
137
+ > <!-- fixture: certificates.fullHex -->
138
+ > ```json
139
+ > {
140
+ > "protocol": "NotaryHash",
141
+ > "version": "1.0",
142
+ > "mode": "full",
143
+ > "algorithm": "ECDSA-secp256k1",
144
+ > "hashAlgorithm": "SHA-256",
145
+ > "payloadHash": "97ef50e782e55cfbfbfb0c6199b96837836b7f7b0fcae76f79a65e2466dc596b",
146
+ > "publicKey": "02375ac16df62a74475844721d6a180927f29314c3455eaa699f7dcf5237c36e52",
147
+ > "signature": "e65171edb82a702a8390fb90f005ba3d4e174ab945ab0bcaf5f1f8d18c3547426ca419ea252b142e863d2eeebdfc787a624bed5a06d23a671959b07b5cb12ba3",
148
+ > "encoding": "hex",
149
+ > "proofHash": "1b34fbeb640e63b366751a279aa749e4f279cfb7e1add1a96150d7fd7c2ff05d",
150
+ > "createdAt": "2026-01-01T00:00:00.000Z",
151
+ > "anchor": {
152
+ > "type": "direct",
153
+ > "network": "bsv-mainnet",
154
+ > "txid": "460d19b875036e41d6dea392bfcb84508120e7b573080c45869eedbae39dec6a",
155
+ > "vout": 0,
156
+ > "blockHeight": 900000,
157
+ > "blockTime": 1767225600
158
+ > }
159
+ > }
160
+ > ```
161
+ >
162
+ > The `merkle` member of the last certificate in a five-proof batch:
163
+ >
164
+ > <!-- fixture: batch.certificates[4].merkle -->
165
+ > ```json
166
+ > {
167
+ > "root": "abb53eb3b2e3530d51685c6813bb85c85892d2f214c2dc504029d071434ac074",
168
+ > "leafIndex": 4,
169
+ > "leafCount": 5,
170
+ > "path": [
171
+ > { "hash": "060ca4e4bbcb647ab0162bb027cd8f08c1577aaa7069166dd629a3df7e57befd", "side": "left" }
172
+ > ]
173
+ > }
174
+ > ```
175
+ >
176
+ > The anchoring transactions and blocks in these examples are test fixtures, not mainnet data. Everything else in them verifies: the signature, `proofHash`, the on-chain record in the transaction, and the batch inclusion.
177
+
178
+ ### 2. §SPV envelope — add at the end of the section
179
+
180
+ > `rawTx` is hex, and `blockHash` is hex in display order, like `txid`. In the `"TSC"` format the entries of `merkleProof.nodes` are hex in display order too, and an entry of `"*"` means "duplicate the working hash", the TSC convention for a missing right sibling. The verifier checks that the header it obtained has hash `spv.blockHash` and height `spv.blockHeight`.
181
+
182
+ ---
183
+
184
+ ## What this changes
185
+
186
+ **For writers, nothing.** Every certificate the reference implementation has issued already
187
+ conforms. The text makes explicit what it writes, so that other implementations can write
188
+ the same.
189
+
190
+ **For readers, two rules tighten.** A verifier that accepted a certificate of another
191
+ version, or decoded base64 by skipping what it did not recognise, refuses instead. Neither
192
+ rule refuses anything a conformant writer produces.
193
+
194
+ The canonical proof bytes, `proofHash`, the on-chain record and the verification steps are
195
+ untouched.
196
+
197
+ ---
198
+
199
+ ## Decisions
200
+
201
+ The first draft left six points to the author. Each is decided here, with the reason.
202
+
203
+ 1. **A verifier refuses a `version` it does not implement.** This is in the proposed text.
204
+ The reference wrote `"1.0"` and read anything: a certificate saying `"2.0"` passed its
205
+ schema, its offline verifier, its SPV check and its anchor match. Reading a future
206
+ format with v1 rules gives a verdict about the wrong thing, and a spec that says nothing
207
+ lets every verifier do that. The reference implementation adopts the rule;
208
+ `@smartledger/bsv` has applied it since 9.9.0.
209
+ 2. **A reader refuses base64 that is not base64.** This is in the proposed text. The
210
+ reference decoded with Node's `Buffer.from`, which skips characters outside the alphabet
211
+ and truncates an impossible length, so a corrupted field became different bytes rather
212
+ than an error. That happened at the reference's own request intake too: a notarize
213
+ request with junk spliced into its base64 key was anchored with the junk dropped. The
214
+ URL-safe alphabet and missing padding stay acceptable, because both are unambiguous and
215
+ refusing them would gain nothing. The reference implementation adopts the rule;
216
+ `@smartledger/bsv` has refused bad characters since 9.9.0, and impossible lengths since
217
+ 9.10.0.
218
+ 3. **Hex is written lowercase and unprefixed; upper case and `0x` MAY be read.** This is in
219
+ the proposed text. Both forms are unambiguous, and the reference already reads them.
220
+ 4. **`network` has two defined names and is descriptive.** This is in the proposed text.
221
+ `"bsv-mainnet"` is what the reference writes. `"bsv-testnet"` is reserved, so that
222
+ testnet certificates do not acquire ad-hoc names. It is not a closed set a verifier must
223
+ enforce. The block header the verifier obtains already fixes which chain the anchor is
224
+ on, so a mislabelled `network` cannot make a certificate verify anywhere it is not
225
+ anchored.
226
+ 5. **Certificates already issued in other shapes stay out of the spec.** The table above
227
+ is one implementation's history. That implementation's readers handle it:
228
+ `@smartledger/bsv` reads those certificates and reports them as legacy. A clause in the
229
+ spec would bind every other implementation to the same history.
230
+ 6. **High-S ECDSA is accepted.** This is in the proposed text, as the reference behaves.
231
+ Requiring low-S would reject certificates the reference has already issued, and
232
+ `proofHash` already stops the malleated form from being passed off as the same
233
+ certificate.
234
+
235
+ ---
236
+
237
+ ## Checking the claims
238
+
239
+ ```
240
+ npx mocha test/notaryhash/fields_amendment.js test/notaryhash/reference_certs.js
241
+ ```
242
+
243
+ The first reads the examples out of this document and checks them against the reference's
244
+ own certificates. It also checks that `@smartledger/bsv` applies the reader rules the text
245
+ states. The second verifies all ten reference certificates and rebuilds each one byte for
246
+ byte from its proof fields, using the values defined here.
@@ -1,100 +1,40 @@
1
- # Proposed BRC-220 amendment: define the `encoding` field
2
-
3
- `encoding` is listed among the certificate's required fields, but its values are never
4
- enumerated. This proposes the definition, with the reasoning that led to it.
5
-
6
- Prepared 2026-08-16 while implementing BRC-220 in `@smartledger/bsv`. The gap surfaced
7
- because an implementation cannot emit a required field whose values are undefined.
8
-
9
- ---
10
-
11
- ## Proposed text
12
-
13
- Insert into **§Certificate**, after the sentence listing the required fields.
14
-
15
- > #### `encoding`
16
- >
17
- > `encoding` names the byte representation of `publicKey` and `signature` as they appear
18
- > in the certificate and in the canonical proof bytes.
19
- >
20
- > Implementations **MUST** support `"raw"` and **SHOULD** emit it:
21
- >
22
- > - **`"raw"`** — the scheme's native fixed-length byte string.
23
- > - For `ECDSA-secp256k1`, a signature is exactly **64 bytes**: `r ‖ s`, each a 32-byte
24
- > big-endian unsigned integer, zero-padded on the left. A public key is the 33-byte
25
- > compressed SEC1 form.
26
- > - For `ML-DSA-*` and `SLH-DSA-*`, the signature and public key are the byte strings the
27
- > scheme itself defines (FIPS 204, FIPS 205). No further framing is applied.
28
- > - **`"der"`** — ECDSA signatures in ASN.1 DER, as produced by Bitcoin tooling.
29
- > Accepted for compatibility with existing Bitcoin-native signers; **SHOULD NOT** be
30
- > emitted by new implementations, for the reason given below. Undefined for post-quantum
31
- > algorithms, which have no DER form.
32
- >
33
- > For `ECDSA-secp256k1`, `s` **MUST** be in the lower half of the curve order
34
- > (`s ≤ n/2`). A signature with a high `s` **MUST** be rejected rather than normalised on
35
- > receipt: normalising changes the signature bytes, and the signature bytes are inside
36
- > `proofHash`.
37
-
38
- ---
39
-
40
- ## Why `"raw"` rather than DER
41
-
42
- ### 1. DER is not canonical, and `proofHash` covers the signature bytes
43
-
44
- The canonical proof bytes include `lp(signature)`, so the signature's exact byte
45
- representation determines `proofHash`. DER does not have one representation per signature.
46
- Measured over 200 signatures from a single key with `@smartledger/bsv`:
47
-
48
- ```
49
- DER lengths: { "69": 1, "70": 108, "71": 91 }
50
- raw lengths: { "64": 200 }
51
- ```
52
-
53
- The variance is ordinary leading-zero handling in the two INTEGERs, and it is entirely
54
- legal DER. But it means the same signing act can yield different `proofHash` values
55
- depending on which library encoded it — and a certificate re-encoded in transit no longer
56
- matches its own integrity root.
57
-
58
- That is the property §Motivation names first: *"any implementation in any language
59
- reproduces identical bytes"*, and the reason the spec already rejects `JSON.stringify`.
60
- A non-canonical signature encoding reintroduces exactly the problem the binary encoding was
61
- chosen to avoid.
62
-
63
- ### 2. Every other algorithm in the spec is already raw
64
-
65
- ML-DSA-65 signatures are 3,309 bytes; SLH-DSA-SHA2-128s are 7,856. Both are fixed-length
66
- byte strings with no DER form. With ECDSA on DER and the post-quantum schemes on raw,
67
- every verifier needs per-algorithm branching on `encoding`. With ECDSA on raw, all sixteen
68
- algorithm identifiers share one representation and the branch disappears.
69
-
70
- ### 3. It matches what the signature actually is
71
-
72
- §Algorithms specifies that the signer signs the 32-byte `payloadHash` **directly**. This is
73
- a detached signature over a digest, not a Bitcoin script signature — the case ES256K
74
- (RFC 7515) and WebCrypto both address, and both use `r ‖ s`, not DER.
75
-
76
- ### 4. Low-S is a separate malleability, and raw does not fix it
77
-
78
- For any ECDSA signature, `s` and `n − s` both verify. They are different bytes, so they
79
- produce different `proofHash` values. Without a normative rule, two valid certificates
80
- exist for one signing act — an ambiguity a notarization protocol should not carry.
81
-
82
- Requiring low-S at signing, and **rejecting** rather than normalising on receipt, keeps
83
- `proofHash` a function of the certificate as issued.
84
-
85
- ---
86
-
87
- ## Note on `toCompact`
88
-
89
- Some Bitcoin libraries expose a 65-byte "compact" signature. That is not this format: it
90
- carries a leading recovery byte for public-key recovery. `"raw"` is the bare 64 bytes,
91
- with no recovery byte, because the public key is already a certificate field.
92
-
93
- ---
94
-
95
- ## Impact on existing certificates
96
-
97
- None, if no certificate has yet been issued with `encoding: "der"`. If any have, they
98
- remain valid — `"der"` stays an accepted value, and the SPV envelope and `proofHash`
99
- semantics are untouched. This amendment defines a field that was previously unspecified;
100
- it does not change any field that was.
1
+ # Withdrawn: proposed BRC-220 `encoding` amendment
2
+
3
+ **This proposal was wrong and has been withdrawn. It was never filed.**
4
+
5
+ It proposed defining the certificate's `encoding` field as `"raw"` or `"der"` — the
6
+ signature's byte format — and requiring ECDSA signatures to be low-S, with a 33-byte
7
+ compressed public key. It was written from this library's implementation alone.
8
+
9
+ Checked against the BRC-220 reference implementation before filing, it contradicted
10
+ it on every point:
11
+
12
+ | | this proposal | reference implementation |
13
+ | --- | --- | --- |
14
+ | `encoding` means | the signature's byte format | how `publicKey` and `signature` are written into the JSON |
15
+ | `encoding` values | `"raw"`, `"der"` | `"hex"`, `"base64"` |
16
+ | signature bytes | 64-byte `r ‖ s` only, DER discouraged | 64-byte `r ‖ s` or DER, told apart by the bytes |
17
+ | high-S signatures | rejected | accepted, deliberately |
18
+ | public keys | 33-byte compressed | 33-byte compressed or 65-byte uncompressed |
19
+
20
+ Every certificate the reference service has issued uses `"hex"` or `"base64"`. Filing
21
+ this would have proposed a spec change incompatible with all of them.
22
+
23
+ It also exposed that `@smartledger/bsv` 8.3.0–9.8.0 could not verify a single
24
+ certificate the reference issued: its `version`, `mode`, `encoding`, `anchor` and batch
25
+ `path` fields all differed. The library now reads both formats, and
26
+ writes the reference one given `format: 'reference'` — the default from 10.0.0. See
27
+ `lib/notaryhash/certificate.js`.
28
+
29
+ ## What is still true
30
+
31
+ The gap this draft set out to close is real: BRC-220 lists `encoding` as a required field
32
+ and never enumerates its values, and says nothing about the JSON form of `version`,
33
+ `mode`, `anchor` or the batch `path`. A clarification defining those fields **as the
34
+ reference writes them** is a separate proposal, drafted in
35
+ [BRC220_CERTIFICATE_FIELDS_AMENDMENT.md](BRC220_CERTIFICATE_FIELDS_AMENDMENT.md).
36
+
37
+ The batch-leaf clarification written alongside this one was correct, and was confirmed
38
+ against the reference implementation: it was filed as
39
+ [bsv-blockchain/BRCs#246](https://github.com/bsv-blockchain/BRCs/pull/246). See
40
+ [BRC220_BATCH_LEAF_AMENDMENT.md](BRC220_BATCH_LEAF_AMENDMENT.md).
@@ -173,45 +173,44 @@ summarising rather than anything in the document.
173
173
  - **The service is a role, not a requirement.** §What a certificate proves: "any party
174
174
  holding a valid (hash, signature, publicKey) triple may re-anchor it; the attestation
175
175
  remains valid". A self-notarizing caller producing its own certificate is conformant.
176
- - **The batch Merkle leaf is NOT settled by the spec — it is a choice this
177
- implementation made.** §On-chain record writes `leaf = SHA256(0x00 ‖ d)`, but that is
178
- RFC 6962's own generic notation for the construction and `d` is never bound to a value.
179
- `canonicalBytes` appears once in the whole document, in the `proofHash` definition, and
180
- nowhere in the batch text. We read `d` as `proofHash`; reading it as `canonicalBytes` is
181
- equally sound and produces a different root, so the two do not interoperate. Recorded as
182
- an ambiguity rather than a settled question, with proposed spec text in
183
- [BRC220_BATCH_LEAF_AMENDMENT.md](BRC220_BATCH_LEAF_AMENDMENT.md) and enforcement in
184
- `test/notaryhash/batch_leaf.js`. This is the second gap of the kind, after `encoding`.
176
+ - **The batch Merkle leaf is NOT settled by the spec.** §On-chain record writes
177
+ `leaf = SHA256(0x00 ‖ d)`, but that is RFC 6962's own generic notation for the
178
+ construction and `d` is never bound to a value. `canonicalBytes` appears once in the whole
179
+ document, in the `proofHash` definition, and nowhere in the batch text. We read `d` as
180
+ `proofHash`; so does the reference implementation's batcher, and given the same five
181
+ proofs it builds the same root. Reading it as `canonicalBytes` is equally sound and
182
+ produces a different root, so the two do not interoperate. Filed upstream as
183
+ [bsv-blockchain/BRCs#246](https://github.com/bsv-blockchain/BRCs/pull/246); see
184
+ [BRC220_BATCH_LEAF_AMENDMENT.md](BRC220_BATCH_LEAF_AMENDMENT.md), enforced in
185
+ `test/notaryhash/batch_leaf.js`.
185
186
  - **`createdAt` is advisory for trust but load-bearing for the hash.** §Verification calls
186
187
  it "an advisory client field only" — meaning proof-of-existence time comes from the
187
188
  block, not from this field. It is still inside the canonical bytes as `createdAtUnix`,
188
189
  so it cannot be altered after issuance.
189
190
 
190
- Decided, and proposed back to the spec:
191
+ Decided here, then reversed against the reference implementation:
191
192
 
192
- - **`encoding` is `"raw"`.** The field is required by the spec but its values were never
193
- enumerated, so this library defines them and proposes the definition upstream — see
194
- `docs/BRC220_ENCODING_AMENDMENT.md`.
193
+ - **`encoding` was defined as `"raw"` — the signature's byte format — and low-S was
194
+ required.** Both were wrong. The spec requires the field without enumerating its values,
195
+ and 8.3.0–9.8.0 filled that gap from this library's own reasoning: raw `r ‖ s` because DER
196
+ is not canonical, low-S because `s` and `n − s` both verify and hash differently.
195
197
 
196
- For `ECDSA-secp256k1` that is 64 bytes, `r ‖ s`, each a 32-byte big-endian integer, with
197
- a 33-byte compressed public key. NOT DER, and not the 65-byte `toCompact` form, which
198
- carries a recovery byte the certificate does not need because it already has the key.
198
+ The reference implementation, which is what issued certificates are checked against,
199
+ decided otherwise on every point. `encoding` is `"hex"` or `"base64"` — how `publicKey`
200
+ and `signature` are written into the JSON, not what their bytes are. A signature may be
201
+ 64-byte `r ‖ s` or DER, told apart by the bytes. A public key may be compressed or
202
+ uncompressed. High-S is accepted deliberately.
199
203
 
200
- The deciding argument is that `proofHash` covers `lp(signature)`, and **DER is not
201
- canonical**. Measured over 200 signatures from one key: DER came out at 69, 70 and 71
202
- bytes depending on leading-zero handling, all of it legal. Raw was 64 bytes every time.
203
- The same signing act producing different `proofHash` values is precisely the failure the
204
- binary encoding exists to prevent — it is why the spec already refuses `JSON.stringify`.
204
+ The reasoning behind the reversal holds: `proofHash` covers the signature bytes exactly
205
+ as the signer produced them, so a DER or malleated form is a *different* certificate,
206
+ not a forgery of this one. What the original argument protected against — two
207
+ certificates for one signing act — is real, but it is the issuer's choice to make, and
208
+ every certificate still commits to exactly one form.
205
209
 
206
- Two supporting reasons: ML-DSA and SLH-DSA have no DER form, so raw makes `encoding`
207
- uniform across all sixteen algorithm identifiers instead of forcing per-algorithm
208
- branching in every verifier; and the signature is detached over a digest rather than a
209
- Bitcoin script signature, which is the ES256K/WebCrypto case, and both use `r ‖ s`.
210
-
211
- **Low-S is required and must be rejected, not normalised.** `s` and `n − s` both verify
212
- and hash differently, so without the rule two valid certificates exist for one signing
213
- act. Normalising on receipt would change the signature bytes, which are inside
214
- `proofHash`. This library already emits low-S and has `Signature.toCanonical()`.
210
+ The library now reads both formats and writes the reference one given
211
+ `format: 'reference'`. Per STABILITY.md the default stays the old format through 9.x,
212
+ with a one-time notice, and becomes 'reference' in 10.0.0. The proposed amendment
213
+ (`docs/BRC220_ENCODING_AMENDMENT.md`) was withdrawn before filing.
215
214
 
216
215
  ### The reference implementation
217
216
 
@@ -220,10 +219,16 @@ Decided, and proposed back to the spec:
220
219
  `txidFromRawTx` against the Bitcoin genesis coinbase, and the Merkle fold against the real
221
220
  block-170 two-transaction proof.
222
221
 
223
- Those vectors are worth more than anything in §6, and should be wired in as gates before
224
- Phase 2 goes far. This module's characteristic failure is being self-consistent and wrong
225
- — every local test green, no other implementation agreeing — and a golden vector from a
226
- second implementation is the only thing that actually rules it out. Our own tests cannot.
222
+ Those vectors are worth more than anything in §6. This module's characteristic failure is
223
+ being self-consistent and wrong — every local test green, no other implementation agreeing
224
+ — and a golden vector from a second implementation is the only thing that actually rules
225
+ it out. Our own tests cannot.
226
+
227
+ That is exactly how it went. Until the reference was checked, every test here passed and
228
+ this library could not verify one certificate the reference had issued: the cryptography
229
+ agreed and the JSON did not. `test/notaryhash/reference_certs.js` now verifies certificates
230
+ the reference produced — full and hybrid, hex and base64, DER, high-S, an uncompressed key,
231
+ and a five-leaf batch — and rebuilds each one byte for byte.
227
232
 
228
233
  ## 8. Not in scope
229
234