Skip to content

Commit d0a2f2d

Browse files
committed
chore: copy TxKeyUtils boilerplate
1 parent 3865876 commit d0a2f2d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

key-utils.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
3+
let Secp256k1 = require("@dashincubator/secp256k1");
4+
5+
/** @typedef {Required<import('dashtx').TxKeyUtils>} TxKeyUtils */
6+
/**
7+
* @typedef KeyUtilsPartial
8+
* @prop {KeySet} set
9+
*/
10+
/** @typedef {TxKeyUtils & KeyUtilsPartial} KeyUtils */
11+
12+
/**
13+
* @callback KeySet
14+
* @param {String} id
15+
* @param {KeyInfo} keyInfo
16+
*/
17+
18+
/** @type {KeyUtils} */
19+
let KeyUtils = module.exports;
20+
21+
/**
22+
* @typedef KeyInfo
23+
* @prop {String} address
24+
* @prop {Uint8Array} privateKey
25+
* @prop {Uint8Array} publicKey
26+
* @prop {String} pubKeyHash
27+
*/
28+
29+
/** @type Object.<String, KeyInfo> */
30+
let keysMap = {};
31+
32+
KeyUtils.set = function (id, keyInfo) {
33+
if (!id) {
34+
throw new Error(`key identifier is not defined)`);
35+
}
36+
keysMap[id] = keyInfo;
37+
};
38+
39+
KeyUtils.sign = async function (privKeyBytes, hashBytes) {
40+
let sigOpts = { canonical: true, extraEntropy: true };
41+
let sigBytes = await Secp256k1.sign(hashBytes, privKeyBytes, sigOpts);
42+
return sigBytes;
43+
};
44+
KeyUtils.getPrivateKey = async function (input) {
45+
if (!input.address) {
46+
//throw new Error('should put the address on the input there buddy...');
47+
console.warn("missing address:", input.txid, input.outputIndex);
48+
return null;
49+
}
50+
51+
let keyInfo = keysMap[input.address];
52+
return keyInfo.privateKey;
53+
};
54+
55+
KeyUtils.getPublicKey = async function (txInput, i) {
56+
let privKeyBytes = await KeyUtils.getPrivateKey(txInput, i);
57+
if (!privKeyBytes) {
58+
return null;
59+
}
60+
let pubKeyBytes = await KeyUtils.toPublicKey(privKeyBytes);
61+
62+
return pubKeyBytes;
63+
};
64+
65+
KeyUtils.toPublicKey = async function (privKeyBytes) {
66+
let isCompressed = true;
67+
let pubKeyBytes = Secp256k1.getPublicKey(privKeyBytes, isCompressed);
68+
69+
return pubKeyBytes;
70+
};

0 commit comments

Comments
 (0)