@fleet-sdk/crypto 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +59 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/coders/hex.bench.ts +15 -0
- package/src/coders/hex.ts +37 -0
- package/src/coders/index.ts +18 -0
- package/src/coders/utf8.ts +19 -0
- package/src/hashes.ts +6 -0
- package/src/index.ts +10 -0
- package/src/types.ts +17 -0
package/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# @fleet-sdk/crypto
|
2
|
+
|
3
|
+
## 0.2.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- 8a13a29: Introduce `crypto` package with Ergo crypto primitives.
|
8
|
+
- 2ab9661: Add byte coders
|
9
|
+
- `base64` - encode/decode base64 string;
|
10
|
+
- `base58` - encode/decode base58 string;
|
11
|
+
- `base58check` - encode/decode base58check string;
|
12
|
+
- `hex` - encode/decode hex string;
|
13
|
+
- `utf8` - encode/decode hex string.
|
14
|
+
- 2ab9661: Add hashing functions:
|
15
|
+
- `blake2b256()`
|
16
|
+
- `sha256()`
|
17
|
+
|
18
|
+
### Patch Changes
|
19
|
+
|
20
|
+
- Updated dependencies [5a79c57]
|
21
|
+
- Updated dependencies [a491ab9]
|
22
|
+
- Updated dependencies [3236dd8]
|
23
|
+
- Updated dependencies [9bd393b]
|
24
|
+
- @fleet-sdk/common@0.2.0
|
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 Nautilus Team
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,3 @@
|
|
1
|
+
# @fleet-sdk/crypto [](https://github.com/fleet-sdk/fleet/blob/master/LICENSE) [](https://www.npmjs.com/package/@fleet-sdk/crypto)
|
2
|
+
|
3
|
+
Ergo blockchain crypto primitives.
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
import * as _scure_base from '@scure/base';
|
2
|
+
|
3
|
+
type BytesInput = Uint8Array | string;
|
4
|
+
interface Coder<F, T> {
|
5
|
+
encode(from: F): T;
|
6
|
+
decode(to: T): F;
|
7
|
+
}
|
8
|
+
interface BytesCoder extends Coder<Uint8Array, string> {
|
9
|
+
/**
|
10
|
+
* Encodes an array of bytes to a string
|
11
|
+
*/
|
12
|
+
encode: (data: Uint8Array) => string;
|
13
|
+
/**
|
14
|
+
* Decodes a string to an array of bytes
|
15
|
+
*/
|
16
|
+
decode: (str: string) => Uint8Array;
|
17
|
+
}
|
18
|
+
|
19
|
+
declare const blake2b256: (message: BytesInput) => Uint8Array;
|
20
|
+
declare const sha256: (message: BytesInput) => Uint8Array;
|
21
|
+
|
22
|
+
declare const hex: BytesCoder;
|
23
|
+
|
24
|
+
declare const utf8: BytesCoder;
|
25
|
+
|
26
|
+
declare const base58check: _scure_base.BytesCoder;
|
27
|
+
declare const base58: BytesCoder;
|
28
|
+
declare const base64: BytesCoder;
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Secure PRNG from "@noble/hashes". Uses crypto.getRandomValues, which defers to OS.
|
32
|
+
*/
|
33
|
+
declare const randomBytes: (bytesLength?: number) => Uint8Array;
|
34
|
+
|
35
|
+
export { BytesCoder, BytesInput, Coder, base58, base58check, base64, blake2b256, hex, randomBytes, sha256, utf8 };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
import * as _scure_base from '@scure/base';
|
2
|
+
|
3
|
+
type BytesInput = Uint8Array | string;
|
4
|
+
interface Coder<F, T> {
|
5
|
+
encode(from: F): T;
|
6
|
+
decode(to: T): F;
|
7
|
+
}
|
8
|
+
interface BytesCoder extends Coder<Uint8Array, string> {
|
9
|
+
/**
|
10
|
+
* Encodes an array of bytes to a string
|
11
|
+
*/
|
12
|
+
encode: (data: Uint8Array) => string;
|
13
|
+
/**
|
14
|
+
* Decodes a string to an array of bytes
|
15
|
+
*/
|
16
|
+
decode: (str: string) => Uint8Array;
|
17
|
+
}
|
18
|
+
|
19
|
+
declare const blake2b256: (message: BytesInput) => Uint8Array;
|
20
|
+
declare const sha256: (message: BytesInput) => Uint8Array;
|
21
|
+
|
22
|
+
declare const hex: BytesCoder;
|
23
|
+
|
24
|
+
declare const utf8: BytesCoder;
|
25
|
+
|
26
|
+
declare const base58check: _scure_base.BytesCoder;
|
27
|
+
declare const base58: BytesCoder;
|
28
|
+
declare const base64: BytesCoder;
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Secure PRNG from "@noble/hashes". Uses crypto.getRandomValues, which defers to OS.
|
32
|
+
*/
|
33
|
+
declare const randomBytes: (bytesLength?: number) => Uint8Array;
|
34
|
+
|
35
|
+
export { BytesCoder, BytesInput, Coder, base58, base58check, base64, blake2b256, hex, randomBytes, sha256, utf8 };
|
package/dist/index.js
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var utils = require('@noble/hashes/utils');
|
4
|
+
var blake2b = require('@noble/hashes/blake2b');
|
5
|
+
var sha256$1 = require('@noble/hashes/sha256');
|
6
|
+
var base = require('@scure/base');
|
7
|
+
var common = require('@fleet-sdk/common');
|
8
|
+
|
9
|
+
// src/index.ts
|
10
|
+
var blake2b256 = (message) => blake2b.blake2b(message, { dkLen: 32 });
|
11
|
+
var sha256 = sha256$1.sha256;
|
12
|
+
var HEXES = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
13
|
+
function bytesToHex(bytes) {
|
14
|
+
common.assertInstanceOf(bytes, Uint8Array);
|
15
|
+
let hex2 = "";
|
16
|
+
for (let i = 0; i < bytes.length; i++) {
|
17
|
+
hex2 += HEXES[bytes[i]];
|
18
|
+
}
|
19
|
+
return hex2;
|
20
|
+
}
|
21
|
+
function hexToBytes(hex2) {
|
22
|
+
common.assertTypeOf(hex2, "string");
|
23
|
+
common.assert(hex2.length % 2 === 0, "Invalid hex padding.");
|
24
|
+
const bytes = new Uint8Array(hex2.length / 2);
|
25
|
+
for (let i = 0; i < bytes.length; i++) {
|
26
|
+
const j = i * 2;
|
27
|
+
const hexByte = hex2.slice(j, j + 2);
|
28
|
+
const byte = parseInt(hexByte, 16);
|
29
|
+
common.assert(!isNaN(byte) && byte >= 0, "Invalid byte sequence.");
|
30
|
+
bytes[i] = byte;
|
31
|
+
}
|
32
|
+
return bytes;
|
33
|
+
}
|
34
|
+
var hex = {
|
35
|
+
encode: bytesToHex,
|
36
|
+
decode: hexToBytes
|
37
|
+
};
|
38
|
+
function bytesToUtf8(bytes) {
|
39
|
+
common.assertInstanceOf(bytes, Uint8Array);
|
40
|
+
return new TextDecoder().decode(bytes);
|
41
|
+
}
|
42
|
+
function utf8ToBytes(str) {
|
43
|
+
common.assertTypeOf(str, "string");
|
44
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
45
|
+
}
|
46
|
+
var utf8 = {
|
47
|
+
encode: bytesToUtf8,
|
48
|
+
decode: utf8ToBytes
|
49
|
+
};
|
50
|
+
|
51
|
+
// src/coders/index.ts
|
52
|
+
var base58check = base.base58check(sha256);
|
53
|
+
var base58 = base.base58;
|
54
|
+
var base64 = base.base64;
|
55
|
+
|
56
|
+
// src/index.ts
|
57
|
+
var randomBytes = utils.randomBytes;
|
58
|
+
|
59
|
+
exports.base58 = base58;
|
60
|
+
exports.base58check = base58check;
|
61
|
+
exports.base64 = base64;
|
62
|
+
exports.blake2b256 = blake2b256;
|
63
|
+
exports.hex = hex;
|
64
|
+
exports.randomBytes = randomBytes;
|
65
|
+
exports.sha256 = sha256;
|
66
|
+
exports.utf8 = utf8;
|
67
|
+
//# sourceMappingURL=out.js.map
|
68
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hashes.ts","../src/coders/index.ts","../src/coders/hex.ts","../src/coders/utf8.ts"],"names":["hex","assertInstanceOf","assertTypeOf"],"mappings":";AAAA,SAAS,eAAe,wBAAwB;;;ACAhD,SAAS,eAAe;AACxB,SAAS,UAAU,mBAAmB;AAG/B,IAAM,aAAa,CAAC,YAAwB,QAAQ,SAAS,EAAE,OAAO,GAAG,CAAC;AAC1E,IAAM,SAAS;;;ACLtB;AAAA,EACE,eAAe;AAAA,EACf,UAAU;AAAA,EACV,UAAU;AAAA,OACL;;;ACJP,SAAS,QAAQ,kBAAkB,oBAAoB;AAGvD,IAAM,QAAQ,MAAM,KAAK,EAAE,QAAQ,IAAI,GAAG,CAAC,GAAG,MAAM,EAA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from \"@noble/hashes\". Uses crypto.getRandomValues, which defers to OS.\n */\nexport const randomBytes = nobleRandomBytes as (bytesLength?: number) => Uint8Array;\n\nexport * from \"./hashes\";\nexport * from \"./types\";\nexport * from \"./coders\";\n","import { blake2b } from \"@noble/hashes/blake2b\";\nimport { sha256 as nobleSha256 } from \"@noble/hashes/sha256\";\nimport { BytesInput } from \"./types\";\n\nexport const blake2b256 = (message: BytesInput) => blake2b(message, { dkLen: 32 });\nexport const sha256 = nobleSha256 as (message: BytesInput) => Uint8Array;\n","import {\n base58check as base58checkCoder,\n base58 as base58Coder,\n base64 as base64Coder\n} from \"@scure/base\";\nimport { sha256 } from \"../hashes\";\nimport { BytesCoder } from \"../types\";\n\nexport const base58check = base58checkCoder(sha256);\nexport const base58 = base58Coder as BytesCoder;\nexport const base64 = base64Coder as BytesCoder;\n\n// export const base64utf8: BytesCoder = {\n// encode: (data: Uint8Array) => utf8.decode()\n// }\n\nexport { hex } from \"./hex\";\nexport { utf8 } from \"./utf8\";\n","import { assert, assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport { BytesCoder } from \"../types\";\n\nconst HEXES = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, \"0\"));\n\nfunction bytesToHex(bytes: Uint8Array): string {\n assertInstanceOf(bytes, Uint8Array);\n\n let hex = \"\";\n for (let i = 0; i < bytes.length; 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 bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < bytes.length; i++) {\n const j = i * 2;\n const hexByte = hex.slice(j, j + 2);\n const byte = parseInt(hexByte, 16);\n assert(!isNaN(byte) && byte >= 0, \"Invalid byte sequence.\");\n\n bytes[i] = byte;\n }\n\n return bytes;\n}\n\nexport const hex: BytesCoder = {\n encode: bytesToHex,\n decode: hexToBytes\n};\n","import { assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport { 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"]}
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
import { randomBytes as randomBytes$1 } from '@noble/hashes/utils';
|
2
|
+
import { blake2b } from '@noble/hashes/blake2b';
|
3
|
+
import { sha256 as sha256$1 } from '@noble/hashes/sha256';
|
4
|
+
import { base58check as base58check$1, base58 as base58$1, base64 as base64$1 } from '@scure/base';
|
5
|
+
import { assertInstanceOf, assertTypeOf, assert } from '@fleet-sdk/common';
|
6
|
+
|
7
|
+
// src/index.ts
|
8
|
+
var blake2b256 = (message) => blake2b(message, { dkLen: 32 });
|
9
|
+
var sha256 = sha256$1;
|
10
|
+
var HEXES = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
11
|
+
function bytesToHex(bytes) {
|
12
|
+
assertInstanceOf(bytes, Uint8Array);
|
13
|
+
let hex2 = "";
|
14
|
+
for (let i = 0; i < bytes.length; i++) {
|
15
|
+
hex2 += HEXES[bytes[i]];
|
16
|
+
}
|
17
|
+
return hex2;
|
18
|
+
}
|
19
|
+
function hexToBytes(hex2) {
|
20
|
+
assertTypeOf(hex2, "string");
|
21
|
+
assert(hex2.length % 2 === 0, "Invalid hex padding.");
|
22
|
+
const bytes = new Uint8Array(hex2.length / 2);
|
23
|
+
for (let i = 0; i < bytes.length; i++) {
|
24
|
+
const j = i * 2;
|
25
|
+
const hexByte = hex2.slice(j, j + 2);
|
26
|
+
const byte = parseInt(hexByte, 16);
|
27
|
+
assert(!isNaN(byte) && byte >= 0, "Invalid byte sequence.");
|
28
|
+
bytes[i] = byte;
|
29
|
+
}
|
30
|
+
return bytes;
|
31
|
+
}
|
32
|
+
var hex = {
|
33
|
+
encode: bytesToHex,
|
34
|
+
decode: hexToBytes
|
35
|
+
};
|
36
|
+
function bytesToUtf8(bytes) {
|
37
|
+
assertInstanceOf(bytes, Uint8Array);
|
38
|
+
return new TextDecoder().decode(bytes);
|
39
|
+
}
|
40
|
+
function utf8ToBytes(str) {
|
41
|
+
assertTypeOf(str, "string");
|
42
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
43
|
+
}
|
44
|
+
var utf8 = {
|
45
|
+
encode: bytesToUtf8,
|
46
|
+
decode: utf8ToBytes
|
47
|
+
};
|
48
|
+
|
49
|
+
// src/coders/index.ts
|
50
|
+
var base58check = base58check$1(sha256);
|
51
|
+
var base58 = base58$1;
|
52
|
+
var base64 = base64$1;
|
53
|
+
|
54
|
+
// src/index.ts
|
55
|
+
var randomBytes = randomBytes$1;
|
56
|
+
|
57
|
+
export { base58, base58check, base64, blake2b256, hex, randomBytes, sha256, utf8 };
|
58
|
+
//# sourceMappingURL=out.js.map
|
59
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hashes.ts","../src/coders/index.ts","../src/coders/hex.ts","../src/coders/utf8.ts"],"names":["hex","assertInstanceOf","assertTypeOf"],"mappings":";AAAA,SAAS,eAAe,wBAAwB;;;ACAhD,SAAS,eAAe;AACxB,SAAS,UAAU,mBAAmB;AAG/B,IAAM,aAAa,CAAC,YAAwB,QAAQ,SAAS,EAAE,OAAO,GAAG,CAAC;AAC1E,IAAM,SAAS;;;ACLtB;AAAA,EACE,eAAe;AAAA,EACf,UAAU;AAAA,EACV,UAAU;AAAA,OACL;;;ACJP,SAAS,QAAQ,kBAAkB,oBAAoB;AAGvD,IAAM,QAAQ,MAAM,KAAK,EAAE,QAAQ,IAAI,GAAG,CAAC,G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/**\n * Secure PRNG from \"@noble/hashes\". Uses crypto.getRandomValues, which defers to OS.\n */\nexport const randomBytes = nobleRandomBytes as (bytesLength?: number) => Uint8Array;\n\nexport * from \"./hashes\";\nexport * from \"./types\";\nexport * from \"./coders\";\n","import { blake2b } from \"@noble/hashes/blake2b\";\nimport { sha256 as nobleSha256 } from \"@noble/hashes/sha256\";\nimport { BytesInput } from \"./types\";\n\nexport const blake2b256 = (message: BytesInput) => blake2b(message, { dkLen: 32 });\nexport const sha256 = nobleSha256 as (message: BytesInput) => Uint8Array;\n","import {\n base58check as base58checkCoder,\n base58 as base58Coder,\n base64 as base64Coder\n} from \"@scure/base\";\nimport { sha256 } from \"../hashes\";\nimport { BytesCoder } from \"../types\";\n\nexport const base58check = base58checkCoder(sha256);\nexport const base58 = base58Coder as BytesCoder;\nexport const base64 = base64Coder as BytesCoder;\n\n// export const base64utf8: BytesCoder = {\n// encode: (data: Uint8Array) => utf8.decode()\n// }\n\nexport { hex } from \"./hex\";\nexport { utf8 } from \"./utf8\";\n","import { assert, assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport { BytesCoder } from \"../types\";\n\nconst HEXES = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, \"0\"));\n\nfunction bytesToHex(bytes: Uint8Array): string {\n assertInstanceOf(bytes, Uint8Array);\n\n let hex = \"\";\n for (let i = 0; i < bytes.length; 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 bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < bytes.length; i++) {\n const j = i * 2;\n const hexByte = hex.slice(j, j + 2);\n const byte = parseInt(hexByte, 16);\n assert(!isNaN(byte) && byte >= 0, \"Invalid byte sequence.\");\n\n bytes[i] = byte;\n }\n\n return bytes;\n}\n\nexport const hex: BytesCoder = {\n encode: bytesToHex,\n decode: hexToBytes\n};\n","import { assertInstanceOf, assertTypeOf } from \"@fleet-sdk/common\";\nimport { 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"]}
|
package/package.json
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
{
|
2
|
+
"name": "@fleet-sdk/crypto",
|
3
|
+
"version": "0.2.0",
|
4
|
+
"description": "Ergo blockchain crypto primitives.",
|
5
|
+
"main": "./dist/index.js",
|
6
|
+
"module": "./dist/index.mjs",
|
7
|
+
"types": "./dist/index.d.ts",
|
8
|
+
"exports": {
|
9
|
+
"require": "./dist/index.js",
|
10
|
+
"import": "./dist/index.mjs"
|
11
|
+
},
|
12
|
+
"sideEffects": true,
|
13
|
+
"repository": "fleet-sdk/fleet",
|
14
|
+
"license": "MIT",
|
15
|
+
"publishConfig": {
|
16
|
+
"access": "public",
|
17
|
+
"provenance": true
|
18
|
+
},
|
19
|
+
"keywords": [
|
20
|
+
"ergo",
|
21
|
+
"blockchain",
|
22
|
+
"crypto"
|
23
|
+
],
|
24
|
+
"engines": {
|
25
|
+
"node": ">=14"
|
26
|
+
},
|
27
|
+
"dependencies": {
|
28
|
+
"@noble/hashes": "^1.3.1",
|
29
|
+
"@scure/base": "^1.1.1",
|
30
|
+
"@fleet-sdk/common": "^0.2.0"
|
31
|
+
},
|
32
|
+
"files": [
|
33
|
+
"src",
|
34
|
+
"dist",
|
35
|
+
"!**/*.spec.*",
|
36
|
+
"!**/*.json",
|
37
|
+
"!tests",
|
38
|
+
"CHANGELOG.md",
|
39
|
+
"LICENSE",
|
40
|
+
"README.md"
|
41
|
+
],
|
42
|
+
"scripts": {
|
43
|
+
"build": "tsup"
|
44
|
+
}
|
45
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { hex as scureHex } from "@scure/base";
|
2
|
+
import { bench, describe } from "vitest";
|
3
|
+
import { hex as fleetHex } from "./hex";
|
4
|
+
|
5
|
+
describe("HEX <> Bytes decoding", () => {
|
6
|
+
const validHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7";
|
7
|
+
|
8
|
+
bench("@scure implementation", () => {
|
9
|
+
scureHex.encode(scureHex.decode(validHex));
|
10
|
+
});
|
11
|
+
|
12
|
+
bench("Fleet implementation", () => {
|
13
|
+
fleetHex.encode(fleetHex.decode(validHex));
|
14
|
+
});
|
15
|
+
});
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { assert, assertInstanceOf, assertTypeOf } from "@fleet-sdk/common";
|
2
|
+
import { BytesCoder } from "../types";
|
3
|
+
|
4
|
+
const HEXES = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
5
|
+
|
6
|
+
function bytesToHex(bytes: Uint8Array): string {
|
7
|
+
assertInstanceOf(bytes, Uint8Array);
|
8
|
+
|
9
|
+
let hex = "";
|
10
|
+
for (let i = 0; i < bytes.length; i++) {
|
11
|
+
hex += HEXES[bytes[i]];
|
12
|
+
}
|
13
|
+
|
14
|
+
return hex;
|
15
|
+
}
|
16
|
+
|
17
|
+
function hexToBytes(hex: string): Uint8Array {
|
18
|
+
assertTypeOf(hex, "string");
|
19
|
+
assert(hex.length % 2 === 0, "Invalid hex padding.");
|
20
|
+
|
21
|
+
const bytes = new Uint8Array(hex.length / 2);
|
22
|
+
for (let i = 0; i < bytes.length; i++) {
|
23
|
+
const j = i * 2;
|
24
|
+
const hexByte = hex.slice(j, j + 2);
|
25
|
+
const byte = parseInt(hexByte, 16);
|
26
|
+
assert(!isNaN(byte) && byte >= 0, "Invalid byte sequence.");
|
27
|
+
|
28
|
+
bytes[i] = byte;
|
29
|
+
}
|
30
|
+
|
31
|
+
return bytes;
|
32
|
+
}
|
33
|
+
|
34
|
+
export const hex: BytesCoder = {
|
35
|
+
encode: bytesToHex,
|
36
|
+
decode: hexToBytes
|
37
|
+
};
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import {
|
2
|
+
base58check as base58checkCoder,
|
3
|
+
base58 as base58Coder,
|
4
|
+
base64 as base64Coder
|
5
|
+
} from "@scure/base";
|
6
|
+
import { sha256 } from "../hashes";
|
7
|
+
import { BytesCoder } from "../types";
|
8
|
+
|
9
|
+
export const base58check = base58checkCoder(sha256);
|
10
|
+
export const base58 = base58Coder as BytesCoder;
|
11
|
+
export const base64 = base64Coder as BytesCoder;
|
12
|
+
|
13
|
+
// export const base64utf8: BytesCoder = {
|
14
|
+
// encode: (data: Uint8Array) => utf8.decode()
|
15
|
+
// }
|
16
|
+
|
17
|
+
export { hex } from "./hex";
|
18
|
+
export { utf8 } from "./utf8";
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { assertInstanceOf, assertTypeOf } from "@fleet-sdk/common";
|
2
|
+
import { BytesCoder } from "../types";
|
3
|
+
|
4
|
+
function bytesToUtf8(bytes: Uint8Array): string {
|
5
|
+
assertInstanceOf(bytes, Uint8Array);
|
6
|
+
|
7
|
+
return new TextDecoder().decode(bytes);
|
8
|
+
}
|
9
|
+
|
10
|
+
function utf8ToBytes(str: string): Uint8Array {
|
11
|
+
assertTypeOf(str, "string");
|
12
|
+
|
13
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
14
|
+
}
|
15
|
+
|
16
|
+
export const utf8: BytesCoder = {
|
17
|
+
encode: bytesToUtf8,
|
18
|
+
decode: utf8ToBytes
|
19
|
+
};
|
package/src/hashes.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import { blake2b } from "@noble/hashes/blake2b";
|
2
|
+
import { sha256 as nobleSha256 } from "@noble/hashes/sha256";
|
3
|
+
import { BytesInput } from "./types";
|
4
|
+
|
5
|
+
export const blake2b256 = (message: BytesInput) => blake2b(message, { dkLen: 32 });
|
6
|
+
export const sha256 = nobleSha256 as (message: BytesInput) => Uint8Array;
|
package/src/index.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Secure PRNG from "@noble/hashes". Uses crypto.getRandomValues, which defers to OS.
|
5
|
+
*/
|
6
|
+
export const randomBytes = nobleRandomBytes as (bytesLength?: number) => Uint8Array;
|
7
|
+
|
8
|
+
export * from "./hashes";
|
9
|
+
export * from "./types";
|
10
|
+
export * from "./coders";
|
package/src/types.ts
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
export type BytesInput = Uint8Array | string;
|
2
|
+
|
3
|
+
export interface Coder<F, T> {
|
4
|
+
encode(from: F): T;
|
5
|
+
decode(to: T): F;
|
6
|
+
}
|
7
|
+
|
8
|
+
export interface BytesCoder extends Coder<Uint8Array, string> {
|
9
|
+
/**
|
10
|
+
* Encodes an array of bytes to a string
|
11
|
+
*/
|
12
|
+
encode: (data: Uint8Array) => string;
|
13
|
+
/**
|
14
|
+
* Decodes a string to an array of bytes
|
15
|
+
*/
|
16
|
+
decode: (str: string) => Uint8Array;
|
17
|
+
}
|