UNPKG

neo-convertor

Version:

A tool to convert neo smart contract data to human-readable one

90 lines (89 loc) 3.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const wallets_1 = require("../wallets"); const uint_1 = require("../uint"); const libs_1 = require("../libs"); const endianChange = (inSmall) => { let result = [], num; if (inSmall.indexOf("0x") === 0) { inSmall = inSmall.slice(2); } else if (inSmall) { result = ['0x']; } let smaArray = libs_1.hexToBytes(inSmall).reverse(); for (let i = 0; i < smaArray.length; i++) { num = smaArray[i]; if (num < 16) { num = smaArray[i].toString(16); num = "0" + num; } else { num = smaArray[i].toString(16); } result.push(num); } return result.join(""); }; class Address { } Address.changeEndian = (scriptHash) => { if (scriptHash.substr(0, 2) === '0x' && scriptHash.length !== 42) { throw new Error(`${scriptHash} is not in legal format.`); } if (scriptHash.substr(0, 2) !== '0x' && scriptHash.length !== 40) { throw new Error(`${scriptHash} is not in legal format.`); } let result = endianChange(scriptHash); if (result.substr(0, 2) === '0x') { result = result.slice(2); } return result; }; Address._addressToScriptHash = (address) => __awaiter(this, void 0, void 0, function* () { if (address.length !== 34) { return Promise.reject(`Illegal Format Address! ${address} length is no 34.`); } return yield wallets_1.Wallet.toScriptHash(address); }); Address.addressToScriptHash = (address, littleEndian = false) => __awaiter(this, void 0, void 0, function* () { const resultInUint160 = yield Address._addressToScriptHash(address); let scriptHash = resultInUint160.toString(); if (littleEndian) { scriptHash = "0x" + scriptHash; scriptHash = endianChange(scriptHash); } return scriptHash; }); Address._scriptHashToAddress = (scriptHash) => __awaiter(this, void 0, void 0, function* () { let hash160 = uint_1.Uint160.parse(scriptHash); const result = yield wallets_1.Wallet.toAddress(hash160); return result; }); Address.scriptHashToAddress = (scriptHash, littleEndian = false) => __awaiter(this, void 0, void 0, function* () { if (littleEndian) { if (scriptHash.substr(0, 2) === "0x") { return Promise.reject(`${scriptHash} begins with "0x" and is not a little endian hash.`); } if (scriptHash.length !== 40) { return Promise.reject(`Illegal Format Script Hash! ${scriptHash} length is not 40.`); } } let xScriptHash = littleEndian ? endianChange(scriptHash) : scriptHash; if (xScriptHash.substr(0, 2) === "0x") { xScriptHash = xScriptHash.slice(2); } if (scriptHash.length !== 40) { return Promise.reject(`Illegal Format Script Hash! ${scriptHash} length is not 40.`); } return yield Address._scriptHashToAddress(xScriptHash); }); exports.Address = Address;