Skip to content

Commit

Permalink
refactor: Remove unneeded deserialize overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 25, 2024
1 parent fa9be48 commit 74cb952
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 153 deletions.
12 changes: 0 additions & 12 deletions .github/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,16 @@ For easier migrating, renaming the following methods is possible to keep the pre
- `StacksTransaction.serialize``StacksTransaction.serializeBytes`
- `serializeCV``serializeCVBytes`
- `serializeAddress``serializeAddressBytes`
- `deserializeAddress``deserializeAddressBytes`
- `serializeLPList``serializeLPListBytes`
- `deserializeLPList``deserializeLPListBytes`
- `serializeLPString``serializeLPStringBytes`
- `deserializeLPString``deserializeLPStringBytes`
- `serializePayload``serializePayloadBytes`
- `deserializePayload``deserializePayloadBytes`
- `serializePublicKey``serializePublicKeyBytes`
- `deserializePublicKey``deserializePublicKeyBytes`
- `serializeStacksMessage``serializeStacksMessageBytes`
- `deserializeStacksMessage``deserializeStacksMessageBytes`
- `serializeMemoString``serializeMemoStringBytes`
- `deserializeMemoString``deserializeMemoStringBytes`
- `serializeTransactionAuthField``serializeTransactionAuthFieldBytes`
- `deserializeTransactionAuthField``deserializeTransactionAuthFieldBytes`
- `serializeMessageSignature``serializeMessageSignatureBytes`
- `deserializeMessageSignature``deserializeMessageSignatureBytes`
- `serializePostCondition``serializePostConditionBytes`
- `deserializePostCondition``deserializePostConditionBytes`
- `serializeStacksMessage``serializeStacksWireBytes`
- `deserializeStacksMessage``deserializeStacksWireBytes`

### Asset Helper Methods

Expand Down Expand Up @@ -287,7 +276,6 @@ This is only used for advanced serialization use-cases internally and should not
- `StacksMessage``StacksWire`
- `StacksMessageType``StacksWireType`
- `serializeStacksMessage``serializeStacksWireBytes`
- `deserializeStacksMessage``deserializeStacksWireBytes`

More types were renamed to indicate use for serialization to _wire-format_:

Expand Down
6 changes: 3 additions & 3 deletions packages/transactions/src/BytesReader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bytesToHex, readUInt16BE, readUInt32BE, readUInt8 } from '@stacks/common';
import { bytesToHex, hexToBytes, readUInt16BE, readUInt32BE, readUInt8 } from '@stacks/common';

function createEnumChecker<T extends string, TEnumValue extends number>(enumVariable: {
[key in T]: TEnumValue;
Expand Down Expand Up @@ -47,8 +47,8 @@ export class BytesReader {
source: Uint8Array;
consumed: number = 0;

constructor(arr: Uint8Array) {
this.source = arr;
constructor(bytes: string | Uint8Array) {
this.source = typeof bytes === 'string' ? hexToBytes(bytes) : bytes;
}

readBytes(length: number): Uint8Array {
Expand Down
8 changes: 4 additions & 4 deletions packages/transactions/src/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import {
addressFromPublicKeys,
createEmptyAddress,
createLPList,
deserializeLPListBytes,
deserializeMessageSignatureBytes,
deserializeLPList,
deserializeMessageSignature,
MessageSignatureWire,
PublicKeyWire,
serializeLPListBytes,
Expand Down Expand Up @@ -258,7 +258,7 @@ export function deserializeSingleSigSpendingCondition(
'Failed to parse singlesig spending condition: incomaptible hash mode and key encoding'
);
}
const signature = deserializeMessageSignatureBytes(bytesReader);
const signature = deserializeMessageSignature(bytesReader);
return {
hashMode,
signer,
Expand All @@ -277,7 +277,7 @@ export function deserializeMultiSigSpendingCondition(
const nonce = BigInt('0x' + bytesToHex(bytesReader.readBytes(8)));
const fee = BigInt('0x' + bytesToHex(bytesReader.readBytes(8)));

const fields = deserializeLPListBytes(bytesReader, StacksWireType.TransactionAuthField)
const fields = deserializeLPList(bytesReader, StacksWireType.TransactionAuthField)
.values as TransactionAuthFieldWire[];

let haveUncompressed = false;
Expand Down
10 changes: 5 additions & 5 deletions packages/transactions/src/clarity/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '.';
import { BytesReader } from '../BytesReader';
import { DeserializationError } from '../errors';
import { deserializeAddressBytes, deserializeLPStringBytes } from '../wire';
import { deserializeAddress, deserializeLPString } from '../wire';

/**
* Deserializes clarity value to clarity type
Expand Down Expand Up @@ -75,12 +75,12 @@ export function deserializeCV<T extends ClarityValue = ClarityValue>(
return falseCV() as T;

case ClarityWireType.address:
const sAddress = deserializeAddressBytes(bytesReader);
const sAddress = deserializeAddress(bytesReader);
return standardPrincipalCVFromAddress(sAddress) as T;

case ClarityWireType.contract:
const cAddress = deserializeAddressBytes(bytesReader);
const contractName = deserializeLPStringBytes(bytesReader);
const cAddress = deserializeAddress(bytesReader);
const contractName = deserializeLPString(bytesReader);
return contractPrincipalCVFromAddress(cAddress, contractName) as T;

case ClarityWireType.ok:
Expand All @@ -107,7 +107,7 @@ export function deserializeCV<T extends ClarityValue = ClarityValue>(
const tupleLength = bytesReader.readUInt32BE();
const tupleContents: { [key: string]: ClarityValue } = {};
for (let i = 0; i < tupleLength; i++) {
const clarityName = deserializeLPStringBytes(bytesReader).content;
const clarityName = deserializeLPString(bytesReader).content;
if (clarityName === undefined) {
throw new DeserializationError('"content" is undefined');
}
Expand Down
23 changes: 6 additions & 17 deletions packages/transactions/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
PublicKey,
bytesToHex,
concatArray,
hexToBytes,
intToBigInt,
isInstance,
writeUInt32BE,
} from '@stacks/common';
import {
Expand Down Expand Up @@ -56,8 +56,8 @@ import {
createLPList,
createMessageSignature,
createTransactionAuthField,
deserializeLPListBytes,
deserializePayloadBytes,
deserializeLPList,
deserializePayload,
serializeLPListBytes,
} from './wire';

Expand Down Expand Up @@ -312,18 +312,7 @@ export class StacksTransaction {
* @param tx hex string or bytes of serialized transaction
*/
export function deserializeTransaction(tx: string | Uint8Array | BytesReader) {
let bytesReader: BytesReader; // todo: add readerFrom method
if (typeof tx === 'string') {
if (tx.slice(0, 2).toLowerCase() === '0x') {
bytesReader = new BytesReader(hexToBytes(tx.slice(2)));
} else {
bytesReader = new BytesReader(hexToBytes(tx));
}
} else if (tx instanceof Uint8Array) {
bytesReader = new BytesReader(tx);
} else {
bytesReader = tx;
}
const bytesReader = isInstance(tx, BytesReader) ? tx : new BytesReader(tx);
const version = bytesReader.readUInt8Enum(TransactionVersion, n => {
throw new Error(`Could not parse ${n} as TransactionVersion`);
});
Expand All @@ -335,8 +324,8 @@ export function deserializeTransaction(tx: string | Uint8Array | BytesReader) {
const postConditionMode = bytesReader.readUInt8Enum(PostConditionMode, n => {
throw new Error(`Could not parse ${n} as PostConditionMode`);
});
const postConditions = deserializeLPListBytes(bytesReader, StacksWireType.PostCondition);
const payload = deserializePayloadBytes(bytesReader);
const postConditions = deserializeLPList(bytesReader, StacksWireType.PostCondition);
const payload = deserializePayload(bytesReader);

return new StacksTransaction(
version,
Expand Down
Loading

0 comments on commit 74cb952

Please sign in to comment.