Skip to content

Commit 92a4b1a

Browse files
committed
feat: add KeyUtils.doubleSha256(bytes)
1 parent e26ae34 commit 92a4b1a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

key-utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let Secp256k1 = require("@dashincubator/secp256k1");
88
* @prop {KeySet} set
99
* @prop {KeySignAsn1} signAsn1
1010
* @prop {KeySignMagic} magicSign
11+
* @prop {DoubleSHA256} doubleSha256
1112
* @prop {KeySignP1363} signP1363
1213
* @prop {ASN1ToP1363Signature} asn1ToP1363Signature
1314
*/
@@ -26,6 +27,12 @@ let Secp256k1 = require("@dashincubator/secp256k1");
2627
* @returns {Promise<Uint8Array>}
2728
*/
2829

30+
/**
31+
* @callback DoubleSHA256
32+
* @param {Uint8Array} dataBytes
33+
* @returns {Promise<Uint8Array>}
34+
*/
35+
2936
/**
3037
* @callback KeySignMagic
3138
* @param {Object} opts
@@ -85,6 +92,21 @@ KeyUtils.signAsn1 = async function (privKeyBytes, hashBytes) {
8592
return sigBytes;
8693
};
8794

95+
KeyUtils.doubleSha256 = async function (bytes) {
96+
let firstHash = await sha256(bytes);
97+
let secondHash = await sha256(firstHash);
98+
return secondHash;
99+
};
100+
101+
/**
102+
* @param {Uint8Array} bytes
103+
*/
104+
async function sha256(bytes) {
105+
let hashBuffer = await crypto.subtle.digest("SHA-256", bytes);
106+
let hashBytes = new Uint8Array(hashBuffer);
107+
return hashBytes;
108+
}
109+
88110
KeyUtils.magicSign = async function ({ privKeyBytes, doubleSha256Bytes }) {
89111
if (doubleSha256Bytes?.length !== 32) {
90112
throw new Error(`'doubleSha256Bytes' must be a 32-byte double sha256 hash`);

0 commit comments

Comments
 (0)