Skip to content

Commit 351ceef

Browse files
authored
BIP-128: exact specification for the checksum calculation (#2121)
1 parent b382728 commit 351ceef

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

bip-0128.mediawiki

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,22 @@ The checksum is calculated by converting the top-level JSON object to an array o
156156
SHA256 hash of the result in lowercase hexadecimal format, and taking a prefix of at least 8
157157
characters.
158158

159-
For example:
159+
"stringifying" in this context means converting the array to a JSON string following
160+
[https://262.ecma-international.org/16.0/index.html#sec-json.stringify ECMAScript's JSON.stringify function specification]
161+
(without additional <code>replacer</code> or <code>space</code> arguments).
162+
This specification covers special cases such as non-ascii characters and newlines.
163+
164+
Optional fields without a value should not be included in the key-value pairs array.
165+
Also, there should not be a key-value pair for the "checksum" field itself.
166+
167+
Code example:
160168
<source lang="javascript">
169+
delete recoveryPlanJson.checksum; // In case the checksum was calculated previously and the object was then modified
161170
const checksumData = new TextEncoder().encode(
162171
JSON.stringify(Object.entries(recoveryPlanJson).sort()),
163172
);
164173
const checksum = new Uint8Array(await crypto.subtle.digest('SHA-256', checksumData));
165-
const checksumHex = Array.from(checksum).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 8);
174+
recoveryPlanJson.checksum = Array.from(checksum).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 8);
166175
</source>
167176
168177
Checksum hex string should be at least 8 characters long. Wallets may choose to use a longer

0 commit comments

Comments
 (0)