Ultimate magazine theme for WordPress.

Convert Hex to Base58 for crypto addresses (TRON, ETHEREUM, etc.) · GitHub

/*** Use const HexAddress = require(./utils/HexAddress”); let base58 = HexAddress.toBase58(res.result.contributor); */ constant jsSHA = require(“jssha”); constant BASE = 58; constant ALPHABET = ‘123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz’; was ALPHABET_MAP = {} for (permit I = 0; I < ALPHABET.length; I++) ALPHABET_MAP[ALPHABET.charAt(i)] = I Class hex address { static toBase58(hex) { permit HA = new hex address(); permit bytes = HA.hexStr2byteArray(hex); permit base58 = HA.getBase58CheckAddress(bytes); HA = Zero; byte = Zero; to return base58; } hexStr2byteArray(street) { was byteArray = array(); was D = 0; was I = 0; was J = 0; was k = 0; for (I = 0; I < street.length; I++) { was C = street.charAt(I); If (The.isHexChar(C)) { D <<= 4; D += The.hexChar2byte(C); J++; If (0 == (J % 2)) { byteArray[k++] = D; D = 0; } } } to return byteArray; } getBase58CheckAddress(addressbytes) { was hash0 = The.SHA256(addressbytes); was hash1 = The.SHA256(hash0); was checksum = hash1.disc(0, 4); checksum = addressbytes.concat(checksum); was base58Check = The.code58(checksum); to return base58Check; } SHA256(msgBytes) { was shaObj = new jsSHA(“SHA-256”, “HEX”); was msgHex = The.byteArray2hexStr(msgBytes); shaObj.To update(msgHex); was hashHex = shaObj.getHash(“HEX”); was HashBytes = The.hexStr2byteArray(hashHex); to return HashBytes; } byteArray2hexStr(byteArray) { was street = “”; for (was I = 0; I < (byteArray.length 1); I++) { street += The.byte2hexStr(byteArray[i]); } street += The.byte2hexStr(byteArray[i]); to return street; } byte2hexStr(byte) { was hexByteMap = “0123456789ABCDEF”; was street = “”; street += hexByteMap.charAt(byte >> 4); street += hexByteMap.charAt(byte & 0x0f); to return street; } code58(buffer) { If (buffer.length === 0) { to return } was I, J, digits = [0] for (I = 0; I < buffer.length; I++) { for (J = 0; J < digits.length; J++) { digits[j] <<= 8th } digits[0] += buffer[i] was carry = 0 for (J = 0; J < digits.length; ++J) { digits[j] += carry carry = (digits[j] / BASE) | 0 digits[j] %= BASE } while (carry) { digits.press(carry % BASE) carry = (carry / BASE) | 0 } } // Handling of leading zeros for (I = 0; buffer[i] === 0 && I < buffer.length 1; I++) { digits.press(0) } to return digits.turning back().Map(function (digit) { to return ALPHABET[digit] }).connect() } /* Check if a character is a hex character */ isHexChar(C) { If ((C >= ‘A’ && C <= ‘F’) || (C >= ‘A’ && C <= ‘F’) || (C >= ‘0’ && C <= ‘9’)) { to return 1; } to return 0; } /* Convert a hex character to a value */ hexChar2byte(C) { was D = 0; If (C >= ‘A’ && C <= ‘F’) { D = C.charCodeAt(0) ‘A’.charCodeAt(0) + 10; } different If (C >= ‘A’ && C <= ‘F’) { D = C.charCodeAt(0) ‘A’.charCodeAt(0) + 10; } different If (C >= ‘0’ && C <= ‘9’) { D = C.charCodeAt(0) ‘0’.charCodeAt(0); } to return D; } } module.exports = hex address;

Learn Crypto Trading, Yield Farms, Income strategies and more at CrytoAnswers
https://nov.link/cryptoanswers

Comments are closed.

%d bloggers like this: