UNPKG

@tonappchain/adnl

Version:

ADNL TypeScript implementation

42 lines 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ADNLAddress = void 0; const hash_1 = require("./hash"); class ADNLAddress { constructor(publicKey) { const value = ADNLAddress.isBytes(publicKey) ? publicKey : publicKey.trim(); if (ADNLAddress.isBytes(value)) { this._publicKey = value; } else if (ADNLAddress.isHex(value)) { this._publicKey = new Uint8Array(Buffer.from(value, 'hex')); } else if (ADNLAddress.isBase64(value)) { this._publicKey = new Uint8Array(Buffer.from(value, 'base64')); } if (this._publicKey.length !== 32) { throw new Error('ADNLAddress: Bad peer public key. Must contain 32 bytes.'); } } get publicKey() { return new Uint8Array(this._publicKey); } get hash() { const typeEd25519 = new Uint8Array([0xc6, 0xb4, 0x13, 0x48]); const key = new Uint8Array([...typeEd25519, ...this._publicKey]); return (0, hash_1.sha256)(key); } static isHex(data) { const re = /^[a-fA-F0-9]+$/; return typeof data === 'string' && re.test(data); } static isBase64(data) { const re = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/; return typeof data === 'string' && re.test(data); } static isBytes(data) { return ArrayBuffer.isView(data); } } exports.ADNLAddress = ADNLAddress; //# sourceMappingURL=address.js.map