Skip to content

Commit 63b113a

Browse files
committed
fix(types): various TxKeyUtils and Required<TxKeyUtils> updates
1 parent db4559d commit 63b113a

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

bin/create-memo.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,32 @@ async function main() {
9797
console.info(`Fee: ${feeDash} (${feeDuffs})`);
9898
//change.satoshis = sats;
9999

100-
let dashTx = DashTx.create({
101-
/** @type {import('../dashtx.js').TxSign} */
100+
/** @type {import('../dashtx.js').TxKeyUtils} */
101+
let keyUtils = {
102102
sign: async function (privKeyBytes, hashBytes) {
103103
let sigOpts = { canonical: true };
104104
let sigBuf = await Secp256k1.sign(hashBytes, privKeyBytes, sigOpts);
105105
return sigBuf;
106106
},
107+
getPublicKey: async function (txInput, i) {
108+
let privKeyBytes = await keyUtils.getPrivateKey(txInput, i);
109+
if (!privKeyBytes) {
110+
return null;
111+
}
112+
let pubKeyBytes = await keyUtils.toPublicKey(privKeyBytes);
113+
114+
return pubKeyBytes;
115+
},
107116
getPrivateKey: async function () {
108117
return privKeyBytes;
109118
},
110-
toPublicKey:
111-
/**
112-
* @param {Uint8Array} privBytes
113-
* @returns {Promise<Uint8Array>}
114-
*/
115-
async function (privBytes) {
116-
let isCompressed = true;
117-
let pubBytes = Secp256k1.getPublicKey(privBytes, isCompressed);
118-
return pubBytes;
119-
},
120-
});
119+
toPublicKey: async function (privBytes) {
120+
let isCompressed = true;
121+
let pubBytes = Secp256k1.getPublicKey(privBytes, isCompressed);
122+
return pubBytes;
123+
},
124+
};
125+
let dashTx = DashTx.create(keyUtils);
121126

122127
//@ts-ignore
123128
let txInfoSigned = await dashTx.hashAndSignAll(txInfo);

dashtx.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
/**
6969
* @callback TxCreate
70-
* @param {TxDeps} keyUtils
70+
* @param {TxKeyUtils} keyUtils
7171
* @returns {tx}
7272
*/
7373

@@ -273,6 +273,9 @@ var DashTx = ("object" === typeof module && exports) || {};
273273
return pubKeyBytes;
274274
};
275275
}
276+
/** @type {TxDeps} */
277+
//@ts-ignore
278+
let keyDeps = keyUtils;
276279

277280
let txInst = {};
278281

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

315318
for (let i = 0; i < txInfo.inputs.length; i += 1) {
316319
let txInput = txInfo.inputs[i];
317-
let privBytes = await keyUtils.getPrivateKey(txInput, i, txInfo.inputs);
320+
let privBytes = await keyDeps.getPrivateKey(txInput, i, txInfo.inputs);
318321
if (privBytes) {
319322
txInput = await txInst.hashAndSignInput(
320323
privBytes,
@@ -359,7 +362,7 @@ var DashTx = ("object" === typeof module && exports) || {};
359362
let txBytes = Tx.utils.hexToBytes(txHex);
360363
let txHashBuf = await Tx.doubleSha256(txBytes);
361364

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

371374
let pubKeyHex = txInput.publicKey;
372375
if (!pubKeyHex) {
373-
let pubKey = await keyUtils.getPublicKey(txInput, i, txInfo.inputs);
376+
let pubKey = await keyDeps.getPublicKey(txInput, i, txInfo.inputs);
377+
if (!pubKey) {
378+
throw new Error(`no public key for input ${i}`);
379+
}
374380
pubKeyHex = Tx.utils.bytesToHex(pubKey);
375381
}
376382
if ("string" !== typeof pubKeyHex) {
@@ -1930,12 +1936,13 @@ if ("object" === typeof module) {
19301936
*/
19311937

19321938
/**
1933-
* @typedef TxDeps
1939+
* @typedef TxKeyUtils
19341940
* @prop {TxGetPrivateKey} getPrivateKey
1935-
* @prop {TxGetPublicKey} getPublicKey - efficiently get public key bytes
1936-
* @prop {TxToPublicKey} [toPublicKey] - convert private bytes to pub bytes
1941+
* @prop {TxGetPublicKey} [getPublicKey] - efficiently get public key bytes
1942+
* @prop {TxToPublicKey} toPublicKey - convert private bytes to pub bytes
19371943
* @prop {TxSign} sign
19381944
*/
1945+
/** @typedef {Required<TxKeyUtils>} TxDeps */
19391946

19401947
/**
19411948
* @typedef TxFees
@@ -2151,17 +2158,17 @@ if ("object" === typeof module) {
21512158
/**
21522159
* @callback TxGetPrivateKey
21532160
* @param {TxInputForSig} txInput
2154-
* @param {Uint53} i
2155-
* @param {Array<TxInputRaw|TxInputForSig>} txInputs
2156-
* @returns {Promise<TxPrivateKey>} - private key Uint8Array
2161+
* @param {Uint53} [i]
2162+
* @param {Array<TxInputRaw|TxInputForSig>} [txInputs]
2163+
* @returns {Promise<TxPrivateKey?>} - private key Uint8Array
21572164
*/
21582165

21592166
/**
21602167
* @callback TxGetPublicKey
21612168
* @param {TxInputForSig} txInput
2162-
* @param {Uint53} i
2163-
* @param {Array<TxInputRaw|TxInputForSig>} txInputs
2164-
* @returns {Promise<TxPublicKey>} - public key Uint8Array
2169+
* @param {Uint53} [i]
2170+
* @param {Array<TxInputRaw|TxInputForSig>} [txInputs]
2171+
* @returns {Promise<TxPublicKey?>} - public key Uint8Array
21652172
*/
21662173

21672174
/**

0 commit comments

Comments
 (0)