-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add ./examples/key-utils.js copy-pasta
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |