Skip to content

Commit

Permalink
doc: add ./examples/key-utils.js copy-pasta
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 23, 2024
1 parent 63b113a commit 098340c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions examples/key-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";

let Secp256k1 = require("@dashincubator/secp256k1");

/** @typedef {Required<import('dashtx').TxKeyUtils>} TxKeyUtils */
/**
* @typedef KeyUtilsPartial
* @prop {KeySet} set
*/
/** @typedef {TxKeyUtils & KeyUtilsPartial} KeyUtils */

/**
* @callback KeySet
* @param {String} id
* @param {KeyInfo} keyInfo
*/

/** @type {KeyUtils} */
let KeyUtils = module.exports;

/**
* @typedef KeyInfo
* @prop {String} [walletId] - from DashHd.toId(walletKey)
* @prop {String} [hdpath] - ex: m/44'/5'/0'/0/0
* @prop {String} address - generally convenient
* @prop {Uint8Array} privateKey - for signing
* @prop {Uint8Array} publicKey - for TxInput
* @prop {String} pubKeyHash - for TxOutput
*/

/** @type Object.<String, KeyInfo> */
let keysMap = {};

KeyUtils.set = function (id, keyInfo) {
if (!id) {
throw new Error(`key identifier is not defined)`);
}
keysMap[id] = keyInfo;
};

KeyUtils.sign = async function (privKeyBytes, hashBytes) {
let sigOpts = { canonical: true, extraEntropy: true };
let sigBytes = await Secp256k1.sign(hashBytes, privKeyBytes, sigOpts);
return sigBytes;
};
KeyUtils.getPrivateKey = async function (input) {
if (!input.address) {
//throw new Error('should put the address on the input there buddy...');
console.warn("missing address:", input.txid, input.outputIndex);
return null;
}

let keyInfo = keysMap[input.address];
return keyInfo.privateKey;
};

KeyUtils.getPublicKey = async function (txInput, i) {
let privKeyBytes = await KeyUtils.getPrivateKey(txInput, i);
if (!privKeyBytes) {
return null;
}
let pubKeyBytes = await KeyUtils.toPublicKey(privKeyBytes);

return pubKeyBytes;
};

KeyUtils.toPublicKey = async function (privKeyBytes) {
let isCompressed = true;
let pubKeyBytes = Secp256k1.getPublicKey(privKeyBytes, isCompressed);

return pubKeyBytes;
};

0 comments on commit 098340c

Please sign in to comment.