mirror of
https://github.com/alexgo-io/stacks.js.git
synced 2026-01-12 17:52:41 +08:00
fix: add Cl helper (#1479)
Adds a Cl bundled export that allows better discovery of clarity methods and less importing. * fix: add cl helper * docs: update tsdocs * test: add cl tests * docs: fix typo Co-authored-by: Hugo C <911307+hugocaillard@users.noreply.github.com> * docs: swap out @visit for @see * fix: remove true and false bool methods --------- Co-authored-by: janniks <janniks@users.noreply.github.com> Co-authored-by: Hugo C <911307+hugocaillard@users.noreply.github.com>
This commit is contained in:
@@ -614,12 +614,9 @@ export function concatArray(elements: (Uint8Array | number[] | number)[]) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Better `instanceof` check for ArrayBuffer types in different environments
|
||||
* Better `instanceof` check for types in different environments
|
||||
* @ignore
|
||||
*/
|
||||
export function isInstance(object: any, type: any) {
|
||||
return (
|
||||
object instanceof type ||
|
||||
(object?.constructor?.name != null && object.constructor.name === type.name)
|
||||
);
|
||||
return object instanceof type || object?.constructor?.name?.toLowerCase() === type.name;
|
||||
}
|
||||
|
||||
163
packages/transactions/src/cl.ts
Normal file
163
packages/transactions/src/cl.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import { asciiToBytes, hexToBytes, utf8ToBytes } from '@stacks/common';
|
||||
import {
|
||||
BufferCV,
|
||||
boolCV,
|
||||
bufferCV,
|
||||
contractPrincipalCV,
|
||||
deserializeCV,
|
||||
intCV,
|
||||
listCV,
|
||||
noneCV,
|
||||
responseErrorCV,
|
||||
responseOkCV,
|
||||
serializeCV,
|
||||
someCV,
|
||||
standardPrincipalCV,
|
||||
stringAsciiCV,
|
||||
stringUtf8CV,
|
||||
tupleCV,
|
||||
uintCV,
|
||||
} from './clarity';
|
||||
|
||||
// todo: https://github.com/hirosystems/clarinet/issues/786
|
||||
|
||||
// Primitives //////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* `Cl.bool` — Creates a Clarity boolean type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link boolCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const bool = boolCV;
|
||||
/**
|
||||
* `Cl.int` — Creates a Clarity `int` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link intCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const int = intCV;
|
||||
/**
|
||||
* `Cl.uInt` — Creates a Clarity `uint` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link uintCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const uint = uintCV;
|
||||
/**
|
||||
* `Cl.contractPrincipal` — Creates a Clarity contract `principal` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link contractPrincipalCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const contractPrincipal = contractPrincipalCV;
|
||||
/**
|
||||
* `Cl.standardPrincipal` — Creates a Clarity standard `principal` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link standardPrincipalCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const standardPrincipal = standardPrincipalCV;
|
||||
// todo: add .principal method that detects `.` inside string for both standard and contract principals
|
||||
|
||||
// Sequences ///////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* `Cl.list` — Creates a Clarity `list` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link listCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const list = listCV;
|
||||
/**
|
||||
* `Cl.stringAscii` — Creates a Clarity `string-ascii` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link stringAsciiCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const stringAscii = stringAsciiCV;
|
||||
/**
|
||||
* `Cl.stringUtf8` — Creates a Clarity `string-utf8` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link stringUtf8CV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const stringUtf8 = stringUtf8CV;
|
||||
/**
|
||||
* `Cl.buffer` — Creates a Clarity `buffer` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link bufferCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const buffer = bufferCV;
|
||||
/**
|
||||
* `Cl.bufferFromHex` — Converts bytes (from a hex string) to a Clarity `buffer` type, represented as a JS object
|
||||
* @param hex bytes encoded as a hex string
|
||||
* @returns input encoded as a {@link BufferCV}
|
||||
*/
|
||||
export const bufferFromHex = (hex: string) => bufferCV(hexToBytes(hex));
|
||||
/**
|
||||
* `Cl.bufferFromAscii` — Converts bytes (from an ASCII string) to a Clarity `buffer` type, represented as a JS object
|
||||
* @param hex bytes encoded as an ASCII string
|
||||
* @returns input encoded as a {@link BufferCV}
|
||||
*/
|
||||
export const bufferFromAscii = (ascii: string) => bufferCV(asciiToBytes(ascii));
|
||||
/**
|
||||
* `Cl.bufferFromUtf8` — Converts bytes (from an UTF-8 string) to a Clarity `buffer` type, represented as a JS object
|
||||
* @param hex bytes encoded as a UTF-8 string
|
||||
* @returns input encoded as a {@link BufferCV}
|
||||
*/
|
||||
export const bufferFromUtf8 = (utf8: string) => bufferCV(utf8ToBytes(utf8));
|
||||
|
||||
// Composites //////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* `Cl.none` — Creates a Clarity optional `none` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link noneCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const none = noneCV;
|
||||
/**
|
||||
* `Cl.some` — Creates a Clarity optional `some` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link someCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const some = someCV;
|
||||
/**
|
||||
* `Cl.ok` — Creates a Clarity response `ok` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link responseOkCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const ok = responseOkCV;
|
||||
/**
|
||||
* `Cl.error` — Creates a Clarity response `error` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link responseErrorCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const error = responseErrorCV;
|
||||
/**
|
||||
* `Cl.tuple` — Creates a Clarity `tuple` type, represented as a JS object
|
||||
*
|
||||
* Alias for {@link tupleCV}
|
||||
* @see {@link serialize}, {@link deserialize}
|
||||
*/
|
||||
export const tuple = tupleCV;
|
||||
|
||||
// Methods /////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* `Cl.serialize` — Serializes a Clarity JS object to the equivalent hex-encoded representation
|
||||
*
|
||||
* Alias for {@link serializeCV}
|
||||
* @see {@link deserialize}
|
||||
*/
|
||||
export const serialize = serializeCV;
|
||||
/**
|
||||
* `Cl.deserialize` — Deserializes a hex string to the equivalent Clarity JS object
|
||||
*
|
||||
* Alias for {@link deserializeCV}
|
||||
* @see {@link serialize}
|
||||
*/
|
||||
export const deserialize = deserializeCV;
|
||||
|
||||
// todo: add `deserializeReadable` methods that translates enums into name strings
|
||||
@@ -40,8 +40,8 @@ import { bytesToAscii, bytesToUtf8, hexToBytes } from '@stacks/common';
|
||||
* // { type: 0, value: 100n }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
export default function deserializeCV<T extends ClarityValue = ClarityValue>(
|
||||
serializedClarityValue: BytesReader | Uint8Array | string
|
||||
|
||||
@@ -147,8 +147,8 @@ function serializeStringUtf8CV(cv: StringUtf8CV) {
|
||||
* // <Uint8Array 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 64>
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
export function serializeCV(value: ClarityValue): Uint8Array {
|
||||
switch (value.type) {
|
||||
|
||||
@@ -23,8 +23,8 @@ interface FalseCV {
|
||||
* // { type: 3 }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const trueCV = (): BooleanCV => ({ type: ClarityType.BoolTrue });
|
||||
|
||||
@@ -41,8 +41,8 @@ const trueCV = (): BooleanCV => ({ type: ClarityType.BoolTrue });
|
||||
* // { type: 4 }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const falseCV = (): BooleanCV => ({ type: ClarityType.BoolFalse });
|
||||
|
||||
@@ -59,8 +59,8 @@ const falseCV = (): BooleanCV => ({ type: ClarityType.BoolFalse });
|
||||
* // { type: 4 }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const boolCV = (bool: boolean) => (bool ? trueCV() : falseCV());
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ interface BufferCV {
|
||||
* // this is a test
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const bufferCV = (buffer: Uint8Array): BufferCV => {
|
||||
if (buffer.length > 1_000_000) {
|
||||
@@ -53,8 +53,8 @@ const bufferCV = (buffer: Uint8Array): BufferCV => {
|
||||
* // this is a test
|
||||
*```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const bufferCVFromString = (str: string): BufferCV => bufferCV(utf8ToBytes(str));
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ interface IntCV {
|
||||
* // { type: 0, value: 100n }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const intCV = (value: IntegerType): IntCV => {
|
||||
const bigInt = intToBigInt(value, true);
|
||||
@@ -60,8 +60,8 @@ interface UIntCV {
|
||||
* // { type: 1, value: 100n }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const uintCV = (value: IntegerType): UIntCV => {
|
||||
const bigInt = intToBigInt(value, false);
|
||||
|
||||
@@ -9,9 +9,9 @@ interface ListCV<T extends ClarityValue = ClarityValue> {
|
||||
/**
|
||||
* Create list of clarity types
|
||||
*
|
||||
* @param {ClarityValue>values: T[]} list of ClarityValues to be converted to ListCV clarity type
|
||||
* @param {ClarityValue[]} list of ClarityValues to be converted to ListCV clarity type
|
||||
*
|
||||
* @returns {ListCV<T>} returns instance of type ListCV<T>
|
||||
* @returns {ListCV<T>} instance of type ListCV<T> of the provided values
|
||||
*
|
||||
* @example
|
||||
* ```
|
||||
@@ -21,8 +21,8 @@ interface ListCV<T extends ClarityValue = ClarityValue> {
|
||||
* // { type: 11, list: [ { type: 0, value: 1n }, { type: 0, value: 2n }, { type: 0, value: 3n }, { type: 0, value: -4n } ] }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function listCV<T extends ClarityValue = ClarityValue>(values: T[]): ListCV<T> {
|
||||
return { type: ClarityType.List, list: values };
|
||||
|
||||
@@ -24,8 +24,8 @@ interface SomeCV<T extends ClarityValue = ClarityValue> {
|
||||
* // { type: 9 }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function noneCV(): NoneCV {
|
||||
return { type: ClarityType.OptionalNone };
|
||||
@@ -46,8 +46,8 @@ function noneCV(): NoneCV {
|
||||
* // { type: 10, value: { type: 3 } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function someCV<T extends ClarityValue = ClarityValue>(value: T): OptionalCV<T> {
|
||||
return { type: ClarityType.OptionalSome, value };
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { LengthPrefixedString, createAddress, createLPString } from '../../postcondition-types';
|
||||
import { Address, addressToString } from '../../common';
|
||||
import { ClarityType } from '../constants';
|
||||
import { utf8ToBytes } from '@stacks/common';
|
||||
import { Address, addressToString } from '../../common';
|
||||
import { LengthPrefixedString, createAddress, createLPString } from '../../postcondition-types';
|
||||
import { ClarityType } from '../constants';
|
||||
|
||||
type PrincipalCV = StandardPrincipalCV | ContractPrincipalCV;
|
||||
|
||||
@@ -38,9 +38,7 @@ function principalCV(principal: string): PrincipalCV {
|
||||
|
||||
/**
|
||||
* Converts stx address in to StandardPrincipalCV clarity type
|
||||
*
|
||||
* @param {addressString} string value to be converted to StandardPrincipalCV clarity type
|
||||
*
|
||||
* @returns {StandardPrincipalCV} returns instance of type StandardPrincipalCV
|
||||
*
|
||||
* @example
|
||||
@@ -51,8 +49,8 @@ function principalCV(principal: string): PrincipalCV {
|
||||
* // { type: 5, address: { type: 0, version: 22, hash160: 'a5d9d331000f5b79578ce56bd157f29a9056f0d6' } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function standardPrincipalCV(addressString: string): StandardPrincipalCV {
|
||||
const addr = createAddress(addressString);
|
||||
@@ -61,9 +59,7 @@ function standardPrincipalCV(addressString: string): StandardPrincipalCV {
|
||||
|
||||
/**
|
||||
* Converts stx address in to StandardPrincipalCV clarity type
|
||||
*
|
||||
* @param {addressString} string value to be converted to StandardPrincipalCV clarity type
|
||||
*
|
||||
* @returns {StandardPrincipalCV} returns instance of type StandardPrincipalCV
|
||||
*
|
||||
* @example
|
||||
@@ -80,8 +76,8 @@ function standardPrincipalCV(addressString: string): StandardPrincipalCV {
|
||||
* // { type: 5, address: { type: 0, version: 22, hash160: 'a5d9d331000f5b79578ce56bd157f29a9056f0d6' } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function standardPrincipalCVFromAddress(address: Address): StandardPrincipalCV {
|
||||
return { type: ClarityType.PrincipalStandard, address };
|
||||
@@ -89,11 +85,8 @@ function standardPrincipalCVFromAddress(address: Address): StandardPrincipalCV {
|
||||
|
||||
/**
|
||||
* Converts stx address in to ContractPrincipalCV clarity type
|
||||
*
|
||||
* @param {addressString} string value to be converted to ContractPrincipalCV clarity type
|
||||
|
||||
* @param {contractName} string containing contract name
|
||||
*
|
||||
* @returns {ContractPrincipalCV} returns instance of type ContractPrincipalCV
|
||||
*
|
||||
* @example
|
||||
@@ -104,8 +97,8 @@ function standardPrincipalCVFromAddress(address: Address): StandardPrincipalCV {
|
||||
* // { type: 6, address: { type: 0, version: 22, hash160: 'a5d9d331000f5b79578ce56bd157f29a9056f0d6' }, contractName: { type: 2, content: 'test', lengthPrefixBytes: 1, maxLengthBytes: 128 } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function contractPrincipalCV(addressString: string, contractName: string): ContractPrincipalCV {
|
||||
const addr = createAddress(addressString);
|
||||
@@ -115,11 +108,8 @@ function contractPrincipalCV(addressString: string, contractName: string): Contr
|
||||
|
||||
/**
|
||||
* Create ContractPrincipalCV from Address type
|
||||
*
|
||||
* @param {address} address value to be converted to ContractPrincipalCV clarity type
|
||||
*
|
||||
* @param {contractName} contract name of type LengthPrefixedString
|
||||
*
|
||||
* @returns {ContractPrincipalCV} returns instance of type ContractPrincipalCV
|
||||
*
|
||||
* @example
|
||||
@@ -131,8 +121,8 @@ function contractPrincipalCV(addressString: string, contractName: string): Contr
|
||||
* // { type: 6, address: { type: 0, version: 22, hash160: 'a5d9d331000f5b79578ce56bd157f29a9056f0d6' }, contractName: { type: 2, content: 'test', lengthPrefixBytes: 1, maxLengthBytes: 128 } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function contractPrincipalCVFromAddress(
|
||||
address: Address,
|
||||
|
||||
@@ -29,8 +29,8 @@ interface ResponseOkCV<T extends ClarityValue = ClarityValue> {
|
||||
* // { type: 8, value: { type: 0, value: 1n } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function responseErrorCV<T extends ClarityValue = ClarityValue>(value: T): ResponseErrorCV<T> {
|
||||
return { type: ClarityType.ResponseErr, value };
|
||||
@@ -52,8 +52,8 @@ function responseErrorCV<T extends ClarityValue = ClarityValue>(value: T): Respo
|
||||
* // { type: 7, value: { type: 0, value: 1n } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function responseOkCV<T extends ClarityValue = ClarityValue>(value: T): ResponseOkCV<T> {
|
||||
return { type: ClarityType.ResponseOk, value };
|
||||
|
||||
@@ -26,8 +26,8 @@ interface StringUtf8CV {
|
||||
* // { type: 13, data: 'hello' }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const stringAsciiCV = (data: string): StringAsciiCV => {
|
||||
return { type: ClarityType.StringASCII, data };
|
||||
@@ -49,22 +49,22 @@ const stringAsciiCV = (data: string): StringAsciiCV => {
|
||||
* // { type: 13, data: 'hello' }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
const stringUtf8CV = (data: string): StringUtf8CV => {
|
||||
return { type: ClarityType.StringUTF8, data };
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @ignore
|
||||
*/
|
||||
const stringCV = (data: string, encoding: 'ascii' | 'utf8'): StringAsciiCV | StringUtf8CV => {
|
||||
switch (encoding) {
|
||||
case 'ascii':
|
||||
return stringAsciiCV(data);
|
||||
case 'utf8':
|
||||
return stringAsciiCV(data);
|
||||
return stringUtf8CV(data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ interface TupleCV<T extends TupleData = TupleData> {
|
||||
* // { type: 12, data: { c: { type: 3 }, b: { type: 4 }, a: { type: 3 } } }
|
||||
* ```
|
||||
*
|
||||
* @visit
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts clarity test cases for more examples}
|
||||
* @see
|
||||
* {@link https://github.com/hirosystems/stacks.js/blob/master/packages/transactions/tests/clarity.test.ts | clarity test cases for more examples}
|
||||
*/
|
||||
function tupleCV<T extends ClarityValue = ClarityValue>(data: TupleData<T>): TupleCV<TupleData<T>> {
|
||||
for (const key in data) {
|
||||
|
||||
@@ -1,50 +1,45 @@
|
||||
export { StacksTransaction, deserializeTransaction } from './transaction';
|
||||
|
||||
export { BytesReader as BytesReader } from './bytesReader';
|
||||
|
||||
export * from './authorization';
|
||||
export {
|
||||
Authorization,
|
||||
StandardAuthorization,
|
||||
SponsoredAuthorization,
|
||||
SpendingCondition,
|
||||
SponsoredAuthorization,
|
||||
StandardAuthorization,
|
||||
emptyMessageSignature,
|
||||
isSingleSig,
|
||||
} from './authorization';
|
||||
|
||||
export * from './builders';
|
||||
export { BytesReader as BytesReader } from './bytesReader';
|
||||
export * as Cl from './cl';
|
||||
export * from './clarity';
|
||||
export * from './common';
|
||||
export * from './constants';
|
||||
export * from './contract-abi';
|
||||
export * from './keys';
|
||||
export {
|
||||
TokenTransferPayload,
|
||||
ContractCallPayload,
|
||||
SmartContractPayload,
|
||||
VersionedSmartContractPayload,
|
||||
PoisonPayload,
|
||||
CoinbasePayload,
|
||||
CoinbasePayloadToAltRecipient,
|
||||
serializePayload,
|
||||
isTokenTransferPayload,
|
||||
isContractCallPayload,
|
||||
isSmartContractPayload,
|
||||
isPoisonPayload,
|
||||
ContractCallPayload,
|
||||
PoisonPayload,
|
||||
SmartContractPayload,
|
||||
TokenTransferPayload,
|
||||
VersionedSmartContractPayload,
|
||||
isCoinbasePayload,
|
||||
isContractCallPayload,
|
||||
isPoisonPayload,
|
||||
isSmartContractPayload,
|
||||
isTokenTransferPayload,
|
||||
serializePayload,
|
||||
} from './payload';
|
||||
|
||||
export * as Pc from './pc';
|
||||
export {
|
||||
createFungiblePostCondition,
|
||||
createNonFungiblePostCondition,
|
||||
createSTXPostCondition,
|
||||
} from './postcondition';
|
||||
|
||||
export * from './clarity';
|
||||
export * from './keys';
|
||||
export * from './builders';
|
||||
export * from './types';
|
||||
export * from './constants';
|
||||
export * from './contract-abi';
|
||||
export * from './signer';
|
||||
export * from './authorization';
|
||||
export * from './utils';
|
||||
export * from './common';
|
||||
export * from './signature';
|
||||
export * from './structuredDataSignature';
|
||||
export * from './postcondition-types';
|
||||
|
||||
export * as Pc from './pc';
|
||||
export * from './signature';
|
||||
export * from './signer';
|
||||
export * from './structuredDataSignature';
|
||||
export { StacksTransaction, deserializeTransaction } from './transaction';
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
|
||||
64
packages/transactions/tests/cl.test.ts
Normal file
64
packages/transactions/tests/cl.test.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
|
||||
import { hexToBytes } from '@stacks/common';
|
||||
import {
|
||||
Cl,
|
||||
boolCV,
|
||||
bufferCV,
|
||||
contractPrincipalCV,
|
||||
falseCV,
|
||||
intCV,
|
||||
listCV,
|
||||
responseErrorCV,
|
||||
responseOkCV,
|
||||
standardPrincipalCV,
|
||||
stringAsciiCV,
|
||||
stringCV,
|
||||
stringUtf8CV,
|
||||
trueCV,
|
||||
tupleCV,
|
||||
uintCV,
|
||||
} from '../src';
|
||||
|
||||
describe('Cl', () => {
|
||||
const CV_EQUIVALENCES = [
|
||||
{ cv: uintCV, cl: Cl.uint, args: [3] },
|
||||
{ cv: intCV, cl: Cl.int, args: [-5] },
|
||||
{ cv: boolCV, cl: Cl.bool, args: [true] },
|
||||
{ cv: boolCV, cl: Cl.bool, args: [false] },
|
||||
{ cv: trueCV, cl: Cl.bool, args: [true] },
|
||||
{ cv: falseCV, cl: Cl.bool, args: [false] },
|
||||
{
|
||||
cv: standardPrincipalCV,
|
||||
cl: Cl.standardPrincipal,
|
||||
args: ['ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE'],
|
||||
},
|
||||
{
|
||||
cv: contractPrincipalCV,
|
||||
cl: Cl.contractPrincipal,
|
||||
args: ['ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE', 'contract'],
|
||||
},
|
||||
{ cv: listCV, cl: Cl.list, args: [[uintCV(1), Cl.uint(2)]] },
|
||||
{ cv: stringCV, cl: Cl.stringAscii, args: ['hello', 'ascii'] },
|
||||
{ cv: stringCV, cl: Cl.stringUtf8, args: ['hello', 'utf8'] },
|
||||
{ cv: stringAsciiCV, cl: Cl.stringAscii, args: ['hello'] },
|
||||
{ cv: stringUtf8CV, cl: Cl.stringUtf8, args: ['hello'] },
|
||||
{ cv: bufferCV, cl: Cl.buffer, args: [hexToBytes('beef')] },
|
||||
{ cv: responseOkCV, cl: Cl.ok, args: [Cl.uint(1)] },
|
||||
{ cv: responseErrorCV, cl: Cl.error, args: [Cl.int(-1)] },
|
||||
{ cv: tupleCV, cl: Cl.tuple, args: [{ a: Cl.uint(1), b: uintCV(2) }] },
|
||||
] as {
|
||||
cv: Function;
|
||||
cl: Function;
|
||||
args: any[];
|
||||
}[];
|
||||
|
||||
test.each(CV_EQUIVALENCES)('cv == cl', ({ cv, cl, args }) => {
|
||||
const cvValue = cv(...args);
|
||||
const clValue = cl(...args);
|
||||
expect(clValue).toEqual(cvValue);
|
||||
|
||||
const wired = Cl.deserialize(Cl.serialize(clValue));
|
||||
expect(wired).toEqual(clValue);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user