From 761f0c927c6c9a06256e8b557e0780579a373e26 Mon Sep 17 00:00:00 2001 From: janniks Date: Wed, 31 Jul 2024 23:00:42 +0200 Subject: [PATCH] feat: Reduce wrapping of types for public key appending --- packages/transactions/src/builders.ts | 3 +-- packages/transactions/src/transaction.ts | 28 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/transactions/src/builders.ts b/packages/transactions/src/builders.ts index 9d0f3caa6..33e5f46f1 100644 --- a/packages/transactions/src/builders.ts +++ b/packages/transactions/src/builders.ts @@ -730,8 +730,7 @@ function mutatingSignAppendMultiSig( signer.signOrigin(signerKey); } else { // or append the public key (which did not sign here) - // todo: remove wrapper type here as well `next` - signer.appendOrigin(createStacksPublicKey(publicKey)); + signer.appendOrigin(publicKey); } } } diff --git a/packages/transactions/src/transaction.ts b/packages/transactions/src/transaction.ts index b4e1a4280..74260ad4a 100644 --- a/packages/transactions/src/transaction.ts +++ b/packages/transactions/src/transaction.ts @@ -44,7 +44,7 @@ import { anchorModeFrom, } from './constants'; import { SerializationError, SigningError } from './errors'; -import { privateKeyIsCompressed, publicKeyIsCompressed } from './keys'; +import { createStacksPublicKey, privateKeyIsCompressed, publicKeyIsCompressed } from './keys'; import { cloneDeep, txidFromData } from './utils'; import { LengthPrefixedList, @@ -58,6 +58,7 @@ import { deserializePayloadBytes, serializeLPListBytes, } from './wire'; +import { PublicKey } from '@stacks/common'; export class StacksTransaction { version: TransactionVersion; @@ -136,14 +137,33 @@ export class StacksTransaction { } } - appendPubkey(publicKey: PublicKeyWire) { + /** + * Append a public key to the spending-condition of the transaction + * + * @param publicKey - the public key to append + * @example + * ```ts + * import { makeSTXTokenTransfer } from '@stacks/transactions'; + * + * const transaction = makeSTXTokenTransfer({ ... }); + * transaction.appendPubkey('034f355bdcb7cc0af728..24c0e585c5e89ac788521e0'); + * ``` + */ + appendPubkey(publicKey: PublicKey): void; + appendPubkey(publicKey: PublicKeyWire): void; + appendPubkey(publicKey: PublicKey | PublicKeyWire): void { + const wire = + typeof publicKey === 'object' && 'type' in publicKey + ? publicKey + : createStacksPublicKey(publicKey); + const cond = this.auth.spendingCondition; if (cond && !isSingleSig(cond)) { - const compressed = publicKeyIsCompressed(publicKey.data); + const compressed = publicKeyIsCompressed(wire.data); cond.fields.push( createTransactionAuthField( compressed ? PubKeyEncoding.Compressed : PubKeyEncoding.Uncompressed, - publicKey + wire ) ); } else {