Skip to content

Commit

Permalink
refactor: decrease circular deps in tx package
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Apr 30, 2024
1 parent 8f21ec4 commit 2fcb21e
Show file tree
Hide file tree
Showing 31 changed files with 194 additions and 219 deletions.
5 changes: 4 additions & 1 deletion packages/common/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { hexToBytes } from './utils';

export type PrivateKey = string | Uint8Array;
export type PublicKey = string | Uint8Array;

/**
* @private
* @ignore
*/
export function privateKeyToBytes(privateKey: string | Uint8Array): Uint8Array {
export function privateKeyToBytes(privateKey: PrivateKey): Uint8Array {
const privateKeyBuffer = typeof privateKey === 'string' ? hexToBytes(privateKey) : privateKey;

if (privateKeyBuffer.length != 32 && privateKeyBuffer.length != 33) {
Expand Down
2 changes: 1 addition & 1 deletion packages/encryption/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { sha256 } from '@noble/hashes/sha256';
import { getPublicKey as nobleGetPublicKey, signSync, utils } from '@noble/secp256k1';
import {
PRIVATE_KEY_COMPRESSED_LENGTH,
PrivateKey,
bytesToHex,
concatBytes,
hexToBytes,
privateKeyToBytes,
readUInt8,
} from '@stacks/common';
import base58 from 'bs58';
import { PrivateKey } from '../../transactions/src';
import { hashRipemd160 } from './hashRipemd160';
import { hashSha256Sync } from './sha2Hash';

Expand Down
10 changes: 8 additions & 2 deletions packages/stacking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import {
StacksNodeApi,
V2PoxInfoResponse,
} from '@stacks/api';
import { ApiOpts, IntegerType, hexToBytes, intToBigInt, isInstance } from '@stacks/common';
import {
ApiOpts,
IntegerType,
PrivateKey,
hexToBytes,
intToBigInt,
isInstance,
} from '@stacks/common';
import { StacksNetwork } from '@stacks/network';
import {
BurnchainRewardListResponse,
Expand All @@ -21,7 +28,6 @@ import {
ContractCallPayload,
OptionalCV,
PrincipalCV,
PrivateKey,
ResponseErrorCV,
StacksTransaction,
TupleCV,
Expand Down
3 changes: 1 addition & 2 deletions packages/stacking/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sha256 } from '@noble/hashes/sha256';
import { bech32, bech32m } from '@scure/base';
import { IntegerType, bigIntToBytes } from '@stacks/common';
import { IntegerType, PrivateKey, bigIntToBytes } from '@stacks/common';
import {
base58CheckDecode,
base58CheckEncode,
Expand All @@ -12,7 +12,6 @@ import {
ClarityType,
ClarityValue,
OptionalCV,
PrivateKey,
TupleCV,
bufferCV,
encodeStructuredData,
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IntegerType,
intToBigInt,
intToBytes,
PrivateKey,
writeUInt16BE,
} from '@stacks/common';
import {
Expand All @@ -22,7 +23,6 @@ import { MessageSignature } from './common';
import { DeserializationError, SigningError, VerificationError } from './errors';
import {
createStacksPublicKey,
PrivateKey,
privateKeyToPublic,
publicKeyFromSignatureVrs,
publicKeyIsCompressed,
Expand Down
1 change: 0 additions & 1 deletion packages/transactions/src/cl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
tupleCV,
uintCV,
} from './clarity';

export { prettyPrint } from './clarity/prettyPrint';

// todo: https://github.com/hirosystems/clarinet/issues/786
Expand Down
3 changes: 1 addition & 2 deletions packages/transactions/src/clarity/clarityValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
SomeCV,
TrueCV,
FalseCV,
principalToString,
} from '.';

import { principalToString } from './types/principalCV';
import { ClarityType } from './constants';
import { asciiToBytes, bytesToAscii, bytesToHex, utf8ToBytes } from '@stacks/common';

Expand Down
5 changes: 3 additions & 2 deletions packages/transactions/src/clarity/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import {
responseErrorCV,
noneCV,
someCV,
listCV,
stringAsciiCV,
stringUtf8CV,
tupleCV,
listCV,
} from '.';
import { BytesReader as BytesReader } from '../bytesReader';
import { deserializeAddressBytes, deserializeLPStringBytes } from '../types';
import { DeserializationError } from '../errors';
import { stringAsciiCV, stringUtf8CV } from './types/stringCV';
import { bytesToAscii, bytesToUtf8, hexToBytes } from '@stacks/common';

/**
Expand Down
49 changes: 13 additions & 36 deletions packages/transactions/src/clarity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,25 @@

export {
ClarityValue,
getCVTypeString,
cvToString,
cvToJSON,
cvToString,
cvToValue,
getCVTypeString,
isClarityType,
} from './clarityValue';
export * from './constants';
export { BooleanCV, TrueCV, FalseCV, trueCV, falseCV, boolCV } from './types/booleanCV';
export { IntCV, UIntCV, intCV, uintCV } from './types/intCV';
export { BufferCV, bufferCV, bufferCVFromString } from './types/bufferCV';
export { OptionalCV, NoneCV, SomeCV, noneCV, someCV, optionalCVOf } from './types/optionalCV';

export {
ResponseCV,
ResponseOkCV,
ResponseErrorCV,
responseOkCV,
responseErrorCV,
} from './types/responseCV';

export {
StandardPrincipalCV,
ContractPrincipalCV,
standardPrincipalCV,
contractPrincipalCV,
standardPrincipalCVFromAddress,
contractPrincipalCVFromAddress,
PrincipalCV,
contractPrincipalCVFromStandard,
principalCV,
principalToString,
} from './types/principalCV';
export * from './values/booleanCV';
export * from './values/bufferCV';
export * from './values/intCV';
export * from './values/listCV';
export * from './values/optionalCV';
export * from './values/principalCV';
export * from './values/responseCV';
export * from './values/stringCV';
export * from './values/tupleCV';

export { ListCV, listCV } from './types/listCV';
export { TupleCV, tupleCV } from './types/tupleCV';
export {
StringAsciiCV,
StringUtf8CV,
stringUtf8CV,
stringAsciiCV,
stringCV,
} from './types/stringCV';
export * from './types';

export * from './serialize';
export * from './deserialize';
export * from './serialize';
5 changes: 1 addition & 4 deletions packages/transactions/src/clarity/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ import {
StandardPrincipalCV,
ContractPrincipalCV,
ResponseCV,
ListCV,
TupleCV,
ClarityValue,
} from '.';
import { ClarityType, clarityTypeToByte } from './constants';

import { SerializationError } from '../errors';
import { StringAsciiCV, StringUtf8CV } from './types/stringCV';
import { CLARITY_INT_BYTE_SIZE, CLARITY_INT_SIZE } from '../constants';
import { ListCV, StringAsciiCV, StringUtf8CV, TupleCV } from './types';

function bytesWithTypeID(typeId: ClarityType, bytes: Uint8Array): Uint8Array {
return concatArray([clarityTypeToByte(typeId), bytes]);
Expand Down
87 changes: 87 additions & 0 deletions packages/transactions/src/clarity/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Address } from '../common';
import { LengthPrefixedString } from '../postcondition-types';
import { ClarityValue } from './clarityValue';
import { ClarityType } from './constants';

export type BooleanCV = TrueCV | FalseCV;

export interface TrueCV {
type: ClarityType.BoolTrue;
}

export interface FalseCV {
type: ClarityType.BoolFalse;
}

export interface BufferCV {
readonly type: ClarityType.Buffer;
readonly buffer: Uint8Array;
}

export interface IntCV {
readonly type: ClarityType.Int;
readonly value: bigint;
}

export interface UIntCV {
readonly type: ClarityType.UInt;
readonly value: bigint;
}

export interface ListCV<T extends ClarityValue = ClarityValue> {
type: ClarityType.List;
list: T[];
}

export type OptionalCV<T extends ClarityValue = ClarityValue> = NoneCV | SomeCV<T>;

export interface NoneCV {
readonly type: ClarityType.OptionalNone;
}

export interface SomeCV<T extends ClarityValue = ClarityValue> {
readonly type: ClarityType.OptionalSome;
readonly value: T;
}

export type PrincipalCV = StandardPrincipalCV | ContractPrincipalCV;

export interface StandardPrincipalCV {
readonly type: ClarityType.PrincipalStandard;
readonly address: Address;
}

export interface ContractPrincipalCV {
readonly type: ClarityType.PrincipalContract;
readonly address: Address;
readonly contractName: LengthPrefixedString;
}

export type ResponseCV = ResponseErrorCV | ResponseOkCV;

export interface ResponseErrorCV<T extends ClarityValue = ClarityValue> {
readonly type: ClarityType.ResponseErr;
readonly value: T;
}

export interface ResponseOkCV<T extends ClarityValue = ClarityValue> {
readonly type: ClarityType.ResponseOk;
readonly value: T;
}

export interface StringAsciiCV {
readonly type: ClarityType.StringASCII;
readonly data: string;
}

export interface StringUtf8CV {
readonly type: ClarityType.StringUTF8;
readonly data: string;
}

export type TupleData<T extends ClarityValue = ClarityValue> = { [key: string]: T };

export interface TupleCV<T extends TupleData = TupleData> {
type: ClarityType.Tuple;
data: T;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { ClarityType } from '../constants';

type BooleanCV = TrueCV | FalseCV;

interface TrueCV {
type: ClarityType.BoolTrue;
}

interface FalseCV {
type: ClarityType.BoolFalse;
}
import { BooleanCV } from '../types';

/**
* Converts true to BooleanCV clarity type
Expand All @@ -26,7 +17,7 @@ interface FalseCV {
* @see
* {@link https://github.com/hirosystems/stacks.js/blob/main/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
*/
const trueCV = (): BooleanCV => ({ type: ClarityType.BoolTrue });
export const trueCV = (): BooleanCV => ({ type: ClarityType.BoolTrue });

/**
* Converts false to BooleanCV clarity type
Expand All @@ -44,7 +35,7 @@ const trueCV = (): BooleanCV => ({ type: ClarityType.BoolTrue });
* @see
* {@link https://github.com/hirosystems/stacks.js/blob/main/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
*/
const falseCV = (): BooleanCV => ({ type: ClarityType.BoolFalse });
export const falseCV = (): BooleanCV => ({ type: ClarityType.BoolFalse });

/**
* Converts a boolean to BooleanCV clarity type
Expand All @@ -62,6 +53,4 @@ const falseCV = (): BooleanCV => ({ type: ClarityType.BoolFalse });
* @see
* {@link https://github.com/hirosystems/stacks.js/blob/main/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
*/
const boolCV = (bool: boolean) => (bool ? trueCV() : falseCV());

export { BooleanCV, TrueCV, FalseCV, boolCV, trueCV, falseCV };
export const boolCV = (bool: boolean) => (bool ? trueCV() : falseCV());
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { utf8ToBytes } from '@stacks/common';
import { ClarityType } from '../constants';

interface BufferCV {
readonly type: ClarityType.Buffer;
readonly buffer: Uint8Array;
}
import { BufferCV } from '../types';

/**
* Converts a Uint8Array to a BufferCV clarity type
Expand All @@ -27,7 +23,7 @@ interface BufferCV {
* @see
* {@link https://github.com/hirosystems/stacks.js/blob/main/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
*/
const bufferCV = (buffer: Uint8Array): BufferCV => {
export const bufferCV = (buffer: Uint8Array): BufferCV => {
// max size 1024 * 1024 = 1MB; https://github.com/stacks-network/stacks-core/blob/c50a93088d7c0261f1dbe31ab24b95028a038447/clarity/src/vm/types/mod.rs#L47
if (buffer.byteLength > 1_048_576) {
throw new Error('Cannot construct clarity buffer that is greater than 1MB');
Expand Down Expand Up @@ -57,6 +53,4 @@ const bufferCV = (buffer: Uint8Array): BufferCV => {
* @see
* {@link https://github.com/hirosystems/stacks.js/blob/main/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
*/
const bufferCVFromString = (str: string): BufferCV => bufferCV(utf8ToBytes(str));

export { BufferCV, bufferCV, bufferCVFromString };
export const bufferCVFromString = (str: string): BufferCV => bufferCV(utf8ToBytes(str));
Loading

0 comments on commit 2fcb21e

Please sign in to comment.