@atomicfinance/crypto 3.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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @atomicfinance/crypto
2
+
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - a06082e: Create unified standalone `bitcoin-abstraction-layer` package
@@ -0,0 +1,47 @@
1
+ /// <reference types="node" />
2
+ import bech32 from 'bech32';
3
+ import base58 from 'bs58';
4
+ declare function isHex(hex: string): boolean;
5
+ /**
6
+ * Ensure message is in buffer format.
7
+ * @param {string} message - any string.
8
+ * @return {string} Returns Buffer.
9
+ */
10
+ declare function ensureBuffer(message: string | Buffer | any): false | Buffer;
11
+ /**
12
+ * Get hash160 of message.
13
+ * @param {!string|Buffer} message - message in string or Buffer.
14
+ * @return {string} Returns the hash160 of a string.
15
+ */
16
+ declare function hash160(message: Buffer): any;
17
+ /**
18
+ * Get sha256 of message.
19
+ * @param {!string|Buffer} message - message in string or Buffer.
20
+ * @return {string} Returns the sha256 of a string.
21
+ */
22
+ declare function sha256(message: string | Buffer): any;
23
+ /**
24
+ * Get ripemd160 of message.
25
+ * @param {!string|Buffer} message - message in string or Buffer.
26
+ * @return {string} Returns the ripemd160 of a string.
27
+ */
28
+ declare function ripemd160(message: string | Buffer): any;
29
+ /**
30
+ * Pad a hex string with '0'
31
+ * @param {string} hex - The hex string to pad.
32
+ * @param {number} lengthBytes - The length of the final string in bytes
33
+ * @return {string} Returns a padded string with length greater or equal to the given length
34
+ * rounded up to the nearest even number.
35
+ */
36
+ declare function padHexStart(hex: string, lengthBytes?: number): string;
37
+ export {
38
+ /**
39
+ * Base58 object with decode, decodeUnsafe, and encode functions.
40
+ */
41
+ base58,
42
+ /**
43
+ * Get bech32 of message.
44
+ * @param {!string} message - any string.
45
+ * @return {string} Returns the bech32 of a string.
46
+ */
47
+ bech32, sha256, ripemd160, hash160, ensureBuffer, padHexStart, isHex, };
package/dist/index.js ADDED
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isHex = exports.padHexStart = exports.ensureBuffer = exports.hash160 = exports.ripemd160 = exports.sha256 = exports.bech32 = exports.base58 = void 0;
7
+ const bech32_1 = __importDefault(require("bech32"));
8
+ exports.bech32 = bech32_1.default;
9
+ const bs58_1 = __importDefault(require("bs58"));
10
+ exports.base58 = bs58_1.default;
11
+ const crypto_hashing_1 = __importDefault(require("crypto-hashing"));
12
+ function isHex(hex) {
13
+ if (!hex.match(/([0-9]|[a-f])/gim))
14
+ return false;
15
+ const buf = Buffer.from(hex, 'hex').toString('hex');
16
+ return buf === hex.toLowerCase();
17
+ }
18
+ exports.isHex = isHex;
19
+ /**
20
+ * Ensure message is in buffer format.
21
+ * @param {string} message - any string.
22
+ * @return {string} Returns Buffer.
23
+ */
24
+ function ensureBuffer(message) {
25
+ if (Buffer.isBuffer(message))
26
+ return message;
27
+ switch (typeof message) {
28
+ case 'string':
29
+ message = isHex(message)
30
+ ? Buffer.from(message, 'hex')
31
+ : Buffer.from(message);
32
+ break;
33
+ case 'object':
34
+ message = Buffer.from(JSON.stringify(message));
35
+ break;
36
+ }
37
+ return Buffer.isBuffer(message) ? message : false;
38
+ }
39
+ exports.ensureBuffer = ensureBuffer;
40
+ /**
41
+ * Get hash of a message in hex.
42
+ * @param {!string} algorithm - Hashing algorithm.
43
+ * @param {!string|Buffer} message - Message to be hashed.
44
+ * @return {string} Returns the hash of a string.
45
+ */
46
+ function hashToHex(algorithm, message) {
47
+ return crypto_hashing_1.default(algorithm, ensureBuffer(message)).toString('hex');
48
+ }
49
+ /**
50
+ * Get hash160 of message.
51
+ * @param {!string|Buffer} message - message in string or Buffer.
52
+ * @return {string} Returns the hash160 of a string.
53
+ */
54
+ function hash160(message) {
55
+ return hashToHex('hash160', message);
56
+ }
57
+ exports.hash160 = hash160;
58
+ /**
59
+ * Get sha256 of message.
60
+ * @param {!string|Buffer} message - message in string or Buffer.
61
+ * @return {string} Returns the sha256 of a string.
62
+ */
63
+ function sha256(message) {
64
+ return hashToHex('sha256', message);
65
+ }
66
+ exports.sha256 = sha256;
67
+ /**
68
+ * Get ripemd160 of message.
69
+ * @param {!string|Buffer} message - message in string or Buffer.
70
+ * @return {string} Returns the ripemd160 of a string.
71
+ */
72
+ function ripemd160(message) {
73
+ return hashToHex('ripemd160', message);
74
+ }
75
+ exports.ripemd160 = ripemd160;
76
+ /**
77
+ * Pad a hex string with '0'
78
+ * @param {string} hex - The hex string to pad.
79
+ * @param {number} lengthBytes - The length of the final string in bytes
80
+ * @return {string} Returns a padded string with length greater or equal to the given length
81
+ * rounded up to the nearest even number.
82
+ */
83
+ function padHexStart(hex, lengthBytes) {
84
+ let lengthString = lengthBytes * 2 || hex.length;
85
+ lengthString += lengthString % 2;
86
+ return hex.padStart(lengthString, '0');
87
+ }
88
+ exports.padHexStart = padHexStart;
89
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AA+F1B,iBA/FK,gBAAM,CA+FL;AA9FR,gDAA0B;AAwFxB,iBAxFK,cAAM,CAwFL;AAvFR,oEAAwC;AAExC,SAAS,KAAK,CAAC,GAAW;IACxB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,kBAAkB,CAAC;QAAE,OAAO,KAAK,CAAC;IAEjD,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAEpD,OAAO,GAAG,KAAK,GAAG,CAAC,WAAW,EAAE,CAAC;AACnC,CAAC;AA2FC,sBAAK;AAzFP;;;;GAIG;AACH,SAAS,YAAY,CAAC,OAA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AI,GAAG,CAAC,MAAM,CAAC;IACjD,YAAY,IAAI,YAAY,GAAG,CAAC,CAAC;IAEjC,OAAO,GAAG,CAAC,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;AACzC,CAAC;AAiBC,kCAAW"}
@@ -0,0 +1 @@
1
+ declare module 'crypto-hashing'
package/lib/index.ts ADDED
@@ -0,0 +1,103 @@
1
+ import bech32 from 'bech32';
2
+ import base58 from 'bs58';
3
+ import cryptoHash from 'crypto-hashing';
4
+
5
+ function isHex(hex: string) {
6
+ if (!hex.match(/([0-9]|[a-f])/gim)) return false;
7
+
8
+ const buf = Buffer.from(hex, 'hex').toString('hex');
9
+
10
+ return buf === hex.toLowerCase();
11
+ }
12
+
13
+ /**
14
+ * Ensure message is in buffer format.
15
+ * @param {string} message - any string.
16
+ * @return {string} Returns Buffer.
17
+ */
18
+ function ensureBuffer(message: string | Buffer | any) {
19
+ if (Buffer.isBuffer(message)) return message;
20
+
21
+ switch (typeof message) {
22
+ case 'string':
23
+ message = isHex(message)
24
+ ? Buffer.from(message, 'hex')
25
+ : Buffer.from(message);
26
+ break;
27
+ case 'object':
28
+ message = Buffer.from(JSON.stringify(message));
29
+ break;
30
+ }
31
+
32
+ return Buffer.isBuffer(message) ? message : false;
33
+ }
34
+
35
+ /**
36
+ * Get hash of a message in hex.
37
+ * @param {!string} algorithm - Hashing algorithm.
38
+ * @param {!string|Buffer} message - Message to be hashed.
39
+ * @return {string} Returns the hash of a string.
40
+ */
41
+ function hashToHex(algorithm: string, message: string | Buffer) {
42
+ return cryptoHash(algorithm, ensureBuffer(message)).toString('hex');
43
+ }
44
+
45
+ /**
46
+ * Get hash160 of message.
47
+ * @param {!string|Buffer} message - message in string or Buffer.
48
+ * @return {string} Returns the hash160 of a string.
49
+ */
50
+ function hash160(message: Buffer) {
51
+ return hashToHex('hash160', message);
52
+ }
53
+
54
+ /**
55
+ * Get sha256 of message.
56
+ * @param {!string|Buffer} message - message in string or Buffer.
57
+ * @return {string} Returns the sha256 of a string.
58
+ */
59
+ function sha256(message: string | Buffer) {
60
+ return hashToHex('sha256', message);
61
+ }
62
+
63
+ /**
64
+ * Get ripemd160 of message.
65
+ * @param {!string|Buffer} message - message in string or Buffer.
66
+ * @return {string} Returns the ripemd160 of a string.
67
+ */
68
+ function ripemd160(message: string | Buffer) {
69
+ return hashToHex('ripemd160', message);
70
+ }
71
+
72
+ /**
73
+ * Pad a hex string with '0'
74
+ * @param {string} hex - The hex string to pad.
75
+ * @param {number} lengthBytes - The length of the final string in bytes
76
+ * @return {string} Returns a padded string with length greater or equal to the given length
77
+ * rounded up to the nearest even number.
78
+ */
79
+ function padHexStart(hex: string, lengthBytes?: number) {
80
+ let lengthString = lengthBytes * 2 || hex.length;
81
+ lengthString += lengthString % 2;
82
+
83
+ return hex.padStart(lengthString, '0');
84
+ }
85
+
86
+ export {
87
+ /**
88
+ * Base58 object with decode, decodeUnsafe, and encode functions.
89
+ */
90
+ base58,
91
+ /**
92
+ * Get bech32 of message.
93
+ * @param {!string} message - any string.
94
+ * @return {string} Returns the bech32 of a string.
95
+ */
96
+ bech32,
97
+ sha256,
98
+ ripemd160,
99
+ hash160,
100
+ ensureBuffer,
101
+ padHexStart,
102
+ isHex,
103
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@atomicfinance/crypto",
3
+ "version": "3.0.0",
4
+ "description": "",
5
+ "module": "dist/index.js",
6
+ "main": "dist/index.js",
7
+ "directories": {
8
+ "dist": "dist",
9
+ "src": "lib"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "lib"
14
+ ],
15
+ "scripts": {
16
+ "build": "../../node_modules/.bin/tsc --project tsconfig.json",
17
+ "test": "../../node_modules/.bin/nyc --reporter=lcov --reporter=text --extension=.ts ../../node_modules/.bin/mocha --recursive \"tests/**/*.test.*\"",
18
+ "lint": "../../node_modules/.bin/eslint --ignore-path ../../.eslintignore -c ../../.eslintrc.js .",
19
+ "lint:fix": "../../node_modules/.bin/eslint --fix --ignore-path ../../.eslintignore -c ../../.eslintrc.js ."
20
+ },
21
+ "author": "Atomic Finance <info@atomic.finance>",
22
+ "license": "MIT",
23
+ "engines": {
24
+ "node": ">=14"
25
+ },
26
+ "dependencies": {
27
+ "bech32": "^1.1.3",
28
+ "bs58": "^4.0.1",
29
+ "crypto-hashing": "^1.0.0"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "devDependencies": {
35
+ "@types/bech32": "^1.1.2",
36
+ "@types/bs58": "^4.0.1"
37
+ },
38
+ "sideEffects": false
39
+ }