Skip to content

Commit

Permalink
refactor signTransaction to use musig
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 committed May 8, 2024
1 parent 929e3fa commit 63bf929
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src.ts/quais.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export {
export {
accessListify,
computeAddress, recoverAddress,
AbstractTransaction, FewestCoinSelector
AbstractTransaction, FewestCoinSelector,
QiTransaction
} from "./transaction/index.js";

export {
Expand Down Expand Up @@ -120,6 +121,8 @@ export {
encryptKeystoreJson, encryptKeystoreJsonSync,

quaiHDAccountPath, qiHDAccountPath,

nobleCrypto,
} from "./wallet/index.js";

export {
Expand Down
4 changes: 3 additions & 1 deletion src.ts/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ export { FewestCoinSelector } from "./coinselector-fewest.js";

export type { TransactionLike } from "./abstract-transaction.js";

export type {TxInput, TxOutput} from "./utxo.js";
export type {TxInput, TxOutput} from "./utxo.js";

export { QiTransaction } from "./qi-transaction.js";
4 changes: 3 additions & 1 deletion src.ts/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ export type {
KeystoreAccount, EncryptOptions
} from "./json-keystore.js"

export { UTXOHDWallet } from "./utxohdwallet.js";
export { UTXOHDWallet } from "./utxohdwallet.js";

export { nobleCrypto } from "./musig-crypto.js";
19 changes: 13 additions & 6 deletions src.ts/wallet/utxohdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,22 @@ export class UTXOHDWallet extends BaseWallet {
private createMuSigSignature(tx: QiTransaction, hash: Uint8Array): string {
const musig = MuSigFactory(nobleCrypto);

const privKeys = tx.txInputs!.map(input => {
// Collect private keys corresponding to the addresses of the inputs
const privKeysSet = new Set<string>();
tx.txInputs!.forEach(input => {
const address = computeAddress(hexlify(input.pubKey));
const utxoAddrObj = this.utxoAddresses.find(utxoAddr => utxoAddr.address === address);
return utxoAddrObj ? utxoAddrObj.privKey : null;
}).filter(privKey => privKey !== null);
if (!utxoAddrObj) {
throw new Error(`Private key not found for public key associated with address: ${address}`);
}
privKeysSet.add(utxoAddrObj.privKey);
});
const privKeys = Array.from(privKeysSet);

// Create an array of public keys corresponding to the private keys for musig aggregation
const pubKeys: Uint8Array[] = privKeys.map(privKey => nobleCrypto.getPublicKey(getBytes(privKey!), true)).filter(pubKey => pubKey !== null) as Uint8Array[];

// Generate nonces for each public key
const nonces = pubKeys.map(pk => musig.nonceGen({publicKey: getBytes(pk!)}));
const aggNonce = musig.nonceAgg(nonces);

Expand All @@ -403,7 +411,7 @@ export class UTXOHDWallet extends BaseWallet {
pubKeys
);

//Each signer creates a partial signature
// Create partial signatures for each private key
const partialSignatures = privKeys.map((sk, index) =>
musig.partialSign({
secretKey: getBytes(sk || ''),
Expand All @@ -415,8 +423,7 @@ export class UTXOHDWallet extends BaseWallet {

// Aggregate the partial signatures into a final aggregated signature
const finalSignature = musig.signAgg(partialSignatures, signingSession);

// const isValid = schnorr.verify(finalSignature, hash, aggPublicKey);

return hexlify(finalSignature);
}

Expand Down

0 comments on commit 63bf929

Please sign in to comment.