@psy-protocol/utils 1.0.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.
- package/dist/EventHub.d.ts +52 -0
- package/dist/EventHub.d.ts.map +1 -0
- package/dist/EventHub.js +150 -0
- package/dist/data/array.d.ts +18 -0
- package/dist/data/array.d.ts.map +1 -0
- package/dist/data/array.js +109 -0
- package/dist/debounce.d.ts +8 -0
- package/dist/debounce.d.ts.map +1 -0
- package/dist/debounce.js +80 -0
- package/dist/hash/index.d.ts +3 -0
- package/dist/hash/index.d.ts.map +1 -0
- package/dist/hash/index.js +2 -0
- package/dist/hash/ripemd160.d.ts +24 -0
- package/dist/hash/ripemd160.d.ts.map +1 -0
- package/dist/hash/ripemd160.js +186 -0
- package/dist/hash/sha256.d.ts +30 -0
- package/dist/hash/sha256.d.ts.map +1 -0
- package/dist/hash/sha256.js +331 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/object.d.ts +4 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +20 -0
- package/dist/seedrandom.d.ts +9 -0
- package/dist/seedrandom.d.ts.map +1 -0
- package/dist/seedrandom.js +175 -0
- package/dist/uuid.d.ts +3 -0
- package/dist/uuid.d.ts.map +1 -0
- package/dist/uuid.js +2 -0
- package/package.json +23 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
interface IBaseEvent<T> {
|
|
2
|
+
type: T;
|
|
3
|
+
}
|
|
4
|
+
type IndexableType = string | number | symbol;
|
|
5
|
+
type TEventListenerMap<T extends IndexableType, TEvents extends IBaseEvent<T>> = {
|
|
6
|
+
[K in T]?: ((event: TEvents & {
|
|
7
|
+
type: K;
|
|
8
|
+
}) => any)[];
|
|
9
|
+
};
|
|
10
|
+
declare class EventHub<TEventType extends IndexableType, TEvents extends IBaseEvent<TEventType>> {
|
|
11
|
+
eventListeners: TEventListenerMap<TEventType, TEvents>;
|
|
12
|
+
addEventListener<T extends TEventType>(type: T, listener: (event: TEvents & {
|
|
13
|
+
type: T;
|
|
14
|
+
}) => any): boolean;
|
|
15
|
+
on<T extends TEventType>(type: T, listener: (event: TEvents & {
|
|
16
|
+
type: T;
|
|
17
|
+
}) => any): boolean;
|
|
18
|
+
onOneOf(types: TEventType[], listener: (event: TEvents) => any): boolean;
|
|
19
|
+
removeOneOf(types: TEventType[], listener: (event: TEvents) => any): boolean;
|
|
20
|
+
remove<T extends TEventType>(type: T, listener: (event: TEvents & {
|
|
21
|
+
type: T;
|
|
22
|
+
}) => any): boolean;
|
|
23
|
+
once<T extends TEventType>(type: T, listener: (event: TEvents & {
|
|
24
|
+
type: T;
|
|
25
|
+
}) => any): () => boolean;
|
|
26
|
+
onceFilter<T extends TEventType>(type: T, filter: (event: TEvents & {
|
|
27
|
+
type: T;
|
|
28
|
+
}) => boolean, listener: (event: TEvents & {
|
|
29
|
+
type: T;
|
|
30
|
+
}) => any): () => boolean;
|
|
31
|
+
removeEventListener<T extends TEventType>(type: T, listener: (event: TEvents & {
|
|
32
|
+
type: T;
|
|
33
|
+
}) => any): boolean;
|
|
34
|
+
removeAllEventListeners<T extends TEventType>(type: T): void;
|
|
35
|
+
private notifyWithErrorsInternal;
|
|
36
|
+
notifyWithErrors<T extends TEventType>(type: T, event: Omit<TEvents & {
|
|
37
|
+
type: T;
|
|
38
|
+
}, "type">): any[];
|
|
39
|
+
notifyWithErrors<T extends TEventType>(event: TEvents & {
|
|
40
|
+
type: T;
|
|
41
|
+
}): any[];
|
|
42
|
+
notify<T extends TEventType>(type: T, event: Omit<TEvents & {
|
|
43
|
+
type: T;
|
|
44
|
+
}, "type">): void;
|
|
45
|
+
notify<T extends TEventType>(event: TEvents & {
|
|
46
|
+
type: T;
|
|
47
|
+
}): void;
|
|
48
|
+
private notifyInternal;
|
|
49
|
+
}
|
|
50
|
+
export type { IBaseEvent, IndexableType, TEventListenerMap };
|
|
51
|
+
export { EventHub };
|
|
52
|
+
//# sourceMappingURL=EventHub.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventHub.d.ts","sourceRoot":"","sources":["../src/EventHub.ts"],"names":[],"mappings":"AAAA,UAAU,UAAU,CAAC,CAAC;IAClB,IAAI,EAAE,CAAC,CAAC;CACX;AACD,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAE9C,KAAK,iBAAiB,CAAC,CAAC,SAAS,aAAa,EAAE,OAAO,SAAS,UAAU,CAAC,CAAC,CAAC,IAAI;KAC5E,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,CAAC,EAAE;CACvD,CAAC;AAEF,cAAM,QAAQ,CAAC,UAAU,SAAS,aAAa,EAAE,OAAO,SAAS,UAAU,CAAC,UAAU,CAAC;IACnF,cAAc,EAAE,iBAAiB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAM;IAC5D,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,GAAG,OAAO;IAqBzG,EAAE,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,GAAG,OAAO;IAG3F,OAAO,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,GAAG,GAAG,OAAO;IAMxE,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,GAAG,GAAG,OAAO;IAM5E,MAAM,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,GAAG,OAAO;IAG/F,IAAI,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,GAAG,MAAM,OAAO;IAanG,UAAU,CAAC,CAAC,SAAS,UAAU,EAC3B,IAAI,EAAE,CAAC,EACP,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,OAAO,EACjD,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,GAChD,MAAM,OAAO;IAehB,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,KAAK,GAAG,GAAG,OAAO;IAsB5G,uBAAuB,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC;IAIrD,OAAO,CAAC,wBAAwB;IAoBhC,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,EAAE,MAAM,CAAC,GAAG,GAAG,EAAE;IAClG,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,GAAG,GAAG,EAAE;IAY3E,MAAM,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,EAAE,MAAM,CAAC,GAAG,IAAI;IACvF,MAAM,CAAC,CAAC,SAAS,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,GAAG,IAAI;IAWhE,OAAO,CAAC,cAAc;CAgBzB;AAED,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,iBAAiB,EAAE,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
package/dist/EventHub.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
class EventHub {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.eventListeners = {};
|
|
4
|
+
}
|
|
5
|
+
addEventListener(type, listener) {
|
|
6
|
+
if (typeof listener !== "function") {
|
|
7
|
+
throw new Error("you cannot add an event listener that is not a function");
|
|
8
|
+
}
|
|
9
|
+
const eventListenerList = this.eventListeners[type];
|
|
10
|
+
if (Object.hasOwnProperty.call(this.eventListeners, type) &&
|
|
11
|
+
eventListenerList &&
|
|
12
|
+
Array.isArray(eventListenerList)) {
|
|
13
|
+
if (eventListenerList.indexOf(listener) === -1) {
|
|
14
|
+
eventListenerList.push(listener);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.eventListeners[type] = [listener];
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
on(type, listener) {
|
|
27
|
+
return this.addEventListener(type, listener);
|
|
28
|
+
}
|
|
29
|
+
onOneOf(types, listener) {
|
|
30
|
+
for (const type of types) {
|
|
31
|
+
this.addEventListener(type, listener);
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
removeOneOf(types, listener) {
|
|
36
|
+
for (const type of types) {
|
|
37
|
+
this.removeEventListener(type, listener);
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
remove(type, listener) {
|
|
42
|
+
return this.removeEventListener(type, listener);
|
|
43
|
+
}
|
|
44
|
+
once(type, listener) {
|
|
45
|
+
let called = false;
|
|
46
|
+
const realListener = (event) => {
|
|
47
|
+
if (called) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
called = true;
|
|
51
|
+
this.removeEventListener(type, realListener);
|
|
52
|
+
listener(event);
|
|
53
|
+
};
|
|
54
|
+
this.addEventListener(type, realListener);
|
|
55
|
+
return () => this.removeEventListener(type, realListener);
|
|
56
|
+
}
|
|
57
|
+
onceFilter(type, filter, listener) {
|
|
58
|
+
let called = false;
|
|
59
|
+
const realListener = (event) => {
|
|
60
|
+
if (called) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (filter(event)) {
|
|
64
|
+
called = true;
|
|
65
|
+
this.removeEventListener(type, realListener);
|
|
66
|
+
listener(event);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
this.addEventListener(type, realListener);
|
|
70
|
+
return () => this.removeEventListener(type, realListener);
|
|
71
|
+
}
|
|
72
|
+
removeEventListener(type, listener) {
|
|
73
|
+
if (Object.hasOwnProperty.call(this.eventListeners, type) && Array.isArray(this.eventListeners[type])) {
|
|
74
|
+
const listeners = this.eventListeners[type];
|
|
75
|
+
if (!listeners) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const index = listeners.indexOf(listener);
|
|
79
|
+
if (index !== -1) {
|
|
80
|
+
if (listeners.length === 0) {
|
|
81
|
+
this.eventListeners[type] = [];
|
|
82
|
+
delete this.eventListeners[type];
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.eventListeners[type] = listeners.slice(0, index).concat(listeners.slice(index + 1));
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
removeAllEventListeners(type) {
|
|
98
|
+
this.eventListeners[type] = [];
|
|
99
|
+
delete this.eventListeners[type];
|
|
100
|
+
}
|
|
101
|
+
notifyWithErrorsInternal(event) {
|
|
102
|
+
const errors = [];
|
|
103
|
+
if (Object.hasOwnProperty.call(this.eventListeners, event.type) &&
|
|
104
|
+
Array.isArray(this.eventListeners[event.type]) &&
|
|
105
|
+
this.eventListeners[event.type].length) {
|
|
106
|
+
const listeners = this.eventListeners[event.type];
|
|
107
|
+
for (const listener of listeners) {
|
|
108
|
+
try {
|
|
109
|
+
listener(event);
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
errors.push(err);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return errors;
|
|
117
|
+
}
|
|
118
|
+
notifyWithErrors(eventOrType, event) {
|
|
119
|
+
if (typeof event === "object" && typeof eventOrType !== "object") {
|
|
120
|
+
return this.notifyWithErrorsInternal({ type: eventOrType, ...event });
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return this.notifyWithErrorsInternal(eventOrType);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
notify(eventOrType, event) {
|
|
127
|
+
if (typeof event === "object" && typeof eventOrType !== "object") {
|
|
128
|
+
this.notifyInternal({ type: eventOrType, ...event });
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
this.notifyInternal(eventOrType);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
notifyInternal(event) {
|
|
135
|
+
if (Object.hasOwnProperty.call(this.eventListeners, event.type) &&
|
|
136
|
+
Array.isArray(this.eventListeners[event.type]) &&
|
|
137
|
+
this.eventListeners[event.type].length) {
|
|
138
|
+
const listeners = this.eventListeners[event.type];
|
|
139
|
+
for (const listener of listeners) {
|
|
140
|
+
try {
|
|
141
|
+
listener(event);
|
|
142
|
+
}
|
|
143
|
+
catch (err) {
|
|
144
|
+
// do nothing
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export { EventHub };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare function u8ArrayToHex(x: Uint8Array | number[]): string;
|
|
2
|
+
declare function hexToU8Array(hex: string): Uint8Array;
|
|
3
|
+
declare function swapEndianU32(x: number): number;
|
|
4
|
+
declare function swapEndianU16(x: number): number;
|
|
5
|
+
declare function u32ArrayToHex(x: Uint32Array | (number | bigint | string)[]): string;
|
|
6
|
+
declare function hexToU32Array(hex: string, bigEndian?: boolean): Uint32Array;
|
|
7
|
+
declare function u16ArrayToHex(x: Uint16Array | (number | bigint | string)[]): string;
|
|
8
|
+
declare function hexToU16Array(hex: string, bigEndian?: boolean): Uint16Array;
|
|
9
|
+
declare function swapEndianU32Array(x: Uint32Array): Uint32Array;
|
|
10
|
+
declare function swapEndianU32Array(x: number[]): number[];
|
|
11
|
+
declare function swapEndianU16Array(x: Uint16Array): Uint16Array;
|
|
12
|
+
declare function swapEndianU16Array(x: number[]): number[];
|
|
13
|
+
declare function isZeroedArray(x: Uint8Array | number[]): boolean;
|
|
14
|
+
declare function isDataValidASCII(array: Uint8Array | number[]): boolean;
|
|
15
|
+
declare function seq(count: number, startIndex?: number, reversed?: number | boolean): number[];
|
|
16
|
+
declare function rseq(count: number, largestIndex?: number): number[];
|
|
17
|
+
export { u8ArrayToHex, hexToU8Array, u32ArrayToHex, hexToU32Array, u16ArrayToHex, hexToU16Array, isZeroedArray, swapEndianU32Array, swapEndianU32, swapEndianU16Array, swapEndianU16, isDataValidASCII, seq, rseq, };
|
|
18
|
+
//# sourceMappingURL=array.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/data/array.ts"],"names":[],"mappings":"AAAA,iBAAS,YAAY,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,EAAE,GAAG,MAAM,CAMtD;AAED,iBAAS,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAW7C;AAED,iBAAS,aAAa,CAAC,CAAC,EAAE,MAAM,UAE/B;AAED,iBAAS,aAAa,CAAC,CAAC,EAAE,MAAM,UAE/B;AAED,iBAAS,aAAa,CAAC,CAAC,EAAE,WAAW,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,MAAM,CAU5E;AACD,iBAAS,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,UAAQ,GAAG,WAAW,CAOlE;AAED,iBAAS,aAAa,CAAC,CAAC,EAAE,WAAW,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,MAAM,CAQ5E;AAED,iBAAS,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,UAAQ,GAAG,WAAW,CAOlE;AAED,iBAAS,kBAAkB,CAAC,CAAC,EAAE,WAAW,GAAG,WAAW,CAAC;AACzD,iBAAS,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;AAQnD,iBAAS,kBAAkB,CAAC,CAAC,EAAE,WAAW,GAAG,WAAW,CAAC;AACzD,iBAAS,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;AAQnD,iBAAS,aAAa,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,EAAE,WAO9C;AAED,iBAAS,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,EAAE,GAAG,OAAO,CAO/D;AAED,iBAAS,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,SAAI,EAAE,QAAQ,GAAE,MAAM,GAAG,OAAe,GAAG,MAAM,EAAE,CAYxF;AAED,iBAAS,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAM5D;AAED,OAAO,EACH,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,aAAa,EACb,aAAa,EACb,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,aAAa,EACb,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,GAAG,EACH,IAAI,GACP,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
function u8ArrayToHex(x) {
|
|
2
|
+
const output = [];
|
|
3
|
+
for (let i = 0, l = x.length; i < l; i++) {
|
|
4
|
+
output[i] = x[i] < 0x10 ? "0" + x[i].toString(16) : x[i].toString(16);
|
|
5
|
+
}
|
|
6
|
+
return output.join("");
|
|
7
|
+
}
|
|
8
|
+
function hexToU8Array(hex) {
|
|
9
|
+
const hexString = hex.charAt(1) === "x" ? hex.substring(2) : hex;
|
|
10
|
+
if (hexString.length % 2 === 1) {
|
|
11
|
+
throw new Error("hex strings must have an even number of characters");
|
|
12
|
+
}
|
|
13
|
+
const output = new Uint8Array(hexString.length / 2);
|
|
14
|
+
for (let i = 0, l = hexString.length / 2; i < l; i++) {
|
|
15
|
+
output[i] = parseInt(hexString.substring(i * 2, i * 2 + 2), 16);
|
|
16
|
+
}
|
|
17
|
+
return output;
|
|
18
|
+
}
|
|
19
|
+
function swapEndianU32(x) {
|
|
20
|
+
return (((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >>> 8) | (x >>> 24)) >>> 0;
|
|
21
|
+
}
|
|
22
|
+
function swapEndianU16(x) {
|
|
23
|
+
return ((x >>> 8) | ((x & 0xff) << 8)) >>> 0;
|
|
24
|
+
}
|
|
25
|
+
function u32ArrayToHex(x) {
|
|
26
|
+
if (x instanceof Uint32Array) {
|
|
27
|
+
return u8ArrayToHex(new Uint8Array(x.buffer));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return u8ArrayToHex(new Uint8Array(new Uint32Array(x.map((x) => Number(x)).map((x) => (x < 0 ? x >>> 0 : x) & 0xffffffff)).buffer));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function hexToU32Array(hex, bigEndian = false) {
|
|
34
|
+
const r = new Uint32Array(hexToU8Array(hex).buffer);
|
|
35
|
+
if (bigEndian) {
|
|
36
|
+
return swapEndianU32Array(r);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return r;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function u16ArrayToHex(x) {
|
|
43
|
+
if (x instanceof Uint16Array) {
|
|
44
|
+
return u8ArrayToHex(new Uint8Array(x.buffer));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return u8ArrayToHex(new Uint8Array(new Uint16Array(x.map((x) => Number(x)).map((x) => (x < 0 ? x >>> 0 : x) & 0xffff)).buffer));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function hexToU16Array(hex, bigEndian = false) {
|
|
51
|
+
const r = new Uint16Array(hexToU8Array(hex).buffer);
|
|
52
|
+
if (bigEndian) {
|
|
53
|
+
return swapEndianU16Array(r);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return r;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function swapEndianU32Array(x) {
|
|
60
|
+
for (let i = 0; i < x.length; i++) {
|
|
61
|
+
x[i] = swapEndianU32(x[i]);
|
|
62
|
+
}
|
|
63
|
+
return x;
|
|
64
|
+
}
|
|
65
|
+
function swapEndianU16Array(x) {
|
|
66
|
+
for (let i = 0; i < x.length; i++) {
|
|
67
|
+
x[i] = swapEndianU16(x[i]);
|
|
68
|
+
}
|
|
69
|
+
return x;
|
|
70
|
+
}
|
|
71
|
+
function isZeroedArray(x) {
|
|
72
|
+
for (let i = 0; i < x.length; i++) {
|
|
73
|
+
if (x[i] !== 0) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
function isDataValidASCII(array) {
|
|
80
|
+
for (let i = 0; i < array.length; i++) {
|
|
81
|
+
if (array[i] < 0 || array[i] > 0x7f) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
function seq(count, startIndex = 0, reversed = false) {
|
|
88
|
+
const arr = [];
|
|
89
|
+
if (reversed) {
|
|
90
|
+
for (let i = 0; i < count; i++) {
|
|
91
|
+
arr[i] = startIndex + count - 1 - i;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
for (let i = 0; i < count; i++) {
|
|
96
|
+
arr[i] = startIndex + i;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return arr;
|
|
100
|
+
}
|
|
101
|
+
function rseq(count, largestIndex) {
|
|
102
|
+
if (typeof largestIndex !== "number") {
|
|
103
|
+
return seq(count, 0, true);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return seq(count, largestIndex - count + 1, true);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export { u8ArrayToHex, hexToU8Array, u32ArrayToHex, hexToU32Array, u16ArrayToHex, hexToU16Array, isZeroedArray, swapEndianU32Array, swapEndianU32, swapEndianU16Array, swapEndianU16, isDataValidASCII, seq, rseq, };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare function debounce(function_: any, wait?: number, options?: any): {
|
|
2
|
+
(...arguments_: any): any;
|
|
3
|
+
clear(): void;
|
|
4
|
+
flush(): void;
|
|
5
|
+
};
|
|
6
|
+
declare function debouncePromise<T, U extends unknown[]>(generator: (...args: U) => Promise<T>, wait?: number, options?: any): (...args: U) => any;
|
|
7
|
+
export { debounce, debouncePromise };
|
|
8
|
+
//# sourceMappingURL=debounce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../src/debounce.ts"],"names":[],"mappings":"AAAA,iBAAS,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,SAAM,EAAE,OAAO,GAAE,GAAQ;oBAoChB,GAAG;;;EAqDjD;AAED,iBAAS,eAAe,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAC3C,SAAS,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EACrC,IAAI,CAAC,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,GAAG,GACd,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,GAAG,CAKrB;AACD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC"}
|
package/dist/debounce.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
function debounce(function_, wait = 100, options = {}) {
|
|
2
|
+
if (typeof function_ !== "function") {
|
|
3
|
+
throw new TypeError(`Expected the first parameter to be a function, got \`${typeof function_}\`.`);
|
|
4
|
+
}
|
|
5
|
+
if (wait < 0) {
|
|
6
|
+
throw new RangeError("`wait` must not be negative.");
|
|
7
|
+
}
|
|
8
|
+
// TODO: Deprecate the boolean parameter at some point.
|
|
9
|
+
const { immediate } = typeof options === "boolean" ? { immediate: options } : options;
|
|
10
|
+
let storedContext;
|
|
11
|
+
let storedArguments;
|
|
12
|
+
let timeoutId;
|
|
13
|
+
let timestamp;
|
|
14
|
+
let result;
|
|
15
|
+
function later() {
|
|
16
|
+
const last = Date.now() - timestamp;
|
|
17
|
+
if (last < wait && last >= 0) {
|
|
18
|
+
timeoutId = setTimeout(later, wait - last);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
timeoutId = undefined;
|
|
22
|
+
if (!immediate) {
|
|
23
|
+
const callContext = storedContext;
|
|
24
|
+
const callArguments = storedArguments;
|
|
25
|
+
storedContext = undefined;
|
|
26
|
+
storedArguments = undefined;
|
|
27
|
+
result = function_.apply(callContext, callArguments);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const debounced = function (...arguments_) {
|
|
32
|
+
//@ts-ignore
|
|
33
|
+
if (storedContext && this !== storedContext) {
|
|
34
|
+
throw new Error("Debounced method called with different contexts.");
|
|
35
|
+
}
|
|
36
|
+
//@ts-ignore
|
|
37
|
+
storedContext = this; // eslint-disable-line unicorn/no-this-assignment
|
|
38
|
+
storedArguments = arguments_;
|
|
39
|
+
timestamp = Date.now();
|
|
40
|
+
const callNow = immediate && !timeoutId;
|
|
41
|
+
if (!timeoutId) {
|
|
42
|
+
timeoutId = setTimeout(later, wait);
|
|
43
|
+
}
|
|
44
|
+
if (callNow) {
|
|
45
|
+
const callContext = storedContext;
|
|
46
|
+
const callArguments = storedArguments;
|
|
47
|
+
storedContext = undefined;
|
|
48
|
+
storedArguments = undefined;
|
|
49
|
+
result = function_.apply(callContext, callArguments);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
debounced.clear = () => {
|
|
54
|
+
if (!timeoutId) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
clearTimeout(timeoutId);
|
|
58
|
+
timeoutId = undefined;
|
|
59
|
+
};
|
|
60
|
+
debounced.flush = () => {
|
|
61
|
+
if (!timeoutId) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const callContext = storedContext;
|
|
65
|
+
const callArguments = storedArguments;
|
|
66
|
+
storedContext = undefined;
|
|
67
|
+
storedArguments = undefined;
|
|
68
|
+
result = function_.apply(callContext, callArguments);
|
|
69
|
+
clearTimeout(timeoutId);
|
|
70
|
+
timeoutId = undefined;
|
|
71
|
+
};
|
|
72
|
+
return debounced;
|
|
73
|
+
}
|
|
74
|
+
function debouncePromise(generator, wait, options) {
|
|
75
|
+
const fnc = (...args) => {
|
|
76
|
+
generator(...args).catch(console.error);
|
|
77
|
+
};
|
|
78
|
+
return debounce(fnc, wait, options);
|
|
79
|
+
}
|
|
80
|
+
export { debounce, debouncePromise };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hash/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*! noble-ripemd160 - MIT License (c) Paul Miller (paulmillr.com) */
|
|
2
|
+
export declare class RIPEMD160 {
|
|
3
|
+
private h0;
|
|
4
|
+
private h1;
|
|
5
|
+
private h2;
|
|
6
|
+
private h3;
|
|
7
|
+
private h4;
|
|
8
|
+
private block;
|
|
9
|
+
private blockSize;
|
|
10
|
+
private offset;
|
|
11
|
+
private length;
|
|
12
|
+
private finalized;
|
|
13
|
+
private view;
|
|
14
|
+
constructor();
|
|
15
|
+
update(data: Uint8Array): this;
|
|
16
|
+
private compress;
|
|
17
|
+
digest(): Uint8Array;
|
|
18
|
+
}
|
|
19
|
+
declare function ripemd160(message: Uint8Array): Uint8Array;
|
|
20
|
+
declare function ripemd160(message: string): string;
|
|
21
|
+
export declare function ripemd160Buffer(data: Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | BigUint64Array | BigInt64Array | ArrayBuffer, encoding?: string): Uint8Array | string;
|
|
22
|
+
export declare function ripemd160Hex(hexString: string, encoding?: string): string | Uint8Array;
|
|
23
|
+
export { ripemd160 };
|
|
24
|
+
//# sourceMappingURL=ripemd160.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ripemd160.d.ts","sourceRoot":"","sources":["../../src/hash/ripemd160.ts"],"names":[],"mappings":"AAAA,oEAAoE;AA2DpE,qBAAa,SAAS;IAClB,OAAO,CAAC,EAAE,CAAkB;IAC5B,OAAO,CAAC,EAAE,CAAkB;IAC5B,OAAO,CAAC,EAAE,CAAkB;IAC5B,OAAO,CAAC,EAAE,CAAkB;IAC5B,OAAO,CAAC,EAAE,CAAkB;IAC5B,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,SAAS,CAAM;IACvB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,IAAI,CAAW;;IAMvB,MAAM,CAAC,IAAI,EAAE,UAAU;IAyBvB,OAAO,CAAC,QAAQ;IAyChB,MAAM,IAAI,UAAU;CAkCvB;AAYD,iBAAS,SAAS,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU,CAAC;AACpD,iBAAS,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;AAQ5C,wBAAgB,eAAe,CAC3B,IAAI,EACE,UAAU,GACV,SAAS,GACT,WAAW,GACX,UAAU,GACV,WAAW,GACX,UAAU,GACV,cAAc,GACd,aAAa,GACb,WAAW,EACjB,QAAQ,CAAC,EAAE,MAAM,GAClB,UAAU,GAAG,MAAM,CAUrB;AACD,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,uBAEhE;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/*! noble-ripemd160 - MIT License (c) Paul Miller (paulmillr.com) */
|
|
2
|
+
import { hexToU8Array } from "../data/array";
|
|
3
|
+
// https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
|
4
|
+
// https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
|
|
5
|
+
// prettier-ignore
|
|
6
|
+
const rl = Uint8Array.from([
|
|
7
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
8
|
+
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
|
|
9
|
+
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
|
|
10
|
+
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
|
|
11
|
+
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
|
|
12
|
+
]);
|
|
13
|
+
// prettier-ignore
|
|
14
|
+
const rr = Uint8Array.from([
|
|
15
|
+
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
|
|
16
|
+
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
|
|
17
|
+
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
|
|
18
|
+
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
|
|
19
|
+
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
|
|
20
|
+
]);
|
|
21
|
+
// prettier-ignore
|
|
22
|
+
const sl = Uint8Array.from([
|
|
23
|
+
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
|
|
24
|
+
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
|
|
25
|
+
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
|
|
26
|
+
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
|
|
27
|
+
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
|
|
28
|
+
]);
|
|
29
|
+
// prettier-ignore
|
|
30
|
+
const sr = Uint8Array.from([
|
|
31
|
+
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
|
|
32
|
+
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
|
|
33
|
+
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
|
|
34
|
+
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
|
|
35
|
+
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
|
|
36
|
+
]);
|
|
37
|
+
const Kl = Uint32Array.from([0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e]);
|
|
38
|
+
const Kr = Uint32Array.from([0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000]);
|
|
39
|
+
function rotl(x, n) {
|
|
40
|
+
return (x << n) | (x >>> (32 - n));
|
|
41
|
+
}
|
|
42
|
+
// It's called f() in spec.
|
|
43
|
+
function f(group, x, y, z) {
|
|
44
|
+
if (group === 0)
|
|
45
|
+
return x ^ y ^ z;
|
|
46
|
+
else if (group === 1)
|
|
47
|
+
return (x & y) | (~x & z);
|
|
48
|
+
else if (group === 2)
|
|
49
|
+
return (x | ~y) ^ z;
|
|
50
|
+
else if (group === 3)
|
|
51
|
+
return (x & z) | (y & ~z);
|
|
52
|
+
else
|
|
53
|
+
return x ^ (y | ~z);
|
|
54
|
+
}
|
|
55
|
+
export class RIPEMD160 {
|
|
56
|
+
constructor() {
|
|
57
|
+
this.h0 = 0x67452301 | 0;
|
|
58
|
+
this.h1 = 0xefcdab89 | 0;
|
|
59
|
+
this.h2 = 0x98badcfe | 0;
|
|
60
|
+
this.h3 = 0x10325476 | 0;
|
|
61
|
+
this.h4 = 0xc3d2e1f0 | 0;
|
|
62
|
+
this.block = new Uint8Array(64);
|
|
63
|
+
this.blockSize = 64;
|
|
64
|
+
this.offset = 0;
|
|
65
|
+
this.length = new Uint32Array(4);
|
|
66
|
+
this.finalized = false;
|
|
67
|
+
this.view = new DataView(this.block.buffer);
|
|
68
|
+
}
|
|
69
|
+
update(data) {
|
|
70
|
+
if (this.finalized)
|
|
71
|
+
throw new Error("Digest already called");
|
|
72
|
+
// consume data
|
|
73
|
+
const block = this.block;
|
|
74
|
+
let offset = 0;
|
|
75
|
+
while (this.offset + data.length - offset >= this.blockSize) {
|
|
76
|
+
for (let i = this.offset; i < this.blockSize;)
|
|
77
|
+
block[i++] = data[offset++];
|
|
78
|
+
this.compress();
|
|
79
|
+
this.offset = 0;
|
|
80
|
+
}
|
|
81
|
+
while (offset < data.length)
|
|
82
|
+
block[this.offset++] = data[offset++];
|
|
83
|
+
// update length
|
|
84
|
+
for (let j = 0, carry = data.length * 8; carry > 0; j++) {
|
|
85
|
+
let len = this.length[j];
|
|
86
|
+
len += carry;
|
|
87
|
+
carry = (len / 0x0100000000) | 0;
|
|
88
|
+
if (carry > 0)
|
|
89
|
+
len -= 0x0100000000 * carry;
|
|
90
|
+
this.length[j] = len;
|
|
91
|
+
}
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
compress() {
|
|
95
|
+
const wordBlocks = new Uint32Array(16);
|
|
96
|
+
for (let i = 0; i < 16; i++) {
|
|
97
|
+
wordBlocks[i] = this.view.getInt32(i * 4, true);
|
|
98
|
+
}
|
|
99
|
+
// prettier-ignore
|
|
100
|
+
let al = this.h0 | 0, ar = al, bl = this.h1 | 0, br = bl, cl = this.h2 | 0, cr = cl, dl = this.h3 | 0, dr = dl, el = this.h4 | 0, er = el;
|
|
101
|
+
// Instead of iterating 0 to 80, we split it into 5 groups
|
|
102
|
+
// And use the groups in constants, functions, etc. Much simpler
|
|
103
|
+
for (let group = 0; group < 5; group++) {
|
|
104
|
+
const rGroup = 4 - group;
|
|
105
|
+
const hbl = Kl[group];
|
|
106
|
+
const hbr = Kr[group];
|
|
107
|
+
for (let i = 0; i < 16; i++) {
|
|
108
|
+
const j = 16 * group + i;
|
|
109
|
+
const tl = (rotl(al + f(group, bl, cl, dl) + wordBlocks[rl[j]] + hbl, sl[j]) + el) | 0;
|
|
110
|
+
al = el, el = dl, dl = rotl(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
|
|
111
|
+
}
|
|
112
|
+
// 2 loops are 10% faster
|
|
113
|
+
for (let i = 0; i < 16; i++) {
|
|
114
|
+
const j = 16 * group + i;
|
|
115
|
+
const tr = (rotl(ar + f(rGroup, br, cr, dr) + wordBlocks[rr[j]] + hbr, sr[j]) + er) | 0;
|
|
116
|
+
ar = er, er = dr, dr = rotl(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// update state
|
|
120
|
+
const t = (this.h1 + cl + dr) | 0;
|
|
121
|
+
this.h1 = (this.h2 + dl + er) | 0;
|
|
122
|
+
this.h2 = (this.h3 + el + ar) | 0;
|
|
123
|
+
this.h3 = (this.h4 + al + br) | 0;
|
|
124
|
+
this.h4 = (this.h0 + bl + cr) | 0;
|
|
125
|
+
this.h0 = t;
|
|
126
|
+
}
|
|
127
|
+
digest() {
|
|
128
|
+
if (this.finalized)
|
|
129
|
+
throw new Error("Digest already called");
|
|
130
|
+
this.finalized = true;
|
|
131
|
+
// create padding and handle blocks
|
|
132
|
+
this.block[this.offset++] = 0x80;
|
|
133
|
+
if (this.offset > 56) {
|
|
134
|
+
this.block.fill(0, this.offset, 64);
|
|
135
|
+
this.compress();
|
|
136
|
+
this.offset = 0;
|
|
137
|
+
}
|
|
138
|
+
this.block.fill(0, this.offset, 56);
|
|
139
|
+
this.view.setUint32(56, this.length[0], true);
|
|
140
|
+
this.view.setUint32(60, this.length[1], true);
|
|
141
|
+
this.compress();
|
|
142
|
+
// produce result
|
|
143
|
+
const res = new DataView(new ArrayBuffer(20));
|
|
144
|
+
res.setInt32(0, this.h0, true);
|
|
145
|
+
res.setInt32(4, this.h1, true);
|
|
146
|
+
res.setInt32(8, this.h2, true);
|
|
147
|
+
res.setInt32(12, this.h3, true);
|
|
148
|
+
res.setInt32(16, this.h4, true);
|
|
149
|
+
// reset state
|
|
150
|
+
this.block.fill(0);
|
|
151
|
+
this.offset = 0;
|
|
152
|
+
for (let i = 0; i < 4; i++) {
|
|
153
|
+
this.length[i] = 0;
|
|
154
|
+
}
|
|
155
|
+
return new Uint8Array(res.buffer);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function toHex(uint8a) {
|
|
159
|
+
return Array.from(uint8a)
|
|
160
|
+
.map((c) => c.toString(16).padStart(2, "0"))
|
|
161
|
+
.join("");
|
|
162
|
+
}
|
|
163
|
+
function utf8ToBytes(str) {
|
|
164
|
+
return new TextEncoder().encode(str);
|
|
165
|
+
}
|
|
166
|
+
function ripemd160(message) {
|
|
167
|
+
const hasher = new RIPEMD160();
|
|
168
|
+
hasher.update(message instanceof Uint8Array ? message : utf8ToBytes(message));
|
|
169
|
+
const hash = hasher.digest();
|
|
170
|
+
return typeof message === "string" ? toHex(hash) : hash;
|
|
171
|
+
}
|
|
172
|
+
export function ripemd160Buffer(data, encoding) {
|
|
173
|
+
const inputDataBuffer = data instanceof ArrayBuffer ? data : data.buffer;
|
|
174
|
+
const hash = new RIPEMD160();
|
|
175
|
+
hash.update(new Uint8Array(inputDataBuffer));
|
|
176
|
+
if (encoding === "hex") {
|
|
177
|
+
return toHex(hash.digest());
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
return hash.digest();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
export function ripemd160Hex(hexString, encoding) {
|
|
184
|
+
return ripemd160Buffer(hexToU8Array(hexString), encoding);
|
|
185
|
+
}
|
|
186
|
+
export { ripemd160 };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare function createHash(algorithm?: string): Hash;
|
|
2
|
+
export declare class Hash {
|
|
3
|
+
private A;
|
|
4
|
+
private B;
|
|
5
|
+
private C;
|
|
6
|
+
private D;
|
|
7
|
+
private E;
|
|
8
|
+
private F;
|
|
9
|
+
private G;
|
|
10
|
+
private H;
|
|
11
|
+
private _byte;
|
|
12
|
+
private _word;
|
|
13
|
+
private _size;
|
|
14
|
+
private _sp;
|
|
15
|
+
constructor();
|
|
16
|
+
update(data: string, encoding?: string): this;
|
|
17
|
+
update(data: Uint8Array): this;
|
|
18
|
+
update(data: ArrayBufferView): this;
|
|
19
|
+
private _uint8;
|
|
20
|
+
private _utf8;
|
|
21
|
+
private _int32;
|
|
22
|
+
digest(): Uint8Array;
|
|
23
|
+
digest(encoding: string): string;
|
|
24
|
+
private _hex;
|
|
25
|
+
private _bin;
|
|
26
|
+
}
|
|
27
|
+
declare function sha256Buffer(data: Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | BigUint64Array | BigInt64Array | ArrayBuffer, encoding?: string): Uint8Array | string;
|
|
28
|
+
declare function sha256Hex(hexString: string, encoding?: string): string | Uint8Array;
|
|
29
|
+
export { sha256Buffer, sha256Hex };
|
|
30
|
+
//# sourceMappingURL=sha256.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sha256.d.ts","sourceRoot":"","sources":["../../src/hash/sha256.ts"],"names":[],"mappings":"AA+GA,wBAAgB,UAAU,CAAC,SAAS,CAAC,EAAE,MAAM,QAM5C;AAED,qBAAa,IAAI;IAEb,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAC3B,OAAO,CAAC,CAAC,CAAkB;IAE3B,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,GAAG,CAAK;;IAahB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAC7C,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAC9B,MAAM,CAAC,IAAI,EAAE,eAAe,GAAG,IAAI;IA0CnC,OAAO,CAAC,MAAM;IAuBd,OAAO,CAAC,KAAK;IAgDb,OAAO,CAAC,MAAM;IAoCd,MAAM,IAAI,UAAU;IACpB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAqChC,OAAO,CAAC,IAAI;IAMZ,OAAO,CAAC,IAAI;CAcf;AA6BD,iBAAS,YAAY,CACjB,IAAI,EACE,UAAU,GACV,SAAS,GACT,WAAW,GACX,UAAU,GACV,WAAW,GACX,UAAU,GACV,cAAc,GACd,aAAa,GACb,WAAW,EACjB,QAAQ,CAAC,EAAE,MAAM,GAClB,UAAU,GAAG,MAAM,CAUrB;AACD,iBAAS,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,uBAEtD;AAED,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2020-2023 Yusuke Kawasaki
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
|
|
25
|
+
*/
|
|
26
|
+
import { hexToU8Array } from "../data/array";
|
|
27
|
+
// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311
|
|
28
|
+
const K = [
|
|
29
|
+
0x428a2f98 | 0,
|
|
30
|
+
0x71374491 | 0,
|
|
31
|
+
0xb5c0fbcf | 0,
|
|
32
|
+
0xe9b5dba5 | 0,
|
|
33
|
+
0x3956c25b | 0,
|
|
34
|
+
0x59f111f1 | 0,
|
|
35
|
+
0x923f82a4 | 0,
|
|
36
|
+
0xab1c5ed5 | 0,
|
|
37
|
+
0xd807aa98 | 0,
|
|
38
|
+
0x12835b01 | 0,
|
|
39
|
+
0x243185be | 0,
|
|
40
|
+
0x550c7dc3 | 0,
|
|
41
|
+
0x72be5d74 | 0,
|
|
42
|
+
0x80deb1fe | 0,
|
|
43
|
+
0x9bdc06a7 | 0,
|
|
44
|
+
0xc19bf174 | 0,
|
|
45
|
+
0xe49b69c1 | 0,
|
|
46
|
+
0xefbe4786 | 0,
|
|
47
|
+
0x0fc19dc6 | 0,
|
|
48
|
+
0x240ca1cc | 0,
|
|
49
|
+
0x2de92c6f | 0,
|
|
50
|
+
0x4a7484aa | 0,
|
|
51
|
+
0x5cb0a9dc | 0,
|
|
52
|
+
0x76f988da | 0,
|
|
53
|
+
0x983e5152 | 0,
|
|
54
|
+
0xa831c66d | 0,
|
|
55
|
+
0xb00327c8 | 0,
|
|
56
|
+
0xbf597fc7 | 0,
|
|
57
|
+
0xc6e00bf3 | 0,
|
|
58
|
+
0xd5a79147 | 0,
|
|
59
|
+
0x06ca6351 | 0,
|
|
60
|
+
0x14292967 | 0,
|
|
61
|
+
0x27b70a85 | 0,
|
|
62
|
+
0x2e1b2138 | 0,
|
|
63
|
+
0x4d2c6dfc | 0,
|
|
64
|
+
0x53380d13 | 0,
|
|
65
|
+
0x650a7354 | 0,
|
|
66
|
+
0x766a0abb | 0,
|
|
67
|
+
0x81c2c92e | 0,
|
|
68
|
+
0x92722c85 | 0,
|
|
69
|
+
0xa2bfe8a1 | 0,
|
|
70
|
+
0xa81a664b | 0,
|
|
71
|
+
0xc24b8b70 | 0,
|
|
72
|
+
0xc76c51a3 | 0,
|
|
73
|
+
0xd192e819 | 0,
|
|
74
|
+
0xd6990624 | 0,
|
|
75
|
+
0xf40e3585 | 0,
|
|
76
|
+
0x106aa070 | 0,
|
|
77
|
+
0x19a4c116 | 0,
|
|
78
|
+
0x1e376c08 | 0,
|
|
79
|
+
0x2748774c | 0,
|
|
80
|
+
0x34b0bcb5 | 0,
|
|
81
|
+
0x391c0cb3 | 0,
|
|
82
|
+
0x4ed8aa4a | 0,
|
|
83
|
+
0x5b9cca4f | 0,
|
|
84
|
+
0x682e6ff3 | 0,
|
|
85
|
+
0x748f82ee | 0,
|
|
86
|
+
0x78a5636f | 0,
|
|
87
|
+
0x84c87814 | 0,
|
|
88
|
+
0x8cc70208 | 0,
|
|
89
|
+
0x90befffa | 0,
|
|
90
|
+
0xa4506ceb | 0,
|
|
91
|
+
0xbef9a3f7 | 0,
|
|
92
|
+
0xc67178f2 | 0,
|
|
93
|
+
];
|
|
94
|
+
const algorithms = {
|
|
95
|
+
sha256: 1,
|
|
96
|
+
};
|
|
97
|
+
export function createHash(algorithm) {
|
|
98
|
+
if (algorithm && !algorithms[algorithm] && !algorithms[algorithm.toLowerCase()]) {
|
|
99
|
+
throw new Error("Digest method not supported");
|
|
100
|
+
}
|
|
101
|
+
return new Hash();
|
|
102
|
+
}
|
|
103
|
+
export class Hash {
|
|
104
|
+
constructor() {
|
|
105
|
+
// first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
|
|
106
|
+
this.A = 0x6a09e667 | 0;
|
|
107
|
+
this.B = 0xbb67ae85 | 0;
|
|
108
|
+
this.C = 0x3c6ef372 | 0;
|
|
109
|
+
this.D = 0xa54ff53a | 0;
|
|
110
|
+
this.E = 0x510e527f | 0;
|
|
111
|
+
this.F = 0x9b05688c | 0;
|
|
112
|
+
this.G = 0x1f83d9ab | 0;
|
|
113
|
+
this.H = 0x5be0cd19 | 0;
|
|
114
|
+
this._size = 0;
|
|
115
|
+
this._sp = 0; // surrogate pair
|
|
116
|
+
if (!sharedBuffer || sharedOffset >= 8000 /* N.allocTotal */) {
|
|
117
|
+
sharedBuffer = new ArrayBuffer(8000 /* N.allocTotal */);
|
|
118
|
+
sharedOffset = 0;
|
|
119
|
+
}
|
|
120
|
+
this._byte = new Uint8Array(sharedBuffer, sharedOffset, 80 /* N.allocBytes */);
|
|
121
|
+
this._word = new Int32Array(sharedBuffer, sharedOffset, 20 /* N.allocWords */);
|
|
122
|
+
sharedOffset += 80 /* N.allocBytes */;
|
|
123
|
+
}
|
|
124
|
+
update(data) {
|
|
125
|
+
// data: string
|
|
126
|
+
if ("string" === typeof data) {
|
|
127
|
+
return this._utf8(data);
|
|
128
|
+
}
|
|
129
|
+
// data: undefined
|
|
130
|
+
if (data == null) {
|
|
131
|
+
throw new TypeError("Invalid type: " + typeof data);
|
|
132
|
+
}
|
|
133
|
+
const byteOffset = data.byteOffset;
|
|
134
|
+
const length = data.byteLength;
|
|
135
|
+
let blocks = (length / 64 /* N.inputBytes */) | 0;
|
|
136
|
+
let offset = 0;
|
|
137
|
+
// longer than 1 block
|
|
138
|
+
if (blocks && !(byteOffset & 3) && !(this._size % 64 /* N.inputBytes */)) {
|
|
139
|
+
const block = new Int32Array(data.buffer, byteOffset, blocks * 16 /* N.inputWords */);
|
|
140
|
+
while (blocks--) {
|
|
141
|
+
this._int32(block, offset >> 2);
|
|
142
|
+
offset += 64 /* N.inputBytes */;
|
|
143
|
+
}
|
|
144
|
+
this._size += offset;
|
|
145
|
+
}
|
|
146
|
+
// data: TypedArray | DataView
|
|
147
|
+
const BYTES_PER_ELEMENT = data.BYTES_PER_ELEMENT;
|
|
148
|
+
if (BYTES_PER_ELEMENT !== 1 && data.buffer) {
|
|
149
|
+
const rest = new Uint8Array(data.buffer, byteOffset + offset, length - offset);
|
|
150
|
+
return this._uint8(rest);
|
|
151
|
+
}
|
|
152
|
+
// no more bytes
|
|
153
|
+
if (offset === length)
|
|
154
|
+
return this;
|
|
155
|
+
// data: Uint8Array | Int8Array
|
|
156
|
+
return this._uint8(data, offset);
|
|
157
|
+
}
|
|
158
|
+
_uint8(data, offset) {
|
|
159
|
+
const { _byte, _word } = this;
|
|
160
|
+
const length = data.length;
|
|
161
|
+
offset = offset | 0;
|
|
162
|
+
while (offset < length) {
|
|
163
|
+
const start = this._size % 64 /* N.inputBytes */;
|
|
164
|
+
let index = start;
|
|
165
|
+
while (offset < length && index < 64 /* N.inputBytes */) {
|
|
166
|
+
_byte[index++] = data[offset++];
|
|
167
|
+
}
|
|
168
|
+
if (index >= 64 /* N.inputBytes */) {
|
|
169
|
+
this._int32(_word);
|
|
170
|
+
}
|
|
171
|
+
this._size += index - start;
|
|
172
|
+
}
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
_utf8(text) {
|
|
176
|
+
const { _byte, _word } = this;
|
|
177
|
+
const length = text.length;
|
|
178
|
+
let surrogate = this._sp;
|
|
179
|
+
for (let offset = 0; offset < length;) {
|
|
180
|
+
const start = this._size % 64 /* N.inputBytes */;
|
|
181
|
+
let index = start;
|
|
182
|
+
while (offset < length && index < 64 /* N.inputBytes */) {
|
|
183
|
+
let code = text.charCodeAt(offset++) | 0;
|
|
184
|
+
if (code < 0x80) {
|
|
185
|
+
// ASCII characters
|
|
186
|
+
_byte[index++] = code;
|
|
187
|
+
}
|
|
188
|
+
else if (code < 0x800) {
|
|
189
|
+
// 2 bytes
|
|
190
|
+
_byte[index++] = 0xc0 | (code >>> 6);
|
|
191
|
+
_byte[index++] = 0x80 | (code & 0x3f);
|
|
192
|
+
}
|
|
193
|
+
else if (code < 0xd800 || code > 0xdfff) {
|
|
194
|
+
// 3 bytes
|
|
195
|
+
_byte[index++] = 0xe0 | (code >>> 12);
|
|
196
|
+
_byte[index++] = 0x80 | ((code >>> 6) & 0x3f);
|
|
197
|
+
_byte[index++] = 0x80 | (code & 0x3f);
|
|
198
|
+
}
|
|
199
|
+
else if (surrogate) {
|
|
200
|
+
// 4 bytes - surrogate pair
|
|
201
|
+
code = ((surrogate & 0x3ff) << 10) + (code & 0x3ff) + 0x10000;
|
|
202
|
+
_byte[index++] = 0xf0 | (code >>> 18);
|
|
203
|
+
_byte[index++] = 0x80 | ((code >>> 12) & 0x3f);
|
|
204
|
+
_byte[index++] = 0x80 | ((code >>> 6) & 0x3f);
|
|
205
|
+
_byte[index++] = 0x80 | (code & 0x3f);
|
|
206
|
+
surrogate = 0;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
surrogate = code;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (index >= 64 /* N.inputBytes */) {
|
|
213
|
+
this._int32(_word);
|
|
214
|
+
_word[0] = _word[16 /* N.inputWords */];
|
|
215
|
+
}
|
|
216
|
+
this._size += index - start;
|
|
217
|
+
}
|
|
218
|
+
this._sp = surrogate;
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
_int32(data, offset) {
|
|
222
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
223
|
+
let i = 0;
|
|
224
|
+
offset = offset | 0;
|
|
225
|
+
while (i < 16 /* N.inputWords */) {
|
|
226
|
+
W[i++] = swap32(data[offset++]);
|
|
227
|
+
}
|
|
228
|
+
for (i = 16 /* N.inputWords */; i < 64 /* N.workWords */; i++) {
|
|
229
|
+
W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0;
|
|
230
|
+
}
|
|
231
|
+
for (i = 0; i < 64 /* N.workWords */; i++) {
|
|
232
|
+
const T1 = (H + sigma1(E) + ch(E, F, G) + K[i] + W[i]) | 0;
|
|
233
|
+
const T2 = (sigma0(A) + maj(A, B, C)) | 0;
|
|
234
|
+
H = G;
|
|
235
|
+
G = F;
|
|
236
|
+
F = E;
|
|
237
|
+
E = (D + T1) | 0;
|
|
238
|
+
D = C;
|
|
239
|
+
C = B;
|
|
240
|
+
B = A;
|
|
241
|
+
A = (T1 + T2) | 0;
|
|
242
|
+
}
|
|
243
|
+
this.A = (A + this.A) | 0;
|
|
244
|
+
this.B = (B + this.B) | 0;
|
|
245
|
+
this.C = (C + this.C) | 0;
|
|
246
|
+
this.D = (D + this.D) | 0;
|
|
247
|
+
this.E = (E + this.E) | 0;
|
|
248
|
+
this.F = (F + this.F) | 0;
|
|
249
|
+
this.G = (G + this.G) | 0;
|
|
250
|
+
this.H = (H + this.H) | 0;
|
|
251
|
+
}
|
|
252
|
+
digest(encoding) {
|
|
253
|
+
const { _byte, _word } = this;
|
|
254
|
+
let i = this._size % 64 /* N.inputBytes */ | 0;
|
|
255
|
+
_byte[i++] = 0x80;
|
|
256
|
+
// pad 0 for current word
|
|
257
|
+
while (i & 3) {
|
|
258
|
+
_byte[i++] = 0;
|
|
259
|
+
}
|
|
260
|
+
i >>= 2;
|
|
261
|
+
if (i > 14 /* N.highIndex */) {
|
|
262
|
+
while (i < 16 /* N.inputWords */) {
|
|
263
|
+
_word[i++] = 0;
|
|
264
|
+
}
|
|
265
|
+
i = 0;
|
|
266
|
+
this._int32(_word);
|
|
267
|
+
}
|
|
268
|
+
// pad 0 for rest words
|
|
269
|
+
while (i < 16 /* N.inputWords */) {
|
|
270
|
+
_word[i++] = 0;
|
|
271
|
+
}
|
|
272
|
+
// input size
|
|
273
|
+
const bits64 = this._size * 8;
|
|
274
|
+
const low32 = (bits64 & 0xffffffff) >>> 0;
|
|
275
|
+
const high32 = (bits64 - low32) / 0x100000000;
|
|
276
|
+
if (high32)
|
|
277
|
+
_word[14 /* N.highIndex */] = swap32(high32);
|
|
278
|
+
if (low32)
|
|
279
|
+
_word[15 /* N.lowIndex */] = swap32(low32);
|
|
280
|
+
this._int32(_word);
|
|
281
|
+
return encoding === "hex" ? this._hex() : this._bin();
|
|
282
|
+
}
|
|
283
|
+
_hex() {
|
|
284
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
285
|
+
return hex32(A) + hex32(B) + hex32(C) + hex32(D) + hex32(E) + hex32(F) + hex32(G) + hex32(H);
|
|
286
|
+
}
|
|
287
|
+
_bin() {
|
|
288
|
+
const { A, B, C, D, E, F, G, H, _byte, _word } = this;
|
|
289
|
+
_word[0] = swap32(A);
|
|
290
|
+
_word[1] = swap32(B);
|
|
291
|
+
_word[2] = swap32(C);
|
|
292
|
+
_word[3] = swap32(D);
|
|
293
|
+
_word[4] = swap32(E);
|
|
294
|
+
_word[5] = swap32(F);
|
|
295
|
+
_word[6] = swap32(G);
|
|
296
|
+
_word[7] = swap32(H);
|
|
297
|
+
return _byte.slice(0, 32);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const W = new Int32Array(64 /* N.workWords */);
|
|
301
|
+
let sharedBuffer;
|
|
302
|
+
let sharedOffset = 0;
|
|
303
|
+
const hex32 = (num) => (num + 0x100000000).toString(16).substr(-8);
|
|
304
|
+
const swapLE = (c) => ((c << 24) & 0xff000000) | ((c << 8) & 0xff0000) | ((c >> 8) & 0xff00) | ((c >> 24) & 0xff);
|
|
305
|
+
const swapBE = (c) => c;
|
|
306
|
+
const swap32 = isBE() ? swapBE : swapLE;
|
|
307
|
+
const ch = (x, y, z) => z ^ (x & (y ^ z));
|
|
308
|
+
const maj = (x, y, z) => (x & y) | (z & (x | y));
|
|
309
|
+
const sigma0 = (x) => ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^ ((x >>> 22) | (x << 10));
|
|
310
|
+
const sigma1 = (x) => ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^ ((x >>> 25) | (x << 7));
|
|
311
|
+
const gamma0 = (x) => ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
|
|
312
|
+
const gamma1 = (x) => ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10);
|
|
313
|
+
function isBE() {
|
|
314
|
+
const buf = new Uint8Array(new Uint16Array([0xfeff]).buffer); // BOM
|
|
315
|
+
return buf[0] === 0xfe;
|
|
316
|
+
}
|
|
317
|
+
function sha256Buffer(data, encoding) {
|
|
318
|
+
const inputDataBuffer = data instanceof ArrayBuffer ? data : data.buffer;
|
|
319
|
+
const hash = new Hash();
|
|
320
|
+
hash.update(new Uint8Array(inputDataBuffer));
|
|
321
|
+
if (encoding) {
|
|
322
|
+
return hash.digest(encoding);
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
return hash.digest();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function sha256Hex(hexString, encoding) {
|
|
329
|
+
return sha256Buffer(hexToU8Array(hexString), encoding);
|
|
330
|
+
}
|
|
331
|
+
export { sha256Buffer, sha256Hex };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { IBaseEvent, IndexableType, TEventListenerMap } from "./EventHub";
|
|
2
|
+
export { EventHub } from "./EventHub";
|
|
3
|
+
export * from "./data/array";
|
|
4
|
+
export * from "./hash";
|
|
5
|
+
export * from "./uuid";
|
|
6
|
+
export * from "./debounce";
|
|
7
|
+
export * from "./seedrandom";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,cAAc,cAAc,CAAC;AAE7B,cAAc,QAAQ,CAAC;AAEvB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC"}
|
package/dist/index.js
ADDED
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,iBAAS,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,GAAG,QAQxD;AAED,iBAAS,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,OAS5C;AAED,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC"}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function setInObject(obj, path, value) {
|
|
2
|
+
const cur = obj;
|
|
3
|
+
for (let i = 0, l = path.length - 1; i < l; i++) {
|
|
4
|
+
if (!Object.hasOwnProperty.call(cur, path[i])) {
|
|
5
|
+
cur[path[i]] = {};
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
cur[path[path.length - 1]] = value;
|
|
9
|
+
}
|
|
10
|
+
function getInObject(obj, path) {
|
|
11
|
+
let cur = obj;
|
|
12
|
+
for (let i = 0, l = path.length; i < l; i++) {
|
|
13
|
+
if (!Object.hasOwnProperty.call(cur, path[i])) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
cur = cur[path[i]];
|
|
17
|
+
}
|
|
18
|
+
return cur;
|
|
19
|
+
}
|
|
20
|
+
export { setInObject, getInObject };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seedrandom.d.ts","sourceRoot":"","sources":["../src/seedrandom.ts"],"names":[],"mappings":"AAmMA,cAAM,UAAU;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,GAAG,CAAC;gBACJ,IAAI,EAAE,MAAM;IAIxB,MAAM,IAAI,MAAM;IAGhB,OAAO,CAAC,GAAG,EAAE,MAAM;CAQtB;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
const pool = [];
|
|
2
|
+
let width = 256, // each RC4 output is 0 <= x < 256
|
|
3
|
+
chunks = 6, // at least six RC4 outputs for each double
|
|
4
|
+
digits = 52, // there are 52 significant digits in a double
|
|
5
|
+
rngname = "random", // rngname: name for Math.random and Math.seedrandom
|
|
6
|
+
startdenom = Math.pow(width, chunks), significance = Math.pow(2, digits), overflow = significance * 2, mask = width - 1, nodecrypto; // node.js crypto module, initialized at the bottom.
|
|
7
|
+
//
|
|
8
|
+
// seedrandom()
|
|
9
|
+
// This is the seedrandom function described above.
|
|
10
|
+
//
|
|
11
|
+
function seedrandom(seed, options, callback) {
|
|
12
|
+
const key = [];
|
|
13
|
+
options = options == true ? { entropy: true } : options || {};
|
|
14
|
+
// Flatten the seed string or build one from local entropy if needed.
|
|
15
|
+
const shortseed = mixkey(flatten(options.entropy ? [seed, tostring(pool)] : seed, 3), key);
|
|
16
|
+
// Use the seed to initialize an ARC4 generator.
|
|
17
|
+
const arc4 = new ARC4(key);
|
|
18
|
+
// This function returns a random double in [0, 1) that contains
|
|
19
|
+
// randomness in every bit of the mantissa of the IEEE 754 value.
|
|
20
|
+
const prng = function () {
|
|
21
|
+
let n = arc4.g(chunks), // Start with a numerator n < 2 ^ 48
|
|
22
|
+
d = startdenom, // and denominator d = 2 ^ 48.
|
|
23
|
+
x = 0; // and no 'extra last byte'.
|
|
24
|
+
while (n < significance) {
|
|
25
|
+
// Fill up all significant digits by
|
|
26
|
+
n = (n + x) * width; // shifting numerator and
|
|
27
|
+
d *= width; // denominator and generating a
|
|
28
|
+
x = arc4.g(1); // new least-significant-byte.
|
|
29
|
+
}
|
|
30
|
+
while (n >= overflow) {
|
|
31
|
+
// To avoid rounding up, before adding
|
|
32
|
+
n /= 2; // last byte, shift everything
|
|
33
|
+
d /= 2; // right using integer math until
|
|
34
|
+
x >>>= 1; // we have exactly the desired bits.
|
|
35
|
+
}
|
|
36
|
+
return (n + x) / d; // Form the number within [0, 1).
|
|
37
|
+
};
|
|
38
|
+
prng.int32 = function () {
|
|
39
|
+
return arc4.g(4) | 0;
|
|
40
|
+
};
|
|
41
|
+
prng.quick = function () {
|
|
42
|
+
return arc4.g(4) / 0x100000000;
|
|
43
|
+
};
|
|
44
|
+
prng.double = prng;
|
|
45
|
+
// Mix the randomness into accumulated entropy.
|
|
46
|
+
mixkey(tostring(arc4.S), pool);
|
|
47
|
+
// Calling convention: what to return as a function of prng, seed, is_math.
|
|
48
|
+
return (options.pass ||
|
|
49
|
+
callback ||
|
|
50
|
+
function (prng, seed, is_math_call, state) {
|
|
51
|
+
if (state) {
|
|
52
|
+
// Load the arc4 state from the given state if it has an S array.
|
|
53
|
+
if (state.S) {
|
|
54
|
+
copy(state, arc4);
|
|
55
|
+
}
|
|
56
|
+
// Only provide the .state method if requested via options.state.
|
|
57
|
+
prng.state = function () {
|
|
58
|
+
return copy(arc4, {});
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
// If called as a method of Math (Math.seedrandom()), mutate
|
|
62
|
+
// Math.random because that is how seedrandom.js has worked since v1.0.
|
|
63
|
+
// Otherwise, it is a newer calling convention, so return the
|
|
64
|
+
// prng directly.
|
|
65
|
+
else
|
|
66
|
+
return prng;
|
|
67
|
+
})(prng, shortseed,
|
|
68
|
+
//@ts-ignore
|
|
69
|
+
"global" in options ? options.global : this == Math, options.state);
|
|
70
|
+
}
|
|
71
|
+
//
|
|
72
|
+
// ARC4
|
|
73
|
+
//
|
|
74
|
+
// An ARC4 implementation. The constructor takes a key in the form of
|
|
75
|
+
// an array of at most (width) integers that should be 0 <= x < (width).
|
|
76
|
+
//
|
|
77
|
+
// The g(count) method returns a pseudorandom integer that concatenates
|
|
78
|
+
// the next (count) outputs from ARC4. Its return value is a number x
|
|
79
|
+
// that is in the range 0 <= x < (width ^ count).
|
|
80
|
+
//
|
|
81
|
+
function ARC4(key) {
|
|
82
|
+
let t, keylen = key.length;
|
|
83
|
+
//@ts-ignore
|
|
84
|
+
let me = this, i = 0, j = (me.i = me.j = 0), s = (me.S = []);
|
|
85
|
+
// The empty key [] is treated as [0].
|
|
86
|
+
if (!keylen) {
|
|
87
|
+
key = [keylen++];
|
|
88
|
+
}
|
|
89
|
+
// Set up S using the standard key scheduling algorithm.
|
|
90
|
+
while (i < width) {
|
|
91
|
+
s[i] = i++;
|
|
92
|
+
}
|
|
93
|
+
for (i = 0; i < width; i++) {
|
|
94
|
+
s[i] = s[(j = mask & (j + key[i % keylen] + (t = s[i])))];
|
|
95
|
+
s[j] = t;
|
|
96
|
+
}
|
|
97
|
+
// The "g" method returns the next (count) outputs as one number.
|
|
98
|
+
(me.g = function (count) {
|
|
99
|
+
// Using instance members instead of closure state nearly doubles speed.
|
|
100
|
+
let t, r = 0, i = me.i, j = me.j, s = me.S;
|
|
101
|
+
while (count--) {
|
|
102
|
+
t = s[(i = mask & (i + 1))];
|
|
103
|
+
r = r * width + s[mask & ((s[i] = s[(j = mask & (j + t))]) + (s[j] = t))];
|
|
104
|
+
}
|
|
105
|
+
me.i = i;
|
|
106
|
+
me.j = j;
|
|
107
|
+
return r;
|
|
108
|
+
// For robust unpredictability, the function call below automatically
|
|
109
|
+
// discards an initial batch of values. This is called RC4-drop[256].
|
|
110
|
+
// See http://google.com/search?q=rsa+fluhrer+response&btnI
|
|
111
|
+
})(width);
|
|
112
|
+
}
|
|
113
|
+
//
|
|
114
|
+
// copy()
|
|
115
|
+
// Copies internal state of ARC4 to or from a plain object.
|
|
116
|
+
//
|
|
117
|
+
function copy(f, t) {
|
|
118
|
+
t.i = f.i;
|
|
119
|
+
t.j = f.j;
|
|
120
|
+
t.S = f.S.slice();
|
|
121
|
+
return t;
|
|
122
|
+
}
|
|
123
|
+
//
|
|
124
|
+
// flatten()
|
|
125
|
+
// Converts an object tree to nested arrays of strings.
|
|
126
|
+
//
|
|
127
|
+
function flatten(obj, depth) {
|
|
128
|
+
let result = [], typ = typeof obj, prop;
|
|
129
|
+
if (depth && typ == "object") {
|
|
130
|
+
for (prop in obj) {
|
|
131
|
+
try {
|
|
132
|
+
result.push(flatten(obj[prop], depth - 1));
|
|
133
|
+
}
|
|
134
|
+
catch (e) { }
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result.length ? result : typ == "string" ? obj : obj + "\0";
|
|
138
|
+
}
|
|
139
|
+
//
|
|
140
|
+
// mixkey()
|
|
141
|
+
// Mixes a string seed into a key that is an array of integers, and
|
|
142
|
+
// returns a shortened string seed that is equivalent to the result key.
|
|
143
|
+
//
|
|
144
|
+
function mixkey(seed, key) {
|
|
145
|
+
let stringseed = seed + "", smear, j = 0;
|
|
146
|
+
while (j < stringseed.length) {
|
|
147
|
+
key[mask & j] = mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++));
|
|
148
|
+
}
|
|
149
|
+
return tostring(key);
|
|
150
|
+
}
|
|
151
|
+
//
|
|
152
|
+
// tostring()
|
|
153
|
+
// Converts an array of charcodes to a string
|
|
154
|
+
//
|
|
155
|
+
function tostring(a) {
|
|
156
|
+
return String.fromCharCode.apply(0, a);
|
|
157
|
+
}
|
|
158
|
+
class SeedRandom {
|
|
159
|
+
constructor(seed) {
|
|
160
|
+
this.seed = seed;
|
|
161
|
+
this.seedrandom = seedrandom(seed);
|
|
162
|
+
}
|
|
163
|
+
random() {
|
|
164
|
+
return this.seedrandom();
|
|
165
|
+
}
|
|
166
|
+
randHex(len) {
|
|
167
|
+
const hex = [];
|
|
168
|
+
for (let i = 0; i < len; i++) {
|
|
169
|
+
const value = Math.floor(this.random() * 0x100);
|
|
170
|
+
hex.push(value.toString(16).padStart(2, "0"));
|
|
171
|
+
}
|
|
172
|
+
return hex.join("");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export { SeedRandom };
|
package/dist/uuid.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../src/uuid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/uuid.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@psy-protocol/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"uuid": "^9.0.1"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/uuid": "^9.0.8",
|
|
15
|
+
"rimraf": "~5.0.10",
|
|
16
|
+
"typescript": "~5.3.3"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "pnpm run clean && pnpm run compile",
|
|
20
|
+
"clean": "rimraf ./dist",
|
|
21
|
+
"compile": "tsc -p tsconfig.build.json"
|
|
22
|
+
}
|
|
23
|
+
}
|