UNPKG

neo-convertor

Version:

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

43 lines (42 loc) 2.14 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 libs_1 = require("../libs"); const uint_1 = require("../uint"); const crypto_1 = require("../crypto"); class Wallet { } Wallet.CoinVersion = 0x17; Wallet.toAddress = (scriptHash) => __awaiter(this, void 0, void 0, function* () { let data = new Uint8Array(25); data[0] = Wallet.CoinVersion; libs_1.copyArray(new Uint8Array(scriptHash.bits.buffer), 0, data, 1, 20); const result = yield crypto_1.Subtle.digest("SHA-256", new Uint8Array(data.buffer, 0, 21)); const finalResult = yield crypto_1.Subtle.digest("SHA-256", result); libs_1.copyArray(new Uint8Array(finalResult), 0, data, 21, 4); return crypto_1.Base58.encode(data); }); Wallet.toScriptHash = (address) => __awaiter(this, void 0, void 0, function* () { let data = crypto_1.Base58.decode(address); if (data.length != 25) throw new RangeError(); if (data[0] != Wallet.CoinVersion) throw new RangeError(); const result = yield crypto_1.Subtle.digest("SHA-256", new Uint8Array(data.buffer, 0, data.length - 4)); const finalResult = yield crypto_1.Subtle.digest("SHA-256", result); let array = new Uint8Array(finalResult); for (var i = 0; i < 4; i++) if (array[i] != data[data.length - 4 + i]) throw new RangeError(); array = new Uint8Array(20); libs_1.copyArray(data, 1, array, 0, 20); return new uint_1.Uint160(array.buffer); }); exports.Wallet = Wallet;