Skip to content

Commit

Permalink
refactor: Remove Serialize prefix from enum keys
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Sep 17, 2024
1 parent 2f2c2a8 commit 088c326
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 131 deletions.
6 changes: 6 additions & 0 deletions .github/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Triplesec](#triplesec)
- [Advanced: WireType](#advanced-wiretype)
- [Advanced: Signed BigInt](#advanced-signed-bigint)
- [Advanced: Refactorings](#advanced-refactorings)
- [Stacks.js (\<=4.x.x) → (5.x.x)](#stacksjs-4xx--5xx)
- [Breaking Changes](#breaking-changes-1)
- [Buffer to Uint8Array](#buffer-to-uint8array)
Expand Down Expand Up @@ -43,6 +44,7 @@
- Disable legacy `triplesec` mnemonic encryption support. [Read more...](#triplesec)
- **Advanced:** Rename `MessageType` and related concepts to `WireType`. [Read more...](#advanced-wiretype)
- **Advanced:** Removes two's complement compatibilty from `intToBigInt` parser method. [Read more...](#advanced-signed-bigint)
- **Advanced:** Refactorings and less visible updates. [Read more...](#advanced-refactorings)

### Stacks Network

Expand Down Expand Up @@ -306,6 +308,10 @@ More types were renamed to indicate use for serialization to _wire-format_:
The `intToBigInt` method no longer supports two's complement signed integers and removed the `signed` boolean parameter.
This likely was a misunderstood and unused feature.

### Advanced: Refactorings

- `AddressHashMode`: The `Serialize` prefixes were removed for brevity.

## Stacks.js (&lt;=4.x.x) → (5.x.x)

### Breaking Changes
Expand Down
12 changes: 6 additions & 6 deletions packages/transactions/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function addressHashModeToVersion(
): AddressVersion {
network = networkFrom(network ?? STACKS_MAINNET);
switch (hashMode) {
case AddressHashMode.SerializeP2PKH:
case AddressHashMode.P2PKH:
switch (network.transactionVersion) {
case TransactionVersion.Mainnet:
return AddressVersion.MainnetSingleSig;
Expand All @@ -28,11 +28,11 @@ export function addressHashModeToVersion(
`Unexpected transactionVersion ${network.transactionVersion} for hashMode ${hashMode}`
);
}
case AddressHashMode.SerializeP2SH:
case AddressHashMode.SerializeP2SHNonSequential:
case AddressHashMode.SerializeP2WPKH:
case AddressHashMode.SerializeP2WSH:
case AddressHashMode.SerializeP2WSHNonSequential:
case AddressHashMode.P2SH:
case AddressHashMode.P2SHNonSequential:
case AddressHashMode.P2WPKH:
case AddressHashMode.P2WSH:
case AddressHashMode.P2WSHNonSequential:
switch (network.transactionVersion) {
case TransactionVersion.Mainnet:
return AddressVersion.MainnetMultiSig;
Expand Down
30 changes: 12 additions & 18 deletions packages/transactions/src/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ export function createSpendingCondition(
) {
if ('publicKey' in options) {
return createSingleSigSpendingCondition(
AddressHashMode.SerializeP2PKH,
AddressHashMode.P2PKH,
options.publicKey,
options.nonce,
options.fee
);
}
// multi-sig
return createMultiSigSpendingCondition(
AddressHashMode.SerializeP2SH,
AddressHashMode.P2SH,
options.numSignatures,
options.publicKeys,
options.nonce,
Expand Down Expand Up @@ -178,14 +178,14 @@ export function isSingleSig(

/** @internal */
export function isSequentialMultiSig(hashMode: AddressHashMode): boolean {
return hashMode === AddressHashMode.SerializeP2SH || hashMode === AddressHashMode.SerializeP2WSH;
return hashMode === AddressHashMode.P2SH || hashMode === AddressHashMode.P2WSH;
}

/** @internal */
export function isNonSequentialMultiSig(hashMode: AddressHashMode): boolean {
return (
hashMode === AddressHashMode.SerializeP2SHNonSequential ||
hashMode === AddressHashMode.SerializeP2WSHNonSequential
hashMode === AddressHashMode.P2SHNonSequential ||
hashMode === AddressHashMode.P2WSHNonSequential
);
}

Expand Down Expand Up @@ -252,7 +252,7 @@ export function deserializeSingleSigSpendingCondition(
const keyEncoding = bytesReader.readUInt8Enum(PubKeyEncoding, n => {
throw new DeserializationError(`Could not parse ${n} as PubKeyEncoding`);
});
if (hashMode === AddressHashMode.SerializeP2WPKH && keyEncoding != PubKeyEncoding.Compressed) {
if (hashMode === AddressHashMode.P2WPKH && keyEncoding != PubKeyEncoding.Compressed) {
throw new DeserializationError(
'Failed to parse singlesig spending condition: incomaptible hash mode and key encoding'
);
Expand Down Expand Up @@ -304,8 +304,7 @@ export function deserializeMultiSigSpendingCondition(

if (
haveUncompressed &&
(hashMode === AddressHashMode.SerializeP2WSH ||
hashMode === AddressHashMode.SerializeP2WSHNonSequential)
(hashMode === AddressHashMode.P2WSH || hashMode === AddressHashMode.P2WSHNonSequential)
) {
throw new VerificationError('Uncompressed keys are not allowed in this hash mode');
}
Expand All @@ -332,7 +331,7 @@ export function deserializeSpendingCondition(bytesReader: BytesReader): Spending
throw new DeserializationError(`Could not parse ${n} as AddressHashMode`);
});

if (hashMode === AddressHashMode.SerializeP2PKH || hashMode === AddressHashMode.SerializeP2WPKH) {
if (hashMode === AddressHashMode.P2PKH || hashMode === AddressHashMode.P2WPKH) {
return deserializeSingleSigSpendingCondition(hashMode, bytesReader);
} else {
return deserializeMultiSigSpendingCondition(hashMode, bytesReader);
Expand Down Expand Up @@ -436,12 +435,7 @@ export function nextVerification(
}

function newInitialSigHash(): SpendingCondition {
const spendingCondition = createSingleSigSpendingCondition(
AddressHashMode.SerializeP2PKH,
'',
0,
0
);
const spendingCondition = createSingleSigSpendingCondition(AddressHashMode.P2PKH, '', 0, 0);
spendingCondition.signer = createEmptyAddress().hash160;
spendingCondition.keyEncoding = PubKeyEncoding.Compressed;
spendingCondition.signature = emptyMessageSignature();
Expand Down Expand Up @@ -537,8 +531,8 @@ function verifyMultiSig(

if (
haveUncompressed &&
(condition.hashMode === AddressHashMode.SerializeP2WSH ||
condition.hashMode === AddressHashMode.SerializeP2WSHNonSequential)
(condition.hashMode === AddressHashMode.P2WSH ||
condition.hashMode === AddressHashMode.P2WSHNonSequential)
)
throw new VerificationError('Uncompressed keys are not allowed in this hash mode');

Expand Down Expand Up @@ -585,7 +579,7 @@ export function createSponsoredAuth(
spendingCondition,
sponsorSpendingCondition: sponsorSpendingCondition
? sponsorSpendingCondition
: createSingleSigSpendingCondition(AddressHashMode.SerializeP2PKH, '0'.repeat(66), 0, 0),
: createSingleSigSpendingCondition(AddressHashMode.P2PKH, '0'.repeat(66), 0, 0),
};
}

Expand Down
20 changes: 10 additions & 10 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ export async function makeUnsignedSTXTokenTransfer(
if ('publicKey' in options) {
// single-sig
spendingCondition = createSingleSigSpendingCondition(
AddressHashMode.SerializeP2PKH,
AddressHashMode.P2PKH,
options.publicKey,
options.nonce,
options.fee
);
} else {
// multi-sig
const hashMode = options.useNonSequentialMultiSig
? AddressHashMode.SerializeP2SHNonSequential
: AddressHashMode.SerializeP2SH;
? AddressHashMode.P2SHNonSequential
: AddressHashMode.P2SH;

const publicKeys = options.address
? sortPublicKeysForAddress(
Expand Down Expand Up @@ -344,16 +344,16 @@ export async function makeUnsignedContractDeploy(
if ('publicKey' in options) {
// single-sig
spendingCondition = createSingleSigSpendingCondition(
AddressHashMode.SerializeP2PKH,
AddressHashMode.P2PKH,
options.publicKey,
options.nonce,
options.fee
);
} else {
// multi-sig
const hashMode = options.useNonSequentialMultiSig
? AddressHashMode.SerializeP2SHNonSequential
: AddressHashMode.SerializeP2SH;
? AddressHashMode.P2SHNonSequential
: AddressHashMode.P2SH;

const publicKeys = options.address
? sortPublicKeysForAddress(
Expand Down Expand Up @@ -501,16 +501,16 @@ export async function makeUnsignedContractCall(
if ('publicKey' in options) {
// single-sig
spendingCondition = createSingleSigSpendingCondition(
AddressHashMode.SerializeP2PKH,
AddressHashMode.P2PKH,
options.publicKey,
options.nonce,
options.fee
);
} else {
// multi-sig
const hashMode = options.useNonSequentialMultiSig
? AddressHashMode.SerializeP2SHNonSequential
: AddressHashMode.SerializeP2SH;
? AddressHashMode.P2SHNonSequential
: AddressHashMode.P2SH;

const publicKeys = options.address
? sortPublicKeysForAddress(
Expand Down Expand Up @@ -643,7 +643,7 @@ export async function sponsorTransaction(
const defaultOptions = {
fee: 0 as IntegerType,
sponsorNonce: 0 as IntegerType,
sponsorAddressHashmode: AddressHashMode.SerializeP2PKH as SingleSigHashMode,
sponsorAddressHashmode: AddressHashMode.P2PKH as SingleSigHashMode,
network: defaultNetwork,
};

Expand Down
25 changes: 12 additions & 13 deletions packages/transactions/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,27 @@ export enum AuthType {
*/
export enum AddressHashMode {
/** `SingleSigHashMode` — hash160(public-key), same as bitcoin's p2pkh */
SerializeP2PKH = 0x00,
P2PKH = 0x00,
/** Legacy `MultiSigHashMode` — hash160(multisig-redeem-script), same as bitcoin's multisig p2sh */
SerializeP2SH = 0x01,
P2SH = 0x01,
/** `SingleSigHashMode` — hash160(segwit-program-00(p2pkh)), same as bitcoin's p2sh-p2wpkh */
SerializeP2WPKH = 0x02,
P2WPKH = 0x02,
/** Legacy `MultiSigHashMode` — hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh */
SerializeP2WSH = 0x03,
P2WSH = 0x03,
/** Non-Sequential `MultiSigHashMode` — hash160(multisig-redeem-script), same as bitcoin's multisig p2sh */
SerializeP2SHNonSequential = 0x05,
P2SHNonSequential = 0x05,
/** Non-Sequential `MultiSigHashMode` — hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh */
SerializeP2WSHNonSequential = 0x07,
P2WSHNonSequential = 0x07,

// todo: `next` rename to remove the `Serialize` prefix?
// todo: `next` rename to remove `NonSequential` and add `Legacy` to sequential mutlisig
// todo: once live, rename to remove `NonSequential` and add `Legacy` to sequential mutlisig
}

export type SingleSigHashMode = AddressHashMode.SerializeP2PKH | AddressHashMode.SerializeP2WPKH;
export type SingleSigHashMode = AddressHashMode.P2PKH | AddressHashMode.P2WPKH;
export type MultiSigHashMode =
| AddressHashMode.SerializeP2SH
| AddressHashMode.SerializeP2WSH
| AddressHashMode.SerializeP2SHNonSequential
| AddressHashMode.SerializeP2WSHNonSequential;
| AddressHashMode.P2SH
| AddressHashMode.P2WSH
| AddressHashMode.P2SHNonSequential
| AddressHashMode.P2WSHNonSequential;

// re-export for backwards compatibility
export { AddressVersion } from '@stacks/network';
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function getAddressFromPublicKey(
): string {
network = networkFrom(network ?? STACKS_MAINNET);
publicKey = typeof publicKey === 'string' ? hexToBytes(publicKey) : publicKey;
const addrVer = addressHashModeToVersion(AddressHashMode.SerializeP2PKH, network);
const addrVer = addressHashModeToVersion(AddressHashMode.P2PKH, network);
const addr = addressFromVersionHash(addrVer, hashP2PKH(publicKey));
const addrString = addressToString(addr);
return addrString;
Expand Down
4 changes: 2 additions & 2 deletions packages/transactions/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export class TransactionSigner {

const spendingCondition = this.transaction.auth.spendingCondition;
if (
spendingCondition.hashMode === AddressHashMode.SerializeP2SH ||
spendingCondition.hashMode === AddressHashMode.SerializeP2WSH
spendingCondition.hashMode === AddressHashMode.P2SH ||
spendingCondition.hashMode === AddressHashMode.P2WSH
) {
// only check oversign on legacy multisig modes
if (
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export function deriveNetworkFromTx(transaction: StacksTransaction) {
export function estimateTransactionByteLength(transaction: StacksTransaction): number {
const hashMode = transaction.auth.spendingCondition.hashMode;
// List of Multi-sig transaction hash modes
const multiSigHashModes = [AddressHashMode.SerializeP2SH, AddressHashMode.SerializeP2WSH];
const multiSigHashModes = [AddressHashMode.P2SH, AddressHashMode.P2WSH];

// Check if its a Multi-sig transaction
if (multiSigHashModes.includes(hashMode)) {
Expand Down
20 changes: 10 additions & 10 deletions packages/transactions/src/wire/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,35 @@ export function addressFromPublicKeys(
throw Error('Invalid number of public keys');
}

if (hashMode === AddressHashMode.SerializeP2PKH || hashMode === AddressHashMode.SerializeP2WPKH) {
if (hashMode === AddressHashMode.P2PKH || hashMode === AddressHashMode.P2WPKH) {
if (publicKeys.length !== 1 || numSigs !== 1) {
throw Error('Invalid number of public keys or signatures');
}
}

if (
hashMode === AddressHashMode.SerializeP2WPKH ||
hashMode === AddressHashMode.SerializeP2WSH ||
hashMode === AddressHashMode.SerializeP2WSHNonSequential
hashMode === AddressHashMode.P2WPKH ||
hashMode === AddressHashMode.P2WSH ||
hashMode === AddressHashMode.P2WSHNonSequential
) {
if (!publicKeys.map(p => p.data).every(publicKeyIsCompressed)) {
throw Error('Public keys must be compressed for segwit');
}
}

switch (hashMode) {
case AddressHashMode.SerializeP2PKH:
case AddressHashMode.P2PKH:
return addressFromVersionHash(version, hashP2PKH(publicKeys[0].data));
case AddressHashMode.SerializeP2WPKH:
case AddressHashMode.P2WPKH:
return addressFromVersionHash(version, hashP2WPKH(publicKeys[0].data));
case AddressHashMode.SerializeP2SH:
case AddressHashMode.SerializeP2SHNonSequential:
case AddressHashMode.P2SH:
case AddressHashMode.P2SHNonSequential:
return addressFromVersionHash(
version,
hashP2SH(numSigs, publicKeys.map(serializePublicKeyBytes))
);
case AddressHashMode.SerializeP2WSH:
case AddressHashMode.SerializeP2WSHNonSequential:
case AddressHashMode.P2WSH:
case AddressHashMode.P2WSHNonSequential:
return addressFromVersionHash(
version,
hashP2WSH(numSigs, publicKeys.map(serializePublicKeyBytes))
Expand Down
Loading

0 comments on commit 088c326

Please sign in to comment.