@btc-vision/transaction 1.7.5 → 1.7.7

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.
@@ -1 +1 @@
1
- export declare const version = "1.7.1";
1
+ export declare const version = "1.7.7";
@@ -1,11 +1,11 @@
1
- import { Map } from './Map.js';
2
- export declare class DeterministicMap<K, V> {
1
+ import { FastMap, PropertyExtendedKey } from './FastMap.js';
2
+ export declare class DeterministicMap<K extends PropertyExtendedKey, V> {
3
3
  #private;
4
4
  private compareFn;
5
5
  private map;
6
6
  constructor(compareFn: (a: K, b: K) => number);
7
7
  get size(): number;
8
- static fromMap<K, V>(map: Map<K, V>, compareFn: (a: K, b: K) => number): DeterministicMap<K, V>;
8
+ static fromMap<K extends PropertyExtendedKey, V>(map: FastMap<K, V>, compareFn: (a: K, b: K) => number): DeterministicMap<K, V>;
9
9
  set(key: K, value: V): void;
10
10
  get(key: K): V | undefined;
11
11
  entries(): IterableIterator<[K, V]>;
@@ -0,0 +1,24 @@
1
+ export type PropertyExtendedKey = PropertyKey | bigint;
2
+ export type FastRecord<V> = {
3
+ [key: string]: V;
4
+ };
5
+ export type IndexKey = string | number;
6
+ export declare class FastMap<K extends PropertyExtendedKey, V> {
7
+ protected _keys: K[];
8
+ protected _values: FastRecord<V>;
9
+ constructor(iterable?: ReadonlyArray<readonly [K, V]> | null | FastMap<K, V>);
10
+ get size(): number;
11
+ setAll(map: FastMap<K, V>): void;
12
+ addAll(map: FastMap<K, V>): void;
13
+ keys(): IterableIterator<K>;
14
+ values(): IterableIterator<V>;
15
+ entries(): IterableIterator<[K, V]>;
16
+ set(key: K, value: V): this;
17
+ indexOf(key: K): number;
18
+ get(key: K): V | undefined;
19
+ has(key: K): boolean;
20
+ delete(key: K): boolean;
21
+ clear(): void;
22
+ forEach(callback: (value: V, key: K, map: FastMap<K, V>) => void, thisArg?: unknown): void;
23
+ [Symbol.iterator](): IterableIterator<[K, V]>;
24
+ }
@@ -75,7 +75,7 @@ export * from './metadata/tokens.js';
75
75
  export * from './transaction/browser/Web3Provider.js';
76
76
  export * from './keypair/Secp256k1PointDeriver.js';
77
77
  export * from './transaction/ContractAddress.js';
78
- export * from './deterministic/Map.js';
78
+ export * from './deterministic/FastMap.js';
79
79
  export * from './deterministic/CustomMap.js';
80
80
  declare global {
81
81
  interface Window {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ export declare class FastBigIntMap {
2
+ private items;
3
+ private keyOrder;
4
+ constructor(iterable?: ReadonlyArray<readonly [bigint, bigint]> | null | FastBigIntMap);
5
+ get size(): number;
6
+ setAll(map: FastBigIntMap): void;
7
+ addAll(map: FastBigIntMap): void;
8
+ set(key: bigint, value: bigint): this;
9
+ get(key: bigint): bigint | undefined;
10
+ has(key: bigint): boolean;
11
+ delete(key: bigint): boolean;
12
+ clear(): void;
13
+ entries(): IterableIterator<[bigint, bigint]>;
14
+ keys(): IterableIterator<bigint>;
15
+ values(): IterableIterator<bigint>;
16
+ forEach(callback: (value: bigint, key: bigint, map: FastBigIntMap) => void, thisArg?: unknown): void;
17
+ [Symbol.iterator](): IterableIterator<[bigint, bigint]>;
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1 +1 @@
1
- export declare const version = "1.7.1";
1
+ export declare const version = "1.7.7";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.7.1';
1
+ export const version = '1.7.7';
@@ -1,8 +1,8 @@
1
1
  import { Address } from '../keypair/Address.js';
2
- import { Map } from './Map.js';
2
+ import { FastMap } from './FastMap.js';
3
3
  export class AddressMap {
4
4
  constructor(iterable) {
5
- this.items = new Map();
5
+ this.items = new FastMap();
6
6
  if (iterable) {
7
7
  for (const [key, value] of iterable) {
8
8
  this.set(key, value);
@@ -1,11 +1,11 @@
1
- import { Map } from './Map.js';
2
- export declare class DeterministicMap<K, V> {
1
+ import { FastMap, PropertyExtendedKey } from './FastMap.js';
2
+ export declare class DeterministicMap<K extends PropertyExtendedKey, V> {
3
3
  #private;
4
4
  private compareFn;
5
5
  private map;
6
6
  constructor(compareFn: (a: K, b: K) => number);
7
7
  get size(): number;
8
- static fromMap<K, V>(map: Map<K, V>, compareFn: (a: K, b: K) => number): DeterministicMap<K, V>;
8
+ static fromMap<K extends PropertyExtendedKey, V>(map: FastMap<K, V>, compareFn: (a: K, b: K) => number): DeterministicMap<K, V>;
9
9
  set(key: K, value: V): void;
10
10
  get(key: K): V | undefined;
11
11
  entries(): IterableIterator<[K, V]>;
@@ -10,12 +10,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _DeterministicMap_keys;
13
- import { Map } from './Map.js';
13
+ import { FastMap } from './FastMap.js';
14
14
  export class DeterministicMap {
15
15
  constructor(compareFn) {
16
16
  this.compareFn = compareFn;
17
17
  _DeterministicMap_keys.set(this, void 0);
18
- this.map = new Map();
18
+ this.map = new FastMap();
19
19
  __classPrivateFieldSet(this, _DeterministicMap_keys, [], "f");
20
20
  }
21
21
  get size() {
@@ -0,0 +1,24 @@
1
+ export type PropertyExtendedKey = PropertyKey | bigint;
2
+ export type FastRecord<V> = {
3
+ [key: string]: V;
4
+ };
5
+ export type IndexKey = string | number;
6
+ export declare class FastMap<K extends PropertyExtendedKey, V> {
7
+ protected _keys: K[];
8
+ protected _values: FastRecord<V>;
9
+ constructor(iterable?: ReadonlyArray<readonly [K, V]> | null | FastMap<K, V>);
10
+ get size(): number;
11
+ setAll(map: FastMap<K, V>): void;
12
+ addAll(map: FastMap<K, V>): void;
13
+ keys(): IterableIterator<K>;
14
+ values(): IterableIterator<V>;
15
+ entries(): IterableIterator<[K, V]>;
16
+ set(key: K, value: V): this;
17
+ indexOf(key: K): number;
18
+ get(key: K): V | undefined;
19
+ has(key: K): boolean;
20
+ delete(key: K): boolean;
21
+ clear(): void;
22
+ forEach(callback: (value: V, key: K, map: FastMap<K, V>) => void, thisArg?: unknown): void;
23
+ [Symbol.iterator](): IterableIterator<[K, V]>;
24
+ }
@@ -0,0 +1,88 @@
1
+ export class FastMap {
2
+ constructor(iterable) {
3
+ this._keys = [];
4
+ this._values = {};
5
+ if (iterable instanceof FastMap) {
6
+ this.setAll(iterable);
7
+ }
8
+ else {
9
+ if (iterable) {
10
+ for (const [key, value] of iterable) {
11
+ this.set(key, value);
12
+ }
13
+ }
14
+ }
15
+ }
16
+ get size() {
17
+ return this._keys.length;
18
+ }
19
+ setAll(map) {
20
+ this._keys = [...map._keys];
21
+ this._values = { ...map._values };
22
+ }
23
+ addAll(map) {
24
+ for (const [key, value] of map.entries()) {
25
+ this.set(key, value);
26
+ }
27
+ }
28
+ *keys() {
29
+ yield* this._keys;
30
+ }
31
+ *values() {
32
+ for (const key of this._keys) {
33
+ yield this._values[key];
34
+ }
35
+ }
36
+ *entries() {
37
+ for (const key of this._keys) {
38
+ yield [key, this._values[key]];
39
+ }
40
+ }
41
+ set(key, value) {
42
+ if (!this.has(key)) {
43
+ this._keys.push(key);
44
+ }
45
+ this._values[key] = value;
46
+ return this;
47
+ }
48
+ indexOf(key) {
49
+ if (!this.has(key)) {
50
+ return -1;
51
+ }
52
+ for (let i = 0; i < this._keys.length; i++) {
53
+ if (this._keys[i] === key) {
54
+ return i;
55
+ }
56
+ }
57
+ throw new Error('Key not found, this should not happen.');
58
+ }
59
+ get(key) {
60
+ return this._values[key];
61
+ }
62
+ has(key) {
63
+ return Object.prototype.hasOwnProperty.call(this._values, key);
64
+ }
65
+ delete(key) {
66
+ if (!this.has(key)) {
67
+ return false;
68
+ }
69
+ const index = this.indexOf(key);
70
+ this._keys.splice(index, 1);
71
+ delete this._values[key];
72
+ return true;
73
+ }
74
+ clear() {
75
+ this._keys = [];
76
+ this._values = {};
77
+ }
78
+ forEach(callback, thisArg) {
79
+ for (const key of this._keys) {
80
+ callback.call(thisArg, this._values[key], key, this);
81
+ }
82
+ }
83
+ *[Symbol.iterator]() {
84
+ for (const key of this._keys) {
85
+ yield [key, this._values[key]];
86
+ }
87
+ }
88
+ }
package/build/opnet.d.ts CHANGED
@@ -75,7 +75,7 @@ export * from './metadata/tokens.js';
75
75
  export * from './transaction/browser/Web3Provider.js';
76
76
  export * from './keypair/Secp256k1PointDeriver.js';
77
77
  export * from './transaction/ContractAddress.js';
78
- export * from './deterministic/Map.js';
78
+ export * from './deterministic/FastMap.js';
79
79
  export * from './deterministic/CustomMap.js';
80
80
  declare global {
81
81
  interface Window {
package/build/opnet.js CHANGED
@@ -74,5 +74,5 @@ export * from './metadata/tokens.js';
74
74
  export * from './transaction/browser/Web3Provider.js';
75
75
  export * from './keypair/Secp256k1PointDeriver.js';
76
76
  export * from './transaction/ContractAddress.js';
77
- export * from './deterministic/Map.js';
77
+ export * from './deterministic/FastMap.js';
78
78
  export * from './deterministic/CustomMap.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.7.5",
4
+ "version": "1.7.7",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.7.1';
1
+ export const version = '1.7.7';
@@ -1,11 +1,11 @@
1
1
  import { Address } from '../keypair/Address.js';
2
- import { Map } from './Map.js';
2
+ import { FastMap } from './FastMap.js';
3
3
 
4
4
  export class AddressMap<V> {
5
- private items: Map<bigint, V>;
5
+ private items: FastMap<bigint, V>;
6
6
 
7
7
  constructor(iterable?: ReadonlyArray<readonly [Address, V]> | null) {
8
- this.items = new Map();
8
+ this.items = new FastMap();
9
9
 
10
10
  if (iterable) {
11
11
  for (const [key, value] of iterable) {
@@ -1,11 +1,11 @@
1
- import { Map } from './Map.js';
1
+ import { FastMap, PropertyExtendedKey } from './FastMap.js';
2
2
 
3
- export class DeterministicMap<K, V> {
4
- private map: Map<K, V>;
3
+ export class DeterministicMap<K extends PropertyExtendedKey, V> {
4
+ private map: FastMap<K, V>;
5
5
  #keys: K[];
6
6
 
7
7
  constructor(private compareFn: (a: K, b: K) => number) {
8
- this.map = new Map<K, V>();
8
+ this.map = new FastMap<K, V>();
9
9
  this.#keys = [];
10
10
  }
11
11
 
@@ -13,8 +13,8 @@ export class DeterministicMap<K, V> {
13
13
  return this.map.size;
14
14
  }
15
15
 
16
- public static fromMap<K, V>(
17
- map: Map<K, V>,
16
+ public static fromMap<K extends PropertyExtendedKey, V>(
17
+ map: FastMap<K, V>,
18
18
  compareFn: (a: K, b: K) => number,
19
19
  ): DeterministicMap<K, V> {
20
20
  const deterministicMap = new DeterministicMap<K, V>(compareFn);
@@ -29,6 +29,7 @@ export class DeterministicMap<K, V> {
29
29
  // Binary search for insertion position
30
30
  let left = 0,
31
31
  right = this.#keys.length;
32
+
32
33
  while (left < right) {
33
34
  const mid = Math.floor((left + right) / 2);
34
35
  if (this.compareFn(this.#keys[mid], key) < 0) {
@@ -39,6 +40,7 @@ export class DeterministicMap<K, V> {
39
40
  }
40
41
  this.#keys.splice(left, 0, key);
41
42
  }
43
+
42
44
  this.map.set(key, value);
43
45
  }
44
46
 
@@ -78,6 +80,7 @@ export class DeterministicMap<K, V> {
78
80
  // Binary search to find the key's index
79
81
  let left = 0,
80
82
  right = this.#keys.length - 1;
83
+
81
84
  while (left <= right) {
82
85
  const mid = Math.floor((left + right) / 2);
83
86
  const cmp = this.compareFn(this.#keys[mid], key);
@@ -0,0 +1,124 @@
1
+ export type PropertyExtendedKey = PropertyKey | bigint;
2
+
3
+ /**
4
+ * Like Record, but supports bigint keys (which JS auto-converts to strings).
5
+ * Reflects actual JavaScript behavior where obj[123n] becomes obj["123"].
6
+ */
7
+ export type FastRecord<V> = {
8
+ [key: string]: V;
9
+ };
10
+
11
+ export type IndexKey = string | number;
12
+
13
+ export class FastMap<K extends PropertyExtendedKey, V> {
14
+ protected _keys: K[] = [];
15
+ protected _values: FastRecord<V> = {};
16
+
17
+ constructor(iterable?: ReadonlyArray<readonly [K, V]> | null | FastMap<K, V>) {
18
+ if (iterable instanceof FastMap) {
19
+ this.setAll(iterable);
20
+ } else {
21
+ if (iterable) {
22
+ for (const [key, value] of iterable) {
23
+ this.set(key, value);
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ public get size(): number {
30
+ return this._keys.length;
31
+ }
32
+
33
+ public setAll(map: FastMap<K, V>): void {
34
+ this._keys = [...map._keys];
35
+ this._values = { ...map._values };
36
+ }
37
+
38
+ public addAll(map: FastMap<K, V>): void {
39
+ for (const [key, value] of map.entries()) {
40
+ this.set(key, value);
41
+ }
42
+ }
43
+
44
+ public *keys(): IterableIterator<K> {
45
+ yield* this._keys;
46
+ }
47
+
48
+ public *values(): IterableIterator<V> {
49
+ for (const key of this._keys) {
50
+ yield this._values[key as IndexKey];
51
+ }
52
+ }
53
+
54
+ public *entries(): IterableIterator<[K, V]> {
55
+ for (const key of this._keys) {
56
+ yield [key, this._values[key as IndexKey]];
57
+ }
58
+ }
59
+
60
+ public set(key: K, value: V): this {
61
+ if (!this.has(key)) {
62
+ this._keys.push(key);
63
+ }
64
+
65
+ this._values[key as IndexKey] = value;
66
+
67
+ return this;
68
+ }
69
+
70
+ public indexOf(key: K): number {
71
+ if (!this.has(key)) {
72
+ return -1;
73
+ }
74
+
75
+ for (let i = 0; i < this._keys.length; i++) {
76
+ if (this._keys[i] === key) {
77
+ return i;
78
+ }
79
+ }
80
+
81
+ throw new Error('Key not found, this should not happen.');
82
+ }
83
+
84
+ public get(key: K): V | undefined {
85
+ return this._values[key as IndexKey];
86
+ }
87
+
88
+ public has(key: K): boolean {
89
+ return Object.prototype.hasOwnProperty.call(this._values, key as IndexKey);
90
+ }
91
+
92
+ public delete(key: K): boolean {
93
+ if (!this.has(key)) {
94
+ return false;
95
+ }
96
+
97
+ const index = this.indexOf(key);
98
+ this._keys.splice(index, 1);
99
+
100
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
101
+ delete this._values[key as IndexKey];
102
+ return true;
103
+ }
104
+
105
+ public clear(): void {
106
+ this._keys = [];
107
+ this._values = {};
108
+ }
109
+
110
+ public forEach(
111
+ callback: (value: V, key: K, map: FastMap<K, V>) => void,
112
+ thisArg?: unknown,
113
+ ): void {
114
+ for (const key of this._keys) {
115
+ callback.call(thisArg, this._values[key as IndexKey], key, this);
116
+ }
117
+ }
118
+
119
+ *[Symbol.iterator](): IterableIterator<[K, V]> {
120
+ for (const key of this._keys) {
121
+ yield [key, this._values[key as IndexKey]];
122
+ }
123
+ }
124
+ }
package/src/opnet.ts CHANGED
@@ -125,7 +125,7 @@ export * from './transaction/browser/Web3Provider.js';
125
125
  export * from './keypair/Secp256k1PointDeriver.js';
126
126
  export * from './transaction/ContractAddress.js';
127
127
 
128
- export * from './deterministic/Map.js';
128
+ export * from './deterministic/FastMap.js';
129
129
  export * from './deterministic/CustomMap.js';
130
130
 
131
131
  declare global {
@@ -0,0 +1,143 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { FastBigIntMap } from './old/FastBigIntMap.js';
3
+ import { FastMap } from '../src';
4
+
5
+ describe('setAll bug', () => {
6
+ describe('FastBigIntMap (original - correct behavior)', () => {
7
+ it('should replace all entries, not merge', () => {
8
+ const map1 = new FastBigIntMap([
9
+ [1n, 100n],
10
+ [2n, 200n],
11
+ ]);
12
+
13
+ const map2 = new FastBigIntMap([
14
+ [3n, 300n],
15
+ [4n, 400n],
16
+ ]);
17
+
18
+ // setAll should REPLACE, not merge
19
+ map2.setAll(map1);
20
+
21
+ // Should have entries from map1 only
22
+ expect(map2.size).toBe(2);
23
+ expect(map2.get(1n)).toBe(100n);
24
+ expect(map2.get(2n)).toBe(200n);
25
+
26
+ // Old entries should be GONE
27
+ expect(map2.has(3n)).toBe(false);
28
+ expect(map2.has(4n)).toBe(false);
29
+ expect(map2.get(3n)).toBeUndefined();
30
+ expect(map2.get(4n)).toBeUndefined();
31
+ });
32
+ });
33
+
34
+ describe('FastMap (replacement - should match original behavior)', () => {
35
+ it('should replace all entries, not merge', () => {
36
+ const map1 = new FastMap<bigint, bigint>([
37
+ [1n, 100n],
38
+ [2n, 200n],
39
+ ]);
40
+
41
+ const map2 = new FastMap<bigint, bigint>([
42
+ [3n, 300n],
43
+ [4n, 400n],
44
+ ]);
45
+
46
+ // setAll should REPLACE, not merge
47
+ map2.setAll(map1);
48
+
49
+ // Should have entries from map1 only
50
+ expect(map2.size).toBe(2);
51
+ expect(map2.get(1n)).toBe(100n);
52
+ expect(map2.get(2n)).toBe(200n);
53
+
54
+ // Old entries should be GONE
55
+ expect(map2.has(3n)).toBe(false);
56
+ expect(map2.has(4n)).toBe(false);
57
+ expect(map2.get(3n)).toBeUndefined();
58
+ expect(map2.get(4n)).toBeUndefined();
59
+ });
60
+
61
+ it('should not leak old keys in iteration', () => {
62
+ const map1 = new FastMap<bigint, bigint>([[1n, 100n]]);
63
+
64
+ const map2 = new FastMap<bigint, bigint>([
65
+ [2n, 200n],
66
+ [3n, 300n],
67
+ ]);
68
+
69
+ map2.setAll(map1);
70
+
71
+ const keys = [...map2.keys()];
72
+ const values = [...map2.values()];
73
+ const entries = [...map2.entries()];
74
+
75
+ expect(keys).toEqual([1n]);
76
+ expect(values).toEqual([100n]);
77
+ expect(entries).toEqual([[1n, 100n]]);
78
+ });
79
+
80
+ it('should handle overlapping keys correctly', () => {
81
+ const map1 = new FastMap<bigint, bigint>([
82
+ [1n, 100n],
83
+ [2n, 200n],
84
+ ]);
85
+
86
+ const map2 = new FastMap<bigint, bigint>([
87
+ [2n, 999n], // overlapping key
88
+ [3n, 300n],
89
+ ]);
90
+
91
+ map2.setAll(map1);
92
+
93
+ // Should have map1's values, including overwriting the overlapping key
94
+ expect(map2.size).toBe(2);
95
+ expect(map2.get(1n)).toBe(100n);
96
+ expect(map2.get(2n)).toBe(200n); // map1's value, not 999n
97
+
98
+ // 3n should be gone
99
+ expect(map2.has(3n)).toBe(false);
100
+ });
101
+ });
102
+
103
+ describe('behavior parity', () => {
104
+ it('FastMap.setAll should behave identically to FastBigIntMap.setAll', () => {
105
+ // Setup identical scenarios
106
+ const oldMap1 = new FastBigIntMap([
107
+ [1n, 10n],
108
+ [2n, 20n],
109
+ ]);
110
+ const oldMap2 = new FastBigIntMap([
111
+ [3n, 30n],
112
+ [4n, 40n],
113
+ [5n, 50n],
114
+ ]);
115
+
116
+ const newMap1 = new FastMap<bigint, bigint>([
117
+ [1n, 10n],
118
+ [2n, 20n],
119
+ ]);
120
+ const newMap2 = new FastMap<bigint, bigint>([
121
+ [3n, 30n],
122
+ [4n, 40n],
123
+ [5n, 50n],
124
+ ]);
125
+
126
+ // Perform setAll on both
127
+ oldMap2.setAll(oldMap1);
128
+ newMap2.setAll(newMap1);
129
+
130
+ // Compare sizes
131
+ expect(newMap2.size).toBe(oldMap2.size);
132
+
133
+ // Compare keys
134
+ expect([...newMap2.keys()]).toEqual([...oldMap2.keys()]);
135
+
136
+ // Compare values
137
+ expect([...newMap2.values()]).toEqual([...oldMap2.values()]);
138
+
139
+ // Compare entries
140
+ expect([...newMap2.entries()]).toEqual([...oldMap2.entries()]);
141
+ });
142
+ });
143
+ });