UNPKG

@arcblock/did

Version:
46 lines (45 loc) 1.35 kB
/* eslint-disable @typescript-eslint/ban-ts-comment */ import padStart from 'lodash/padStart'; import { toBN, fromBase58, isHexStrict, stripHexPrefix } from '@ocap/util'; const DID_PREFIX = 'did:abt:'; /** * Convert did to bytes with length of 26 * * @param {string} did * @returns {Buffer} */ const toBytes = (did) => { try { if (isHexStrict(did)) { return Buffer.from(stripHexPrefix(did), 'hex'); } let bytes = fromBase58(did.replace(DID_PREFIX, '')); while (bytes.length < 26) { bytes = Buffer.concat([Buffer.from([0]), bytes]); } return bytes; } catch (err) { // @ts-ignore throw new Error(`Cannot convert DID string to byte array: ${err.message}`); } }; /** * Convert number to bit string with predefined length */ const toBits = (number, length) => padStart(toBN(number).toString(2), length, '0'); /** * Ensure the hex length is even number, 2, 4, 6, 8 * * @param {string} hex * @param {number} length * @returns {string} hex */ const toStrictHex = (hex, length) => { const str = hex.replace(/^0x/i, ''); if (typeof length === 'number' && length % 2 === 0) { return padStart(hex, length, '0'); } return str.length % 2 === 0 ? str : `0${str}`; }; export { DID_PREFIX, toBits, toBytes, toStrictHex };