@fleet-sdk/common 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.mts +268 -0
  4. package/dist/index.d.ts +268 -0
  5. package/dist/index.js +604 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +551 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +9 -9
  10. package/src/index.ts +1 -0
  11. package/src/models/collection.ts +107 -0
  12. package/src/models/index.ts +1 -0
  13. package/src/types/boxes.ts +2 -2
  14. package/src/types/common.ts +10 -3
  15. package/src/types/inputs.ts +3 -3
  16. package/src/utils/{arrayUtils.ts → array.ts} +26 -21
  17. package/src/utils/assertions.ts +90 -0
  18. package/src/utils/{bigIntUtils.ts → bigInt.ts} +28 -65
  19. package/src/utils/bytes.bench.ts +43 -0
  20. package/src/utils/bytes.ts +34 -0
  21. package/src/utils/index.ts +6 -6
  22. package/src/utils/object.ts +28 -0
  23. package/src/utils/{boxUtils.ts → utxo.ts} +27 -5
  24. package/dist/cjs/index.d.ts +0 -2
  25. package/dist/cjs/index.js +0 -19
  26. package/dist/cjs/index.js.map +0 -1
  27. package/dist/cjs/types/boxes.d.ts +0 -22
  28. package/dist/cjs/types/boxes.js +0 -3
  29. package/dist/cjs/types/boxes.js.map +0 -1
  30. package/dist/cjs/types/common.d.ts +0 -18
  31. package/dist/cjs/types/common.js +0 -15
  32. package/dist/cjs/types/common.js.map +0 -1
  33. package/dist/cjs/types/contextExtension.d.ts +0 -4
  34. package/dist/cjs/types/contextExtension.js +0 -3
  35. package/dist/cjs/types/contextExtension.js.map +0 -1
  36. package/dist/cjs/types/index.d.ts +0 -8
  37. package/dist/cjs/types/index.js +0 -25
  38. package/dist/cjs/types/index.js.map +0 -1
  39. package/dist/cjs/types/inputs.d.ts +0 -37
  40. package/dist/cjs/types/inputs.js +0 -3
  41. package/dist/cjs/types/inputs.js.map +0 -1
  42. package/dist/cjs/types/proverResult.d.ts +0 -6
  43. package/dist/cjs/types/proverResult.js +0 -3
  44. package/dist/cjs/types/proverResult.js.map +0 -1
  45. package/dist/cjs/types/registers.d.ts +0 -9
  46. package/dist/cjs/types/registers.js +0 -3
  47. package/dist/cjs/types/registers.js.map +0 -1
  48. package/dist/cjs/types/token.d.ts +0 -18
  49. package/dist/cjs/types/token.js +0 -3
  50. package/dist/cjs/types/token.js.map +0 -1
  51. package/dist/cjs/types/transactions.d.ts +0 -22
  52. package/dist/cjs/types/transactions.js +0 -3
  53. package/dist/cjs/types/transactions.js.map +0 -1
  54. package/dist/cjs/utils/arrayUtils.d.ts +0 -26
  55. package/dist/cjs/utils/arrayUtils.js +0 -151
  56. package/dist/cjs/utils/arrayUtils.js.map +0 -1
  57. package/dist/cjs/utils/bigIntLiterals.d.ts +0 -7
  58. package/dist/cjs/utils/bigIntLiterals.js +0 -12
  59. package/dist/cjs/utils/bigIntLiterals.js.map +0 -1
  60. package/dist/cjs/utils/bigIntUtils.d.ts +0 -51
  61. package/dist/cjs/utils/bigIntUtils.js +0 -158
  62. package/dist/cjs/utils/bigIntUtils.js.map +0 -1
  63. package/dist/cjs/utils/boxUtils.d.ts +0 -21
  64. package/dist/cjs/utils/boxUtils.js +0 -110
  65. package/dist/cjs/utils/boxUtils.js.map +0 -1
  66. package/dist/cjs/utils/index.d.ts +0 -6
  67. package/dist/cjs/utils/index.js +0 -23
  68. package/dist/cjs/utils/index.js.map +0 -1
  69. package/dist/cjs/utils/objectUtils.d.ts +0 -3
  70. package/dist/cjs/utils/objectUtils.js +0 -23
  71. package/dist/cjs/utils/objectUtils.js.map +0 -1
  72. package/dist/cjs/utils/stringUtils.d.ts +0 -7
  73. package/dist/cjs/utils/stringUtils.js +0 -21
  74. package/dist/cjs/utils/stringUtils.js.map +0 -1
  75. package/dist/esm/index.d.ts +0 -2
  76. package/dist/esm/index.js +0 -3
  77. package/dist/esm/index.js.map +0 -1
  78. package/dist/esm/types/boxes.d.ts +0 -22
  79. package/dist/esm/types/boxes.js +0 -2
  80. package/dist/esm/types/boxes.js.map +0 -1
  81. package/dist/esm/types/common.d.ts +0 -18
  82. package/dist/esm/types/common.js +0 -12
  83. package/dist/esm/types/common.js.map +0 -1
  84. package/dist/esm/types/contextExtension.d.ts +0 -4
  85. package/dist/esm/types/contextExtension.js +0 -2
  86. package/dist/esm/types/contextExtension.js.map +0 -1
  87. package/dist/esm/types/index.d.ts +0 -8
  88. package/dist/esm/types/index.js +0 -9
  89. package/dist/esm/types/index.js.map +0 -1
  90. package/dist/esm/types/inputs.d.ts +0 -37
  91. package/dist/esm/types/inputs.js +0 -2
  92. package/dist/esm/types/inputs.js.map +0 -1
  93. package/dist/esm/types/proverResult.d.ts +0 -6
  94. package/dist/esm/types/proverResult.js +0 -2
  95. package/dist/esm/types/proverResult.js.map +0 -1
  96. package/dist/esm/types/registers.d.ts +0 -9
  97. package/dist/esm/types/registers.js +0 -2
  98. package/dist/esm/types/registers.js.map +0 -1
  99. package/dist/esm/types/token.d.ts +0 -18
  100. package/dist/esm/types/token.js +0 -2
  101. package/dist/esm/types/token.js.map +0 -1
  102. package/dist/esm/types/transactions.d.ts +0 -22
  103. package/dist/esm/types/transactions.js +0 -2
  104. package/dist/esm/types/transactions.js.map +0 -1
  105. package/dist/esm/utils/arrayUtils.d.ts +0 -26
  106. package/dist/esm/utils/arrayUtils.js +0 -135
  107. package/dist/esm/utils/arrayUtils.js.map +0 -1
  108. package/dist/esm/utils/bigIntLiterals.d.ts +0 -7
  109. package/dist/esm/utils/bigIntLiterals.js +0 -9
  110. package/dist/esm/utils/bigIntLiterals.js.map +0 -1
  111. package/dist/esm/utils/bigIntUtils.d.ts +0 -51
  112. package/dist/esm/utils/bigIntUtils.js +0 -148
  113. package/dist/esm/utils/bigIntUtils.js.map +0 -1
  114. package/dist/esm/utils/boxUtils.d.ts +0 -21
  115. package/dist/esm/utils/boxUtils.js +0 -103
  116. package/dist/esm/utils/boxUtils.js.map +0 -1
  117. package/dist/esm/utils/index.d.ts +0 -6
  118. package/dist/esm/utils/index.js +0 -7
  119. package/dist/esm/utils/index.js.map +0 -1
  120. package/dist/esm/utils/objectUtils.d.ts +0 -3
  121. package/dist/esm/utils/objectUtils.js +0 -17
  122. package/dist/esm/utils/objectUtils.js.map +0 -1
  123. package/dist/esm/utils/stringUtils.d.ts +0 -7
  124. package/dist/esm/utils/stringUtils.js +0 -16
  125. package/dist/esm/utils/stringUtils.js.map +0 -1
  126. package/dist/tsconfig.esm.tsbuildinfo +0 -1
  127. package/dist/tsconfig.tsbuildinfo +0 -1
  128. package/src/utils/bigIntLiterals.ts +0 -9
  129. package/src/utils/objectUtils.ts +0 -19
  130. package/src/utils/stringUtils.ts +0 -18
@@ -0,0 +1,107 @@
1
+ import { OneOrMore } from "../types";
2
+ import { isDefined } from "../utils";
3
+
4
+ export type CollectionAddOptions = { index?: number };
5
+
6
+ export abstract class Collection<InternalType, ExternalType> implements Iterable<InternalType> {
7
+ protected readonly _items: InternalType[];
8
+
9
+ constructor() {
10
+ this._items = [];
11
+ }
12
+
13
+ protected _isIndexOutOfBounds(index: number): boolean {
14
+ return index < 0 || index >= this._items.length;
15
+ }
16
+
17
+ [Symbol.iterator](): Iterator<InternalType> {
18
+ let counter = 0;
19
+
20
+ return {
21
+ next: () => {
22
+ return {
23
+ done: counter >= this.length,
24
+ value: this._items[counter++]
25
+ };
26
+ }
27
+ };
28
+ }
29
+
30
+ public get length(): number {
31
+ return this._items.length;
32
+ }
33
+
34
+ public get isEmpty(): boolean {
35
+ return this.length === 0;
36
+ }
37
+
38
+ public at(index: number): InternalType {
39
+ if (this._isIndexOutOfBounds(index)) {
40
+ throw new RangeError(`Index '${index}' is out of range.`);
41
+ }
42
+
43
+ return this._items[index];
44
+ }
45
+
46
+ public add(items: OneOrMore<ExternalType>, options?: CollectionAddOptions): number {
47
+ return this._addOneOrMore(items, options);
48
+ }
49
+
50
+ abstract remove(item: unknown): number;
51
+
52
+ protected abstract _map(item: ExternalType | InternalType): InternalType;
53
+
54
+ protected _addOne(item: InternalType | ExternalType, options?: CollectionAddOptions): number {
55
+ if (isDefined(options) && isDefined(options.index)) {
56
+ if (options.index === this.length) {
57
+ this._items.push(this._map(item));
58
+
59
+ return this.length;
60
+ }
61
+
62
+ if (this._isIndexOutOfBounds(options.index)) {
63
+ throw new RangeError(`Index '${options.index}' is out of range.`);
64
+ }
65
+
66
+ this._items.splice(options.index, 0, this._map(item));
67
+
68
+ return this.length;
69
+ }
70
+
71
+ this._items.push(this._map(item));
72
+
73
+ return this._items.length;
74
+ }
75
+
76
+ protected _addOneOrMore(items: OneOrMore<ExternalType>, options?: CollectionAddOptions): number {
77
+ if (Array.isArray(items)) {
78
+ if (isDefined(options) && isDefined(options.index)) {
79
+ items = items.reverse();
80
+ }
81
+
82
+ for (const item of items) {
83
+ this._addOne(item, options);
84
+ }
85
+ } else {
86
+ this._addOne(items, options);
87
+ }
88
+
89
+ return this.length;
90
+ }
91
+
92
+ public toArray(): InternalType[] {
93
+ return [...this._items];
94
+ }
95
+
96
+ public reduce<U>(
97
+ callbackFn: (
98
+ accumulator: U,
99
+ currentValue: InternalType,
100
+ currentIndex: number,
101
+ array: InternalType[]
102
+ ) => U,
103
+ initialValue: U
104
+ ): U {
105
+ return this._items.reduce(callbackFn, initialValue);
106
+ }
107
+ }
@@ -0,0 +1 @@
1
+ export * from "./collection";
@@ -1,4 +1,4 @@
1
- import { ErgoTree } from "./common";
1
+ import { ErgoTreeHex } from "./common";
2
2
  import { NonMandatoryRegisters } from "./registers";
3
3
  import { TokenAmount } from "./token";
4
4
  import { TransactionId } from "./transactions";
@@ -7,7 +7,7 @@ export type BoxId = string;
7
7
  export type AmountType = string | bigint;
8
8
 
9
9
  type BoxBaseType<T extends AmountType, R extends NonMandatoryRegisters> = {
10
- ergoTree: ErgoTree;
10
+ ergoTree: ErgoTreeHex;
11
11
  creationHeight: number;
12
12
  value: T;
13
13
  assets: TokenAmount<T>[];
@@ -1,5 +1,5 @@
1
1
  export type HexString = string;
2
- export type ErgoTree = string;
2
+ export type ErgoTreeHex = string;
3
3
  export type Base58String = string;
4
4
  export type Amount = string | bigint;
5
5
 
@@ -13,8 +13,8 @@ export type FilterPredicate<T> = (item: T) => boolean;
13
13
  export type BuildOutputType = "default" | "EIP-12";
14
14
 
15
15
  export enum Network {
16
- Mainnet = 0 << 4,
17
- Testnet = 1 << 4
16
+ Mainnet = 0x00,
17
+ Testnet = 0x10
18
18
  }
19
19
 
20
20
  export enum AddressType {
@@ -22,3 +22,10 @@ export enum AddressType {
22
22
  P2SH = 2,
23
23
  P2S = 3
24
24
  }
25
+
26
+ export const ergoTreeHeaderFlags = {
27
+ sizeInclusion: 0x08,
28
+ constantSegregation: 0x10
29
+ } as const;
30
+
31
+ export type ErgoTreeHeaderFlag = (typeof ergoTreeHeaderFlags)[keyof typeof ergoTreeHeaderFlags];
@@ -1,5 +1,5 @@
1
1
  import { BoxId } from "./boxes";
2
- import { ErgoTree } from "./common";
2
+ import { ErgoTreeHex } from "./common";
3
3
  import { ContextExtension } from "./contextExtension";
4
4
  import { ProverResult } from "./proverResult";
5
5
  import { NonMandatoryRegisters } from "./registers";
@@ -19,7 +19,7 @@ export type UnsignedInput = {
19
19
  export type EIP12UnsignedInput = UnsignedInput & {
20
20
  transactionId: TransactionId;
21
21
  index: number;
22
- ergoTree: ErgoTree;
22
+ ergoTree: ErgoTreeHex;
23
23
  creationHeight: number;
24
24
  value: string;
25
25
  assets: TokenAmount<string>[];
@@ -30,7 +30,7 @@ export type EIP12UnsignedDataInput = {
30
30
  boxId: BoxId;
31
31
  transactionId: TransactionId;
32
32
  index: number;
33
- ergoTree: ErgoTree;
33
+ ergoTree: ErgoTreeHex;
34
34
  creationHeight: number;
35
35
  value: string;
36
36
  assets: TokenAmount<string>[];
@@ -1,36 +1,37 @@
1
1
  import { SortingDirection, SortingSelector } from "../types";
2
+ import { assert, isEmpty } from "./assertions";
2
3
 
3
4
  type ObjectSelector<T> = (item: T) => T[keyof T];
4
5
 
5
- export function isEmpty<T extends object>(obj?: T): obj is undefined;
6
- export function isEmpty<T>(array?: T[]): array is undefined;
7
- export function isEmpty<T>(obj?: T[] | object): obj is undefined {
8
- if (!obj) {
9
- return true;
10
- }
6
+ export function first(array: undefined): undefined;
7
+ export function first<T>(array: ArrayLike<T>): T;
8
+ export function first<T>(array: ArrayLike<T> | undefined): T | number | undefined {
9
+ if (!array) return undefined;
10
+ assert(array.length > 0, "Empty array.");
11
11
 
12
- return Array.isArray(obj) ? obj.length === 0 : Object.keys(obj).length === 0;
12
+ return array[0];
13
13
  }
14
14
 
15
- export function some<T extends object>(obj?: T): obj is T;
16
- export function some<T>(array?: T[]): array is T[];
17
- export function some<T>(obj?: T[] | object): boolean {
18
- return !isEmpty(obj);
15
+ export function last(array: undefined): undefined;
16
+ export function last<T>(array: ArrayLike<T>): T;
17
+ export function last<T>(array: ArrayLike<T> | undefined): T | undefined {
18
+ if (!array) return undefined;
19
+ assert(array.length > 0, "Empty array.");
20
+
21
+ return at(array, -1);
19
22
  }
20
23
 
21
- export function first(array: undefined): undefined;
22
- export function first(array: Uint8Array): number;
23
- export function first<T>(array: ArrayLike<T>): T;
24
- export function first<T>(array?: ArrayLike<T> | Uint8Array): T | number | undefined {
25
- if (!array) {
26
- return;
27
- }
24
+ export function at(array: undefined, index: number): undefined;
25
+ export function at<T>(array: ArrayLike<T>, index: number): T;
26
+ export function at<T>(array: ArrayLike<T> | undefined, index: number): T | undefined {
27
+ const len = array?.length;
28
+ if (!len) return undefined;
28
29
 
29
- if (!array[0]) {
30
- throw Error("Empty array.");
30
+ if (index < 0) {
31
+ index += len;
31
32
  }
32
33
 
33
- return array[0];
34
+ return array[index];
34
35
  }
35
36
 
36
37
  /**
@@ -189,3 +190,7 @@ export function uniqBy<T>(
189
190
  .values()
190
191
  );
191
192
  }
193
+
194
+ export function depthOf(array: unknown | unknown[]): number {
195
+ return Array.isArray(array) ? 1 + Math.max(0, ...array.map(depthOf)) : 0;
196
+ }
@@ -0,0 +1,90 @@
1
+ export type AssertErrorMessageInput = string | Error | (() => string);
2
+
3
+ // eslint-disable-next-line @typescript-eslint/ban-types
4
+ type Constructable = Function;
5
+
6
+ type JSPrimitiveTypes =
7
+ | "string"
8
+ | "number"
9
+ | "bigint"
10
+ | "boolean"
11
+ | "symbol"
12
+ | "undefined"
13
+ | "object"
14
+ | "function";
15
+
16
+ export function assert(condition: boolean, error: AssertErrorMessageInput): asserts condition {
17
+ if (condition) return;
18
+
19
+ let err: Error | undefined = undefined;
20
+ switch (typeof error) {
21
+ case "string":
22
+ err = new Error(error);
23
+ break;
24
+ case "function":
25
+ err = new Error(error());
26
+ break;
27
+ default:
28
+ err = error;
29
+ }
30
+
31
+ throw err;
32
+ }
33
+
34
+ export function assertTypeOf<T>(obj: T, expected: JSPrimitiveTypes): asserts obj {
35
+ const type = typeof obj;
36
+
37
+ if (type !== expected) {
38
+ throw new Error(`Expected an object of type '${expected}', got '${type}'.`);
39
+ }
40
+ }
41
+
42
+ const toString = (value: unknown) => Object.prototype.toString.call(value);
43
+ function getTypeName(value: unknown): string {
44
+ if (value === null) return "null";
45
+ const type = typeof value;
46
+
47
+ return type === "object" || type === "function" ? toString(value).slice(8, -1) : type;
48
+ }
49
+
50
+ export function assertInstanceOf<T>(obj: T, expected: Constructable): asserts obj {
51
+ const condition = obj instanceof expected;
52
+
53
+ if (!condition) {
54
+ throw new Error(`Expected an instance of '${expected.name}', got '${getTypeName(obj)}'.`);
55
+ }
56
+ }
57
+
58
+ export function isEmpty<T extends object>(obj?: T): obj is undefined;
59
+ export function isEmpty<T>(array?: T[]): array is undefined;
60
+ export function isEmpty<T>(obj?: T[] | object): obj is undefined {
61
+ if (!obj) return true;
62
+
63
+ return Array.isArray(obj) ? obj.length === 0 : Object.keys(obj).length === 0;
64
+ }
65
+
66
+ export function some<T extends object>(obj?: T): obj is T;
67
+ export function some<T>(array?: T[]): array is T[];
68
+ export function some<T>(obj?: T[] | object): boolean {
69
+ return !isEmpty(obj);
70
+ }
71
+
72
+ export function isTruthy<T>(value?: T): value is NonNullable<T> {
73
+ return !!value;
74
+ }
75
+
76
+ export function isFalsy<T>(value?: T): value is undefined {
77
+ return !value;
78
+ }
79
+
80
+ export function isUndefined(v: unknown): v is undefined {
81
+ return v === undefined || v === null || Number.isNaN(v);
82
+ }
83
+
84
+ export function isDefined<T>(v: T | undefined): v is T {
85
+ return !isUndefined(v);
86
+ }
87
+
88
+ export function hasKey(o: unknown, key: PropertyKey): boolean {
89
+ return Object.prototype.hasOwnProperty.call(o, key);
90
+ }
@@ -1,10 +1,17 @@
1
1
  import { Amount } from "../types";
2
- import { first, isEmpty } from "./arrayUtils";
3
- import { _0n, _10n } from "./bigIntLiterals";
4
- import { isUndefined } from "./objectUtils";
2
+ import { first } from "./array";
3
+ import { isEmpty, isUndefined } from "./assertions";
5
4
 
6
5
  type NumberLike = string | number | bigint | boolean;
7
6
 
7
+ export const _0n = BigInt(0);
8
+ export const _1n = BigInt(1);
9
+ export const _7n = BigInt(7);
10
+ export const _10n = BigInt(10);
11
+ export const _63n = BigInt(63);
12
+ export const _127n = BigInt(127);
13
+ export const _128n = BigInt(128);
14
+
8
15
  export function ensureBigInt(number: NumberLike): bigint {
9
16
  return typeof number === "bigint" ? number : BigInt(number);
10
17
  }
@@ -92,6 +99,10 @@ export function decimalize(value: Amount, options?: FormattingOptions | number):
92
99
  return _buildFormattedDecimal(integer.toString(10), decimal.toString(10), options);
93
100
  }
94
101
 
102
+ export function percent(value: bigint, percentage: bigint, precision = 2n) {
103
+ return (value * percentage) / 10n ** precision;
104
+ }
105
+
95
106
  function _buildFormattedDecimal(
96
107
  integer: string,
97
108
  decimal: string,
@@ -150,74 +161,26 @@ export function sumBy<T>(
150
161
  return acc;
151
162
  }
152
163
 
153
- /**
154
- * Converts a hex string to bigint.
155
- * @param hex The hex string to be converted.
156
- * @returns The bigint value represented by the hex string.
157
- */
158
- export function hexToBigInt(hex: string): bigint {
159
- // https://coolaj86.com/articles/convert-hex-to-decimal-with-js-bigints/
160
- if (hex.length % 2) {
161
- hex = "0" + hex;
162
- }
163
-
164
- const value = BigInt("0x" + hex);
165
- const highByte = parseInt(hex.slice(0, 2), 16);
166
-
167
- if (0x80 & highByte) {
168
- return -_bitNegate(value); // add two's complement and invert the number to negative
169
- }
170
-
171
- return value;
172
- }
173
-
174
- /**
175
- * Serializes a `BigInt` to a hex string
176
- * @param value The bigint value to be serialized
177
- * @returns Hex representation for the provided `number`.
178
- */
179
- export function bigIntToHex(value: bigint): string {
180
- // implementation inspired on
181
- // https://coolaj86.com/articles/convert-decimal-to-hex-with-js-bigints/
182
- const positive = value >= _0n;
183
- if (!positive) {
184
- value = _bitNegate(value);
185
- }
164
+ export function min<T extends bigint | number>(...numbers: T[]): T {
165
+ let min = first(numbers);
186
166
 
187
- let hex = value.toString(16);
188
- if (hex.length % 2) {
189
- hex = "0" + hex;
190
- }
191
-
192
- if (positive && 0x80 & parseInt(hex.slice(0, 2), 16)) {
193
- hex = "00" + hex;
167
+ for (const num of numbers) {
168
+ if (num < min) {
169
+ min = num;
170
+ }
194
171
  }
195
172
 
196
- return hex;
173
+ return min;
197
174
  }
198
175
 
199
- /**
200
- * Returns the two’s complement of a bigint value.
201
- * @param value The bigint value to negate.
202
- * @returns The two’s complement of `number` as a bigint.
203
- */
204
- export function _bitNegate(value: bigint): bigint {
205
- const negative = value < _0n;
206
- if (negative) {
207
- value = -value; // turn into a positive number
208
- }
209
-
210
- const bits = value.toString(2);
211
- let bitLen = bits.length; // convert to binary
176
+ export function max<T extends bigint | number>(...numbers: T[]): T {
177
+ let max = first(numbers);
212
178
 
213
- const mod = bitLen % 8;
214
- if (mod > 0) {
215
- bitLen += 8 - mod;
216
- } else if (negative && first(bits) === "1" && bits.indexOf("1", 1) !== -1) {
217
- bitLen += 8;
179
+ for (const num of numbers) {
180
+ if (num > max) {
181
+ max = num;
182
+ }
218
183
  }
219
184
 
220
- const mask = (1n << BigInt(bitLen)) - 1n; // create a mask
221
-
222
- return (~value & mask) + 1n; // invert bits, mask it, and add one
185
+ return max;
223
186
  }
@@ -0,0 +1,43 @@
1
+ import { bench, describe } from "vitest";
2
+ import { isHex } from "./bytes";
3
+
4
+ const HEX_PATTERN = /^[0-9A-Fa-f]+$/s;
5
+ export function isHexRegex(value?: string) {
6
+ if (!value || value.length % 2) return false;
7
+
8
+ return HEX_PATTERN.test(value);
9
+ }
10
+
11
+ const HEX_CHARSET = new Set("0123456789abcdefABCDEF");
12
+ function isHexChar(value: string) {
13
+ if (!value || value.length % 2) return false;
14
+
15
+ const valueSet = new Set(Array.from(value));
16
+ for (const c of valueSet) {
17
+ if (!HEX_CHARSET.has(c)) {
18
+ return false;
19
+ }
20
+ }
21
+
22
+ return true;
23
+ }
24
+
25
+ describe("Hex string checking", () => {
26
+ const validHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7";
27
+ const invalidHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7tt";
28
+
29
+ bench("Using charset", () => {
30
+ isHexChar(validHex);
31
+ isHexChar(invalidHex);
32
+ });
33
+
34
+ bench("Using regex", () => {
35
+ isHexRegex(validHex);
36
+ isHexRegex(invalidHex);
37
+ });
38
+
39
+ bench("Using number constructor", () => {
40
+ isHex(validHex);
41
+ isHex(invalidHex);
42
+ });
43
+ });
@@ -0,0 +1,34 @@
1
+ import { assertInstanceOf } from ".";
2
+
3
+ export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
4
+ const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
5
+
6
+ let pad = 0;
7
+ for (const bytes of arrays) {
8
+ assertInstanceOf(bytes, Uint8Array);
9
+
10
+ r.set(bytes, pad);
11
+ pad += bytes.length;
12
+ }
13
+
14
+ return r;
15
+ }
16
+
17
+ export function isHex(value?: string) {
18
+ if (!value || value.length % 2) return false;
19
+
20
+ if (!value.startsWith("0x")) {
21
+ value = "0x" + value;
22
+ }
23
+
24
+ return !isNaN(Number(value));
25
+ }
26
+
27
+ /**
28
+ * Get hex string size in bytes
29
+ * @param hex
30
+ * @returns the byte size if the hex string
31
+ */
32
+ export function byteSizeOf(hex: string): number {
33
+ return hex.length / 2;
34
+ }
@@ -1,6 +1,6 @@
1
- export * from "./arrayUtils";
2
- export * from "./bigIntLiterals";
3
- export * from "./bigIntUtils";
4
- export * from "./boxUtils";
5
- export * from "./objectUtils";
6
- export * from "./stringUtils";
1
+ export * from "./array";
2
+ export * from "./bigInt";
3
+ export * from "./utxo";
4
+ export * from "./object";
5
+ export * from "./assertions";
6
+ export * from "./bytes";
@@ -0,0 +1,28 @@
1
+ import { isEmpty, isUndefined } from "./assertions";
2
+
3
+ export function clearUndefined(value: Record<string, unknown>) {
4
+ const result: Record<string, unknown> = {};
5
+ for (const key in value) {
6
+ const val = value[key];
7
+ if (!isUndefined(val)) {
8
+ result[key] = val;
9
+ }
10
+ }
11
+
12
+ return result;
13
+ }
14
+
15
+ export function ensureDefaults<T extends Partial<R>, R extends object = Required<T>>(
16
+ options: T | undefined,
17
+ defaults: R
18
+ ): R & T {
19
+ if (isEmpty(options)) return defaults as R & T;
20
+
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ const newObj: any = { ...defaults, ...options };
23
+ for (const key in newObj) {
24
+ newObj[key as keyof object] = options[key as keyof R] ?? defaults[key as keyof R];
25
+ }
26
+
27
+ return newObj;
28
+ }
@@ -1,8 +1,15 @@
1
- import { Amount, AmountType, Box, NonMandatoryRegisters, TokenAmount, TokenId } from "../types";
2
- import { isEmpty } from "./arrayUtils";
3
- import { _0n } from "./bigIntLiterals";
4
- import { ensureBigInt } from "./bigIntUtils";
5
- import { isDefined, isUndefined } from "./objectUtils";
1
+ import {
2
+ Amount,
3
+ AmountType,
4
+ Box,
5
+ BoxCandidate,
6
+ NonMandatoryRegisters,
7
+ TokenAmount,
8
+ TokenId
9
+ } from "../types";
10
+ import { isDefined, isEmpty, isUndefined } from "./assertions";
11
+ import { ensureBigInt } from "./bigInt";
12
+ import { _0n } from "./bigInt";
6
13
 
7
14
  const NANOERGS_TOKEN_ID = "nanoErgs";
8
15
 
@@ -159,3 +166,18 @@ export type MinimalBoxAmounts = readonly {
159
166
  value: Amount;
160
167
  assets: TokenAmount<Amount>[];
161
168
  }[];
169
+
170
+ export function ensureUTxOBigInt(box: Box<Amount>): Box<bigint>;
171
+ export function ensureUTxOBigInt(candidate: BoxCandidate<Amount>): BoxCandidate<bigint>;
172
+ export function ensureUTxOBigInt(
173
+ box: Box<Amount> | BoxCandidate<Amount>
174
+ ): BoxCandidate<bigint> | Box<bigint> {
175
+ return {
176
+ ...box,
177
+ value: ensureBigInt(box.value),
178
+ assets: box.assets.map((token) => ({
179
+ tokenId: token.tokenId,
180
+ amount: ensureBigInt(token.amount)
181
+ }))
182
+ };
183
+ }
@@ -1,2 +0,0 @@
1
- export * from "./utils";
2
- export * from "./types";
package/dist/cjs/index.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./utils"), exports);
18
- __exportStar(require("./types"), exports);
19
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,0CAAwB"}
@@ -1,22 +0,0 @@
1
- import { ErgoTree } from "./common";
2
- import { NonMandatoryRegisters } from "./registers";
3
- import { TokenAmount } from "./token";
4
- import { TransactionId } from "./transactions";
5
- export type BoxId = string;
6
- export type AmountType = string | bigint;
7
- type BoxBaseType<T extends AmountType, R extends NonMandatoryRegisters> = {
8
- ergoTree: ErgoTree;
9
- creationHeight: number;
10
- value: T;
11
- assets: TokenAmount<T>[];
12
- additionalRegisters: R;
13
- };
14
- export type BoxCandidate<T extends AmountType, R extends NonMandatoryRegisters = NonMandatoryRegisters> = BoxBaseType<T, R> & {
15
- boxId?: BoxId;
16
- };
17
- export type Box<T extends AmountType, R extends NonMandatoryRegisters = NonMandatoryRegisters> = BoxBaseType<T, R> & {
18
- boxId: BoxId;
19
- transactionId: TransactionId;
20
- index: number;
21
- };
22
- export {};
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=boxes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"boxes.js","sourceRoot":"","sources":["../../../src/types/boxes.ts"],"names":[],"mappings":""}