mirror of
https://github.com/bitcoin/bips.git
synced 2026-03-23 16:05:41 +00:00
BIP-128: exact specification for the checksum calculation (#2121)
This commit is contained in:
@@ -156,13 +156,22 @@ The checksum is calculated by converting the top-level JSON object to an array o
|
||||
SHA256 hash of the result in lowercase hexadecimal format, and taking a prefix of at least 8
|
||||
characters.
|
||||
|
||||
For example:
|
||||
"stringifying" in this context means converting the array to a JSON string following
|
||||
[https://262.ecma-international.org/16.0/index.html#sec-json.stringify ECMAScript's JSON.stringify function specification]
|
||||
(without additional <code>replacer</code> or <code>space</code> arguments).
|
||||
This specification covers special cases such as non-ascii characters and newlines.
|
||||
|
||||
Optional fields without a value should not be included in the key-value pairs array.
|
||||
Also, there should not be a key-value pair for the "checksum" field itself.
|
||||
|
||||
Code example:
|
||||
<source lang="javascript">
|
||||
delete recoveryPlanJson.checksum; // In case the checksum was calculated previously and the object was then modified
|
||||
const checksumData = new TextEncoder().encode(
|
||||
JSON.stringify(Object.entries(recoveryPlanJson).sort()),
|
||||
);
|
||||
const checksum = new Uint8Array(await crypto.subtle.digest('SHA-256', checksumData));
|
||||
const checksumHex = Array.from(checksum).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 8);
|
||||
recoveryPlanJson.checksum = Array.from(checksum).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 8);
|
||||
</source>
|
||||
|
||||
Checksum hex string should be at least 8 characters long. Wallets may choose to use a longer
|
||||
|
||||
Reference in New Issue
Block a user