|
1 |
| -import { bech32 } from '@scure/base' |
2 | 1 | import crypto from 'crypto'
|
3 |
| -import cryptojs from 'crypto-js' |
4 |
| - |
5 |
| -const ripemd160 = cryptojs.RIPEMD160, |
6 |
| - sha256 = cryptojs.SHA256, |
7 |
| - hexEncoding = cryptojs.enc.Hex |
8 |
| - |
9 |
| -/** |
10 |
| - * Convert a string to an array of bytes. |
11 |
| - * |
12 |
| - * @param {string} string The input string to be converted. |
13 |
| - * @returns {number[]} The bytes representing the input string. |
14 |
| - */ |
15 |
| -export const getBytes = (string: string): number[] => { |
16 |
| - const arrayBytes: number[] = [] |
17 |
| - const buffer = Buffer.from(string, 'utf16le') |
18 |
| - for (let i = 0; i < buffer.length; i++) { |
19 |
| - arrayBytes.push(buffer[i]) |
20 |
| - } |
21 |
| - return arrayBytes |
22 |
| -} |
23 |
| - |
24 |
| -/** |
25 |
| - * Convert a Buffer to a hexadecimal string. |
26 |
| - * |
27 |
| - * @param {Buffer} arr The input Buffer to be converted. |
28 |
| - * @returns {string} The hexadecimal string representing the input Buffer. |
29 |
| - */ |
30 |
| -export const ab2hexstring = (arr: Buffer): string => { |
31 |
| - let result = '' |
32 |
| - for (let i = 0; i < arr.length; i++) { |
33 |
| - let str = arr[i].toString(16) |
34 |
| - str = str.length === 0 ? '00' : str.length === 1 ? '0' + str : str |
35 |
| - result += str |
36 |
| - } |
37 |
| - return result |
38 |
| -} |
39 |
| - |
40 |
| -/** |
41 |
| - * Calculate `ripemd160(sha256(hex))` from a hexadecimal string. |
42 |
| - * |
43 |
| - * @param {string} hex The input hexadecimal string. |
44 |
| - * @returns {string} The result of the hash operation. |
45 |
| - * |
46 |
| - * @throws {"sha256ripemd160 expects a string"} Thrown if a non-string input is provided. |
47 |
| - * @throws {"invalid hex string length"} Thrown if the input hexadecimal string has an invalid length. |
48 |
| - */ |
49 |
| -export const sha256ripemd160 = (hex: string): string => { |
50 |
| - if (typeof hex !== 'string') throw new Error('sha256ripemd160 expects a string') |
51 |
| - if (hex.length % 2 !== 0) throw new Error(`invalid hex string length: ${hex}`) |
52 |
| - const hexEncoded = hexEncoding.parse(hex) |
53 |
| - const ProgramSha256 = sha256(hexEncoded) |
54 |
| - return ripemd160(ProgramSha256).toString() |
55 |
| -} |
56 |
| - |
57 |
| -/** |
58 |
| - * Encode an address from a string or Buffer. |
59 |
| - * |
60 |
| - * @param {string | Buffer} value The input string or Buffer to be encoded. |
61 |
| - * @param {string} prefix The prefix of the address. (optional) |
62 |
| - * @param {BufferEncoding} type The buffer encoding type. It is used when a string is provided. (optional) |
63 |
| - * @returns {string} The address generated from the input string or Buffer. |
64 |
| - */ |
65 |
| -export const encodeAddress = (value: string | Buffer, prefix = 'thor', type: BufferEncoding = 'hex'): string => { |
66 |
| - const data = Buffer.isBuffer(value) ? value : Buffer.from(value, type) |
67 |
| - const words = bech32.toWords(Uint8Array.from(data)) |
68 |
| - return bech32.encode(prefix, words) |
69 |
| -} |
70 |
| - |
71 |
| -/** |
72 |
| - * Create an address from a public key. |
73 |
| - * |
74 |
| - * @param {Buffer} publicKey The public key in Buffer format. |
75 |
| - * @returns {string} The address generated from the input public key (Buffer format). |
76 |
| - */ |
77 |
| -export const createAddress = (publicKey: Buffer): string => { |
78 |
| - const hexed = ab2hexstring(publicKey) |
79 |
| - const hash = sha256ripemd160(hexed) |
80 |
| - const address = encodeAddress(hash, 'thor') |
81 |
| - return address |
82 |
| -} |
83 | 2 |
|
84 | 3 | /**
|
85 | 4 | * Calculate pbkdf2 (Password-Based Key Derivation Function 2).
|
|
0 commit comments