Skip to content

Commit

Permalink
fix(types): various TxKeyUtils and Required<TxKeyUtils> updates
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 23, 2024
1 parent db4559d commit 63b113a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
31 changes: 18 additions & 13 deletions bin/create-memo.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,32 @@ async function main() {
console.info(`Fee: ${feeDash} (${feeDuffs})`);
//change.satoshis = sats;

let dashTx = DashTx.create({
/** @type {import('../dashtx.js').TxSign} */
/** @type {import('../dashtx.js').TxKeyUtils} */
let keyUtils = {
sign: async function (privKeyBytes, hashBytes) {
let sigOpts = { canonical: true };
let sigBuf = await Secp256k1.sign(hashBytes, privKeyBytes, sigOpts);
return sigBuf;
},
getPublicKey: async function (txInput, i) {
let privKeyBytes = await keyUtils.getPrivateKey(txInput, i);
if (!privKeyBytes) {
return null;
}
let pubKeyBytes = await keyUtils.toPublicKey(privKeyBytes);

return pubKeyBytes;
},
getPrivateKey: async function () {
return privKeyBytes;
},
toPublicKey:
/**
* @param {Uint8Array} privBytes
* @returns {Promise<Uint8Array>}
*/
async function (privBytes) {
let isCompressed = true;
let pubBytes = Secp256k1.getPublicKey(privBytes, isCompressed);
return pubBytes;
},
});
toPublicKey: async function (privBytes) {
let isCompressed = true;
let pubBytes = Secp256k1.getPublicKey(privBytes, isCompressed);
return pubBytes;
},
};
let dashTx = DashTx.create(keyUtils);

//@ts-ignore
let txInfoSigned = await dashTx.hashAndSignAll(txInfo);
Expand Down
33 changes: 20 additions & 13 deletions dashtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

/**
* @callback TxCreate
* @param {TxDeps} keyUtils
* @param {TxKeyUtils} keyUtils
* @returns {tx}
*/

Expand Down Expand Up @@ -273,6 +273,9 @@ var DashTx = ("object" === typeof module && exports) || {};
return pubKeyBytes;
};
}
/** @type {TxDeps} */
//@ts-ignore
let keyDeps = keyUtils;

let txInst = {};

Expand Down Expand Up @@ -314,7 +317,7 @@ var DashTx = ("object" === typeof module && exports) || {};

for (let i = 0; i < txInfo.inputs.length; i += 1) {
let txInput = txInfo.inputs[i];
let privBytes = await keyUtils.getPrivateKey(txInput, i, txInfo.inputs);
let privBytes = await keyDeps.getPrivateKey(txInput, i, txInfo.inputs);
if (privBytes) {
txInput = await txInst.hashAndSignInput(
privBytes,
Expand Down Expand Up @@ -359,7 +362,7 @@ var DashTx = ("object" === typeof module && exports) || {};
let txBytes = Tx.utils.hexToBytes(txHex);
let txHashBuf = await Tx.doubleSha256(txBytes);

let sigBuf = await keyUtils.sign(privBytes, txHashBuf);
let sigBuf = await keyDeps.sign(privBytes, txHashBuf);
let sigHex = "";
if ("string" === typeof sigBuf) {
console.warn(`sign() should return a Uint8Array of an ASN.1 signature`);
Expand All @@ -370,7 +373,10 @@ var DashTx = ("object" === typeof module && exports) || {};

let pubKeyHex = txInput.publicKey;
if (!pubKeyHex) {
let pubKey = await keyUtils.getPublicKey(txInput, i, txInfo.inputs);
let pubKey = await keyDeps.getPublicKey(txInput, i, txInfo.inputs);
if (!pubKey) {
throw new Error(`no public key for input ${i}`);
}
pubKeyHex = Tx.utils.bytesToHex(pubKey);
}
if ("string" !== typeof pubKeyHex) {
Expand Down Expand Up @@ -1930,12 +1936,13 @@ if ("object" === typeof module) {
*/

/**
* @typedef TxDeps
* @typedef TxKeyUtils
* @prop {TxGetPrivateKey} getPrivateKey
* @prop {TxGetPublicKey} getPublicKey - efficiently get public key bytes
* @prop {TxToPublicKey} [toPublicKey] - convert private bytes to pub bytes
* @prop {TxGetPublicKey} [getPublicKey] - efficiently get public key bytes
* @prop {TxToPublicKey} toPublicKey - convert private bytes to pub bytes
* @prop {TxSign} sign
*/
/** @typedef {Required<TxKeyUtils>} TxDeps */

/**
* @typedef TxFees
Expand Down Expand Up @@ -2151,17 +2158,17 @@ if ("object" === typeof module) {
/**
* @callback TxGetPrivateKey
* @param {TxInputForSig} txInput
* @param {Uint53} i
* @param {Array<TxInputRaw|TxInputForSig>} txInputs
* @returns {Promise<TxPrivateKey>} - private key Uint8Array
* @param {Uint53} [i]
* @param {Array<TxInputRaw|TxInputForSig>} [txInputs]
* @returns {Promise<TxPrivateKey?>} - private key Uint8Array
*/

/**
* @callback TxGetPublicKey
* @param {TxInputForSig} txInput
* @param {Uint53} i
* @param {Array<TxInputRaw|TxInputForSig>} txInputs
* @returns {Promise<TxPublicKey>} - public key Uint8Array
* @param {Uint53} [i]
* @param {Array<TxInputRaw|TxInputForSig>} [txInputs]
* @returns {Promise<TxPublicKey?>} - public key Uint8Array
*/

/**
Expand Down

0 comments on commit 63b113a

Please sign in to comment.