js-databox
Version:
databox & metabox
66 lines (65 loc) • 2.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getToAccountIdentifier = exports.getUint8ArrayFromHex = exports.ArrayToHexString = void 0;
const buffer_1 = require("buffer");
const candid_1 = require("@dfinity/candid");
const getCrc_1 = require("@dfinity/principal/lib/cjs/utils/getCrc");
const SHA1 = __importStar(require("@dfinity/principal/lib/cjs/utils/sha224"));
const ArrayToHexString = (byteArray) => {
return Array.from(byteArray, function (byte) {
return ("0" + (byte & 0xff).toString(16)).slice(-2);
}).join("");
};
exports.ArrayToHexString = ArrayToHexString;
const getUint8ArrayFromHex = (str) => {
return Uint8Array.from(buffer_1.Buffer.from(str, "hex"));
};
exports.getUint8ArrayFromHex = getUint8ArrayFromHex;
const getToAccountIdentifier = (principal, s) => {
const padding = new buffer_1.Buffer("\x0Aaccount-id");
const array = new Uint8Array([
...padding,
...principal.toUint8Array(),
...getPrincipalSubAccountArray(s),
]);
const hash = SHA1.sha224(array);
const checksum = to32bits((0, getCrc_1.getCrc32)(hash));
const array2 = new Uint8Array([...checksum, ...hash]);
return (0, candid_1.toHexString)(array2);
};
exports.getToAccountIdentifier = getToAccountIdentifier;
const getPrincipalSubAccountArray = (principal) => {
const p = Array.from(principal.toUint8Array());
let tmp = Array(1).fill(p.length).concat(p);
while (tmp.length < 32)
tmp.push(0);
return tmp;
};
const to32bits = (num) => {
let b = new ArrayBuffer(4);
new DataView(b).setUint32(0, num);
return Array.from(new Uint8Array(b));
};