PHP WebShell

Текущая директория: /opt/BitGoJS/node_modules/@stacks/transactions/dist/esm

Просмотр файла: utils.js

import { Buffer } from '@stacks/common';
import { sha256, sha512 } from 'sha.js';
import { serializeCV } from './clarity';
import RIPEMD160 from 'ripemd160-min';
import randombytes from 'randombytes';
import { deserializeCV } from './clarity';
import fetch from 'cross-fetch';
import { c32addressDecode } from 'c32check';
import lodashCloneDeep from 'lodash/cloneDeep';
import { with0x } from '@stacks/common';
export { randombytes as randomBytes };
export class BufferArray {
    constructor() {
        this._value = [];
    }
    get value() {
        return this._value;
    }
    appendHexString(hexString) {
        this.value.push(Buffer.from(hexString, 'hex'));
    }
    push(buffer) {
        return this._value.push(buffer);
    }
    appendByte(octet) {
        if (!Number.isInteger(octet) || octet < 0 || octet > 255) {
            throw new Error(`Value ${octet} is not a valid byte`);
        }
        this.value.push(Buffer.from([octet]));
    }
    concatBuffer() {
        return Buffer.concat(this.value);
    }
}
export const leftPadHex = (hexString) => hexString.length % 2 == 0 ? hexString : `0${hexString}`;
export const leftPadHexToLength = (hexString, length) => hexString.padStart(length, '0');
export const rightPadHexToLength = (hexString, length) => hexString.padEnd(length, '0');
export const intToHexString = (integer, lengthBytes = 8) => integer.toString(16).padStart(lengthBytes * 2, '0');
export const hexStringToInt = (hexString) => parseInt(hexString, 16);
export const exceedsMaxLengthBytes = (string, maxLengthBytes) => string ? Buffer.from(string).length > maxLengthBytes : false;
export function cloneDeep(obj) {
    return lodashCloneDeep(obj);
}
export function omit(obj, prop) {
    const clone = cloneDeep(obj);
    delete clone[prop];
    return clone;
}
export class sha512_256 extends sha512 {
    constructor() {
        super();
        Object.assign(this, {
            _ah: 0x22312194,
            _al: 0xfc2bf72c,
            _bh: 0x9f555fa3,
            _bl: 0xc84c64c2,
            _ch: 0x2393b86b,
            _cl: 0x6f53b151,
            _dh: 0x96387719,
            _dl: 0x5940eabd,
            _eh: 0x96283ee2,
            _el: 0xa88effe3,
            _fh: 0xbe5e1e25,
            _fl: 0x53863992,
            _gh: 0x2b0199fc,
            _gl: 0x2c85b8aa,
            _hh: 0x0eb72ddc,
            _hl: 0x81c52ca2,
        });
    }
    digest(encoding) {
        const buff = super.digest().slice(0, 32);
        return encoding ? buff.toString(encoding) : buff;
    }
}
export const txidFromData = (data) => new sha512_256().update(data).digest('hex');
export const hash160 = (input) => {
    const sha256Result = new sha256().update(input).digest();
    return Buffer.from(new RIPEMD160().update(sha256Result).digest());
};
export const hashP2PKH = (input) => {
    return hash160(input).toString('hex');
};
export const hashP2SH = (numSigs, pubKeys) => {
    if (numSigs > 15 || pubKeys.length > 15) {
        throw Error('P2SH multisig address can only contain up to 15 public keys');
    }
    const bufferArray = new BufferArray();
    bufferArray.appendByte(80 + numSigs);
    pubKeys.forEach(pubKey => {
        bufferArray.appendByte(pubKey.length);
        bufferArray.push(pubKey);
    });
    bufferArray.appendByte(80 + pubKeys.length);
    bufferArray.appendByte(174);
    const redeemScript = bufferArray.concatBuffer();
    const redeemScriptHash = hash160(redeemScript);
    return redeemScriptHash.toString('hex');
};
export function isClarityName(name) {
    const regex = /^[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*$|^[-+=/*]$|^[<>]=?$/;
    return regex.test(name) && name.length < 128;
}
export async function fetchPrivate(input, init) {
    const defaultFetchOpts = {
        referrer: 'no-referrer',
        referrerPolicy: 'no-referrer',
    };
    const fetchOpts = Object.assign(defaultFetchOpts, init);
    const fetchResult = await fetch(input, fetchOpts);
    return fetchResult;
}
export function cvToHex(cv) {
    const serialized = serializeCV(cv);
    return `0x${serialized.toString('hex')}`;
}
export function hexToCV(hex) {
    return deserializeCV(hex);
}
export const parseReadOnlyResponse = (response) => {
    if (response.okay) {
        return hexToCV(response.result);
    }
    else {
        throw new Error(response.cause);
    }
};
export const validateStacksAddress = (stacksAddress) => {
    try {
        c32addressDecode(stacksAddress);
        return true;
    }
    catch (e) {
        return false;
    }
};
export const validateTxId = (txid) => {
    if (txid === 'success')
        return true;
    const value = with0x(txid).toLowerCase();
    if (value.length !== 66)
        return false;
    return with0x(BigInt(value).toString(16).padStart(64, '0')) === value;
};
//# sourceMappingURL=utils.js.map

Выполнить команду


Для локальной разработки. Не используйте в интернете!