@atomicfinance/crypto 3.5.3 → 3.6.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/package.json +1 -1
- package/lib/index.js +0 -89
package/package.json
CHANGED
package/lib/index.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
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 (0, 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
|