Skip to content

Commit

Permalink
feat: Reduce wrapping of types for public key appending
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Sep 24, 2024
1 parent 413f206 commit 2c9bd6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
3 changes: 1 addition & 2 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
28 changes: 24 additions & 4 deletions packages/transactions/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -58,6 +58,7 @@ import {
deserializePayloadBytes,
serializeLPListBytes,
} from './wire';
import { PublicKey } from '@stacks/common';

export class StacksTransaction {
version: TransactionVersion;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2c9bd6d

Please sign in to comment.