@fleet-sdk/crypto 0.4.0 → 0.5.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/CHANGELOG.md +13 -0
- package/dist/index.d.mts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +28 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +28 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
import { HexString } from '@fleet-sdk/common';
|
1
2
|
import * as _scure_base from '@scure/base';
|
2
3
|
|
3
|
-
type
|
4
|
+
type ByteInput = Uint8Array | HexString;
|
4
5
|
interface Coder<F, T> {
|
5
6
|
encode(decoded: F): T;
|
6
7
|
decode(encoded: T): F;
|
@@ -16,8 +17,9 @@ interface BytesCoder extends Coder<Uint8Array, string> {
|
|
16
17
|
decode: (str: string) => Uint8Array;
|
17
18
|
}
|
18
19
|
|
19
|
-
declare function
|
20
|
-
declare function
|
20
|
+
declare function ensureBytes(input: ByteInput): Uint8Array;
|
21
|
+
declare function blake2b256(message: ByteInput): Uint8Array;
|
22
|
+
declare function sha256(message: ByteInput): Uint8Array;
|
21
23
|
|
22
24
|
declare const hex: BytesCoder;
|
23
25
|
|
@@ -45,4 +47,4 @@ declare function validateEcPoint(pointBytes: Uint8Array): boolean;
|
|
45
47
|
*/
|
46
48
|
declare const randomBytes: (bytesLength?: number) => Uint8Array;
|
47
49
|
|
48
|
-
export {
|
50
|
+
export { type ByteInput, type BytesCoder, type Coder, base58, base58check, base64, bigintBE, blake2b256, ensureBytes, hex, randomBytes, sha256, utf8, validateEcPoint };
|
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
import { HexString } from '@fleet-sdk/common';
|
1
2
|
import * as _scure_base from '@scure/base';
|
2
3
|
|
3
|
-
type
|
4
|
+
type ByteInput = Uint8Array | HexString;
|
4
5
|
interface Coder<F, T> {
|
5
6
|
encode(decoded: F): T;
|
6
7
|
decode(encoded: T): F;
|
@@ -16,8 +17,9 @@ interface BytesCoder extends Coder<Uint8Array, string> {
|
|
16
17
|
decode: (str: string) => Uint8Array;
|
17
18
|
}
|
18
19
|
|
19
|
-
declare function
|
20
|
-
declare function
|
20
|
+
declare function ensureBytes(input: ByteInput): Uint8Array;
|
21
|
+
declare function blake2b256(message: ByteInput): Uint8Array;
|
22
|
+
declare function sha256(message: ByteInput): Uint8Array;
|
21
23
|
|
22
24
|
declare const hex: BytesCoder;
|
23
25
|
|
@@ -45,4 +47,4 @@ declare function validateEcPoint(pointBytes: Uint8Array): boolean;
|
|
45
47
|
*/
|
46
48
|
declare const randomBytes: (bytesLength?: number) => Uint8Array;
|
47
49
|
|
48
|
-
export {
|
50
|
+
export { type ByteInput, type BytesCoder, type Coder, base58, base58check, base64, bigintBE, blake2b256, ensureBytes, hex, randomBytes, sha256, utf8, validateEcPoint };
|
package/dist/index.js
CHANGED
@@ -7,7 +7,24 @@ var base = require('@scure/base');
|
|
7
7
|
var common = require('@fleet-sdk/common');
|
8
8
|
|
9
9
|
// src/index.ts
|
10
|
-
var HEXES = Array.from(
|
10
|
+
var HEXES = Array.from(
|
11
|
+
{ length: 256 },
|
12
|
+
(_, i) => i.toString(16).padStart(2, "0")
|
13
|
+
);
|
14
|
+
var HexChar = {
|
15
|
+
ZERO: 48,
|
16
|
+
// 0
|
17
|
+
NINE: 57,
|
18
|
+
// 9
|
19
|
+
A_UP: 65,
|
20
|
+
// A
|
21
|
+
F_UP: 70,
|
22
|
+
// F
|
23
|
+
A_LO: 97,
|
24
|
+
// a
|
25
|
+
F_LO: 102
|
26
|
+
// f
|
27
|
+
};
|
11
28
|
function bytesToHex(bytes) {
|
12
29
|
common.assertInstanceOf(bytes, Uint8Array);
|
13
30
|
let hex3 = "";
|
@@ -29,12 +46,11 @@ function hexToBytes(hex3) {
|
|
29
46
|
return bytes;
|
30
47
|
}
|
31
48
|
function charCodeToBase16(char) {
|
32
|
-
if (char >=
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
return char - (97 /* A_LO */ - 10);
|
49
|
+
if (char >= HexChar.ZERO && char <= HexChar.NINE) return char - HexChar.ZERO;
|
50
|
+
if (char >= HexChar.A_UP && char <= HexChar.F_UP)
|
51
|
+
return char - (HexChar.A_UP - 10);
|
52
|
+
if (char >= HexChar.A_LO && char <= HexChar.F_LO)
|
53
|
+
return char - (HexChar.A_LO - 10);
|
38
54
|
throw new Error("Invalid byte sequence.");
|
39
55
|
}
|
40
56
|
var hex = {
|
@@ -59,14 +75,14 @@ var bigintBE = {
|
|
59
75
|
*/
|
60
76
|
encode(data) {
|
61
77
|
const hexInput = base.hex.encode(data);
|
62
|
-
return BigInt(hexInput
|
78
|
+
return BigInt(hexInput === "" ? "0" : `0x${hexInput}`);
|
63
79
|
},
|
64
80
|
/**
|
65
81
|
* Decode a `BigInt` to a `Uint8Array`.
|
66
82
|
*/
|
67
83
|
decode(data) {
|
68
84
|
const hexData = data.toString(16);
|
69
|
-
return base.hex.decode(hexData.length % 2 ?
|
85
|
+
return base.hex.decode(hexData.length % 2 ? `0${hexData}` : hexData);
|
70
86
|
}
|
71
87
|
};
|
72
88
|
|
@@ -77,9 +93,7 @@ var base64 = base.base64;
|
|
77
93
|
|
78
94
|
// src/hashes.ts
|
79
95
|
function ensureBytes(input) {
|
80
|
-
|
81
|
-
return input;
|
82
|
-
return hex.decode(input);
|
96
|
+
return typeof input === "string" ? hex.decode(input) : input;
|
83
97
|
}
|
84
98
|
function blake2b256(message) {
|
85
99
|
return blake2b.blake2b(ensureBytes(message), { dkLen: 32 });
|
@@ -88,8 +102,7 @@ function sha256(message) {
|
|
88
102
|
return sha256$1.sha256(ensureBytes(message));
|
89
103
|
}
|
90
104
|
function validateEcPoint(pointBytes) {
|
91
|
-
if (common.isEmpty(pointBytes))
|
92
|
-
return false;
|
105
|
+
if (common.isEmpty(pointBytes)) return false;
|
93
106
|
switch (pointBytes[0]) {
|
94
107
|
case 2 /* Compressed */:
|
95
108
|
case 3 /* CompressedOdd */:
|
@@ -109,6 +122,7 @@ exports.base58check = base58check;
|
|
109
122
|
exports.base64 = base64;
|
110
123
|
exports.bigintBE = bigintBE;
|
111
124
|
exports.blake2b256 = blake2b256;
|
125
|
+
exports.ensureBytes = ensureBytes;
|
112
126
|
exports.hex = hex;
|
113
127
|
exports.randomBytes = randomBytes;
|
114
128
|
exports.sha256 = sha256;
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hashes.ts","../src/coders/index.ts","../src/coders/hex.ts","../src/coders/utf8.ts","../src/coders/bigintBE.ts","../src/ecpoint.ts"],"names":["hex","assertInstanceOf","assertTypeOf"],"mappings":";AAAA,SAAS,eAAe,wBAAwB;;;ACAhD,SAAS,eAAe;AACxB,SAAS,UAAU,eAAe;;;ACDlC;AAAA,EACE,eAAe;AAAA,EACf,UAAU;AAAA,EACV,UAAU;AAAA,OACL;;;ACJP,SAAS,QAAQ,kBAAkB,oBAAoB;AAGvD,IAAM,QAAQ,MAAM,
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hashes.ts","../src/coders/index.ts","../src/coders/hex.ts","../src/coders/utf8.ts","../src/coders/bigintBE.ts","../src/ecpoint.ts"],"names":["hex","assertInstanceOf","assertTypeOf"],"mappings":";AAAA,SAAS,eAAe,wBAAwB;;;ACAhD,SAAS,eAAe;AACxB,SAAS,UAAU,eAAe;;;ACDlC;AAAA,EACE,eAAe;AAAA,EACf,UAAU;AAAA,EACV,UAAU;AAAA,OACL;;;ACJP,SAAS,QAAQ,kBAAkB,oBAAoB;AAGvD,IAAM,QAAQ,MAAM;AAAA,EAAK,EAAE,QAAQ,IAAI;AAAA,EAAG,CAAC,GAAG,MAC5C,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAChC;AAEA,IAAM,UAAU;AAAA,EACd,M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Secure PRNG from \"@noble/hashes\". Uses crypto.getRandomValues, which defers to OS.\n */\nexport const randomBytes = nobleRandomBytes as (\n bytesLength?: number\n) => Uint8Array;\n\nexport * from \"./hashes\";\nexport * from \"./types\";\nexport * from \"./coders\";\nexport * from \"./ecpoint\";\n","import { blake2b } from \"@noble/hashes/blake2b\";\nimport { sha256 as _sha256 } from \"@noble/hashes/sha256\";\nimport { hex } from \"./coders\";\nimport type { ByteInput } from \"./types\";\n\nexport function ensureBytes(input: ByteInput): Uint8Array {\n return typeof input === \"string\" ? hex.decode(input) : input;\n}\n\nexport function blake2b256(message: ByteInput): Uint8Array {\n return blake2b(ensureBytes(message), { dkLen: 32 });\n}\n\nexport function sha256(message: ByteInput): Uint8Array {\n return _sha256(ensureBytes(message));\n}\n","import {\n base58check as base58checkCoder,\n base58 as base58Coder,\n base64 as base64Coder\n} from \"@scure/base\";\nimport { sha256 } from \"../hashes\";\nimport type { BytesCoder } from \"../types\";\n\nexport const base58check = base58checkCoder(sha256);\nexport const base58 = base58Coder as BytesCoder;\nexport const base64 = base64Coder as BytesCoder;\n\nexport { hex } from \"./hex\";\nexport { utf8 } from \"./utf8\";\nexport { bigintBE } from \"./bigintBE\";\n","import { assert, assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport type { BytesCoder } from \"../types\";\n\nconst HEXES = Array.from({ length: 256 }, (_, i) =>\n i.toString(16).padStart(2, \"0\")\n);\n\nconst HexChar = {\n ZERO: 48, // 0\n NINE: 57, // 9\n A_UP: 65, // A\n F_UP: 70, // F\n A_LO: 97, // a\n F_LO: 102 // f\n} as const;\n\nfunction bytesToHex(bytes: Uint8Array): string {\n assertInstanceOf(bytes, Uint8Array);\n\n let hex = \"\";\n for (let i = 0, len = bytes.length; i < len; i++) {\n hex += HEXES[bytes[i]];\n }\n\n return hex;\n}\n\nfunction hexToBytes(hex: string): Uint8Array {\n assertTypeOf(hex, \"string\");\n assert(hex.length % 2 === 0, \"Invalid hex padding.\");\n\n const len = hex.length / 2;\n const bytes = new Uint8Array(len);\n for (let i = 0, j = 0; i < len; i++) {\n const n1 = charCodeToBase16(hex.charCodeAt(j++));\n const n2 = charCodeToBase16(hex.charCodeAt(j++));\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\nfunction charCodeToBase16(char: number) {\n if (char >= HexChar.ZERO && char <= HexChar.NINE) return char - HexChar.ZERO;\n if (char >= HexChar.A_UP && char <= HexChar.F_UP)\n return char - (HexChar.A_UP - 10);\n if (char >= HexChar.A_LO && char <= HexChar.F_LO)\n return char - (HexChar.A_LO - 10);\n\n throw new Error(\"Invalid byte sequence.\");\n}\n\nexport const hex: BytesCoder = {\n encode: bytesToHex,\n decode: hexToBytes\n};\n","import { assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport type { BytesCoder } from \"../types\";\n\nfunction bytesToUtf8(bytes: Uint8Array): string {\n assertInstanceOf(bytes, Uint8Array);\n\n return new TextDecoder().decode(bytes);\n}\n\nfunction utf8ToBytes(str: string): Uint8Array {\n assertTypeOf(str, \"string\");\n\n return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809\n}\n\nexport const utf8: BytesCoder = {\n encode: bytesToUtf8,\n decode: utf8ToBytes\n};\n","import { hex } from \"@scure/base\";\nimport type { Coder } from \"../types\";\n\n/**\n * A coder for Big Endian `BigInt` <> `Uint8Array` conversion..\n */\nexport const bigintBE: Coder<Uint8Array, bigint> = {\n /**\n * Encode a `Uint8Array` to a `BigInt`.\n */\n encode(data) {\n const hexInput = hex.encode(data);\n return BigInt(hexInput === \"\" ? \"0\" : `0x${hexInput}`);\n },\n\n /**\n * Decode a `BigInt` to a `Uint8Array`.\n */\n decode(data) {\n const hexData = data.toString(16);\n return hex.decode(hexData.length % 2 ? `0${hexData}` : hexData);\n }\n};\n","import { isEmpty } from \"@fleet-sdk/common\";\n\n/**\n * EC point type\n */\nenum EcPointType {\n /**\n * Compressed, positive Y coordinate\n */\n Compressed = 0x02,\n /**\n * Compressed, negative Y coordinate\n */\n CompressedOdd = 0x03,\n /**\n * Uncompressed\n */\n Uncompressed = 0x04\n}\n\n/**\n * Validate Elliptic Curve point\n *\n * @param pointBytes EC point bytes\n * @returns True if the point is valid\n */\nexport function validateEcPoint(pointBytes: Uint8Array) {\n if (isEmpty(pointBytes)) return false;\n\n switch (pointBytes[0]) {\n case EcPointType.Compressed:\n case EcPointType.CompressedOdd:\n return pointBytes.length === 33;\n case EcPointType.Uncompressed:\n return pointBytes.length === 65;\n default:\n return false;\n }\n}\n"]}
|
package/dist/index.mjs
CHANGED
@@ -5,7 +5,24 @@ import { base58check as base58check$1, hex as hex$1, base58 as base58$1, base64
|
|
5
5
|
import { isEmpty, assertInstanceOf, assertTypeOf, assert } from '@fleet-sdk/common';
|
6
6
|
|
7
7
|
// src/index.ts
|
8
|
-
var HEXES = Array.from(
|
8
|
+
var HEXES = Array.from(
|
9
|
+
{ length: 256 },
|
10
|
+
(_, i) => i.toString(16).padStart(2, "0")
|
11
|
+
);
|
12
|
+
var HexChar = {
|
13
|
+
ZERO: 48,
|
14
|
+
// 0
|
15
|
+
NINE: 57,
|
16
|
+
// 9
|
17
|
+
A_UP: 65,
|
18
|
+
// A
|
19
|
+
F_UP: 70,
|
20
|
+
// F
|
21
|
+
A_LO: 97,
|
22
|
+
// a
|
23
|
+
F_LO: 102
|
24
|
+
// f
|
25
|
+
};
|
9
26
|
function bytesToHex(bytes) {
|
10
27
|
assertInstanceOf(bytes, Uint8Array);
|
11
28
|
let hex3 = "";
|
@@ -27,12 +44,11 @@ function hexToBytes(hex3) {
|
|
27
44
|
return bytes;
|
28
45
|
}
|
29
46
|
function charCodeToBase16(char) {
|
30
|
-
if (char >=
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
return char - (97 /* A_LO */ - 10);
|
47
|
+
if (char >= HexChar.ZERO && char <= HexChar.NINE) return char - HexChar.ZERO;
|
48
|
+
if (char >= HexChar.A_UP && char <= HexChar.F_UP)
|
49
|
+
return char - (HexChar.A_UP - 10);
|
50
|
+
if (char >= HexChar.A_LO && char <= HexChar.F_LO)
|
51
|
+
return char - (HexChar.A_LO - 10);
|
36
52
|
throw new Error("Invalid byte sequence.");
|
37
53
|
}
|
38
54
|
var hex = {
|
@@ -57,14 +73,14 @@ var bigintBE = {
|
|
57
73
|
*/
|
58
74
|
encode(data) {
|
59
75
|
const hexInput = hex$1.encode(data);
|
60
|
-
return BigInt(hexInput
|
76
|
+
return BigInt(hexInput === "" ? "0" : `0x${hexInput}`);
|
61
77
|
},
|
62
78
|
/**
|
63
79
|
* Decode a `BigInt` to a `Uint8Array`.
|
64
80
|
*/
|
65
81
|
decode(data) {
|
66
82
|
const hexData = data.toString(16);
|
67
|
-
return hex$1.decode(hexData.length % 2 ?
|
83
|
+
return hex$1.decode(hexData.length % 2 ? `0${hexData}` : hexData);
|
68
84
|
}
|
69
85
|
};
|
70
86
|
|
@@ -75,9 +91,7 @@ var base64 = base64$1;
|
|
75
91
|
|
76
92
|
// src/hashes.ts
|
77
93
|
function ensureBytes(input) {
|
78
|
-
|
79
|
-
return input;
|
80
|
-
return hex.decode(input);
|
94
|
+
return typeof input === "string" ? hex.decode(input) : input;
|
81
95
|
}
|
82
96
|
function blake2b256(message) {
|
83
97
|
return blake2b(ensureBytes(message), { dkLen: 32 });
|
@@ -86,8 +100,7 @@ function sha256(message) {
|
|
86
100
|
return sha256$1(ensureBytes(message));
|
87
101
|
}
|
88
102
|
function validateEcPoint(pointBytes) {
|
89
|
-
if (isEmpty(pointBytes))
|
90
|
-
return false;
|
103
|
+
if (isEmpty(pointBytes)) return false;
|
91
104
|
switch (pointBytes[0]) {
|
92
105
|
case 2 /* Compressed */:
|
93
106
|
case 3 /* CompressedOdd */:
|
@@ -102,6 +115,6 @@ function validateEcPoint(pointBytes) {
|
|
102
115
|
// src/index.ts
|
103
116
|
var randomBytes = randomBytes$1;
|
104
117
|
|
105
|
-
export { base58, base58check, base64, bigintBE, blake2b256, hex, randomBytes, sha256, utf8, validateEcPoint };
|
118
|
+
export { base58, base58check, base64, bigintBE, blake2b256, ensureBytes, hex, randomBytes, sha256, utf8, validateEcPoint };
|
106
119
|
//# sourceMappingURL=out.js.map
|
107
120
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hashes.ts","../src/coders/index.ts","../src/coders/hex.ts","../src/coders/utf8.ts","../src/coders/bigintBE.ts","../src/ecpoint.ts"],"names":["hex","assertInstanceOf","assertTypeOf"],"mappings":";AAAA,SAAS,eAAe,wBAAwB;;;ACAhD,SAAS,eAAe;AACxB,SAAS,UAAU,eAAe;;;ACDlC;AAAA,EACE,eAAe;AAAA,EACf,UAAU;AAAA,EACV,UAAU;AAAA,OACL;;;ACJP,SAAS,QAAQ,kBAAkB,oBAAoB;AAGvD,IAAM,QAAQ,MAAM,
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hashes.ts","../src/coders/index.ts","../src/coders/hex.ts","../src/coders/utf8.ts","../src/coders/bigintBE.ts","../src/ecpoint.ts"],"names":["hex","assertInstanceOf","assertTypeOf"],"mappings":";AAAA,SAAS,eAAe,wBAAwB;;;ACAhD,SAAS,eAAe;AACxB,SAAS,UAAU,eAAe;;;ACDlC;AAAA,EACE,eAAe;AAAA,EACf,UAAU;AAAA,EACV,UAAU;AAAA,OACL;;;ACJP,SAAS,QAAQ,kBAAkB,oBAAoB;AAGvD,IAAM,QAAQ,MAAM;AAAA,EAAK,EAAE,QAAQ,IAAI;AAAA,EAAG,CAAC,GAAG,MAC5C,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAChC;AAEA,IAAM,UAAU;AAAA,EACd,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AACR;AAEA,SAAS,WAAW,OA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;AAEO,IAAM,OAAmB;AAAA,EAC9B,QAAQ;AAAA,EACR,QAAQ;AACV;;;AClBA,SAAS,OAAAF,YAAW;AAMb,IAAM,WAAsC;AAAA;AAAA;AAAA;AAAA,EAIjD,OAAO,MAAM;AACX,UAAM,WAAWA,KAAI,OAAO,IAAI;AAChC,WAAO,OAAO,aAAa,KAAK,MAAM,KAAK,QAAQ,EAAE;AAAA,EACvD;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,MAAM;AACX,UAAM,UAAU,KAAK,SAAS,EAAE;AAChC,WAAOA,KAAI,OAAO,QAAQ,SAAS,IAAI,IAAI,OAAO,KAAK,OAAO;AAAA,EAChE;AACF;;;AHdO,IAAM,cAAc,iBAAiB,MAAM;AAC3C,IAAM,SAAS;AACf,IAAM,SAAS;;;ADLf,SAAS,YAAY,OAA8B;AACxD,SAAO,OAAO,UAAU,WAAW,IAAI,OAAO,KAAK,IAAI;AACzD;AAEO,SAAS,WAAW,SAAgC;AACzD,SAAO,QAAQ,YAAY,OAAO,GAAG,EAAE,OAAO,GAAG,CAAC;AACpD;AAEO,SAAS,OAAO,SAAgC;AACrD,SAAO,QAAQ,YAAY,OAAO,CAAC;AACrC;;;AKfA,SAAS,eAAe;AA0BjB,SAAS,gBAAgB,YAAwB;AACtD,MAAI,QAAQ,UAAU,EAAG,QAAO;AAEhC,UAAQ,WAAW,CAAC,GAAG;AAAA,IACrB,KAAK;AAAA,IACL,KAAK;AACH,aAAO,WAAW,WAAW;AAAA,IAC/B,KAAK;AACH,aAAO,WAAW,WAAW;AAAA,IAC/B;AACE,aAAO;AAAA,EACX;AACF;;;ANjCO,IAAM,cAAc","sourcesContent":["import { randomBytes as nobleRandomBytes } from \"@noble/hashes/utils\";\n\n/**\n * Secure PRNG from \"@noble/hashes\". Uses crypto.getRandomValues, which defers to OS.\n */\nexport const randomBytes = nobleRandomBytes as (\n bytesLength?: number\n) => Uint8Array;\n\nexport * from \"./hashes\";\nexport * from \"./types\";\nexport * from \"./coders\";\nexport * from \"./ecpoint\";\n","import { blake2b } from \"@noble/hashes/blake2b\";\nimport { sha256 as _sha256 } from \"@noble/hashes/sha256\";\nimport { hex } from \"./coders\";\nimport type { ByteInput } from \"./types\";\n\nexport function ensureBytes(input: ByteInput): Uint8Array {\n return typeof input === \"string\" ? hex.decode(input) : input;\n}\n\nexport function blake2b256(message: ByteInput): Uint8Array {\n return blake2b(ensureBytes(message), { dkLen: 32 });\n}\n\nexport function sha256(message: ByteInput): Uint8Array {\n return _sha256(ensureBytes(message));\n}\n","import {\n base58check as base58checkCoder,\n base58 as base58Coder,\n base64 as base64Coder\n} from \"@scure/base\";\nimport { sha256 } from \"../hashes\";\nimport type { BytesCoder } from \"../types\";\n\nexport const base58check = base58checkCoder(sha256);\nexport const base58 = base58Coder as BytesCoder;\nexport const base64 = base64Coder as BytesCoder;\n\nexport { hex } from \"./hex\";\nexport { utf8 } from \"./utf8\";\nexport { bigintBE } from \"./bigintBE\";\n","import { assert, assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport type { BytesCoder } from \"../types\";\n\nconst HEXES = Array.from({ length: 256 }, (_, i) =>\n i.toString(16).padStart(2, \"0\")\n);\n\nconst HexChar = {\n ZERO: 48, // 0\n NINE: 57, // 9\n A_UP: 65, // A\n F_UP: 70, // F\n A_LO: 97, // a\n F_LO: 102 // f\n} as const;\n\nfunction bytesToHex(bytes: Uint8Array): string {\n assertInstanceOf(bytes, Uint8Array);\n\n let hex = \"\";\n for (let i = 0, len = bytes.length; i < len; i++) {\n hex += HEXES[bytes[i]];\n }\n\n return hex;\n}\n\nfunction hexToBytes(hex: string): Uint8Array {\n assertTypeOf(hex, \"string\");\n assert(hex.length % 2 === 0, \"Invalid hex padding.\");\n\n const len = hex.length / 2;\n const bytes = new Uint8Array(len);\n for (let i = 0, j = 0; i < len; i++) {\n const n1 = charCodeToBase16(hex.charCodeAt(j++));\n const n2 = charCodeToBase16(hex.charCodeAt(j++));\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\nfunction charCodeToBase16(char: number) {\n if (char >= HexChar.ZERO && char <= HexChar.NINE) return char - HexChar.ZERO;\n if (char >= HexChar.A_UP && char <= HexChar.F_UP)\n return char - (HexChar.A_UP - 10);\n if (char >= HexChar.A_LO && char <= HexChar.F_LO)\n return char - (HexChar.A_LO - 10);\n\n throw new Error(\"Invalid byte sequence.\");\n}\n\nexport const hex: BytesCoder = {\n encode: bytesToHex,\n decode: hexToBytes\n};\n","import { assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport type { BytesCoder } from \"../types\";\n\nfunction bytesToUtf8(bytes: Uint8Array): string {\n assertInstanceOf(bytes, Uint8Array);\n\n return new TextDecoder().decode(bytes);\n}\n\nfunction utf8ToBytes(str: string): Uint8Array {\n assertTypeOf(str, \"string\");\n\n return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809\n}\n\nexport const utf8: BytesCoder = {\n encode: bytesToUtf8,\n decode: utf8ToBytes\n};\n","import { hex } from \"@scure/base\";\nimport type { Coder } from \"../types\";\n\n/**\n * A coder for Big Endian `BigInt` <> `Uint8Array` conversion..\n */\nexport const bigintBE: Coder<Uint8Array, bigint> = {\n /**\n * Encode a `Uint8Array` to a `BigInt`.\n */\n encode(data) {\n const hexInput = hex.encode(data);\n return BigInt(hexInput === \"\" ? \"0\" : `0x${hexInput}`);\n },\n\n /**\n * Decode a `BigInt` to a `Uint8Array`.\n */\n decode(data) {\n const hexData = data.toString(16);\n return hex.decode(hexData.length % 2 ? `0${hexData}` : hexData);\n }\n};\n","import { isEmpty } from \"@fleet-sdk/common\";\n\n/**\n * EC point type\n */\nenum EcPointType {\n /**\n * Compressed, positive Y coordinate\n */\n Compressed = 0x02,\n /**\n * Compressed, negative Y coordinate\n */\n CompressedOdd = 0x03,\n /**\n * Uncompressed\n */\n Uncompressed = 0x04\n}\n\n/**\n * Validate Elliptic Curve point\n *\n * @param pointBytes EC point bytes\n * @returns True if the point is valid\n */\nexport function validateEcPoint(pointBytes: Uint8Array) {\n if (isEmpty(pointBytes)) return false;\n\n switch (pointBytes[0]) {\n case EcPointType.Compressed:\n case EcPointType.CompressedOdd:\n return pointBytes.length === 33;\n case EcPointType.Uncompressed:\n return pointBytes.length === 65;\n default:\n return false;\n }\n}\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@fleet-sdk/crypto",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.0",
|
4
4
|
"description": "Ergo blockchain crypto primitives.",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"module": "./dist/index.mjs",
|
@@ -30,12 +30,12 @@
|
|
30
30
|
"crypto"
|
31
31
|
],
|
32
32
|
"engines": {
|
33
|
-
"node": ">=
|
33
|
+
"node": ">=18"
|
34
34
|
},
|
35
35
|
"dependencies": {
|
36
36
|
"@noble/hashes": "^1.4.0",
|
37
|
-
"@scure/base": "^1.1.
|
38
|
-
"@fleet-sdk/common": "^0.
|
37
|
+
"@scure/base": "^1.1.7",
|
38
|
+
"@fleet-sdk/common": "^0.4.1"
|
39
39
|
},
|
40
40
|
"files": [
|
41
41
|
"dist",
|