@gleif-it/did-webs-ts
Version:
did-webs typescript library
31 lines (30 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeKey = void 0;
const R = require("remeda");
const isValidEd25519Cesr_js_1 = require("../validators/isValidEd25519Cesr.js");
const isValidSecp256k1Cesr_js_1 = require("../validators/isValidSecp256k1Cesr.js");
const base64UrlToBytes_js_1 = require("../conversions/base64UrlToBytes.js");
const bytesToBase64Url_js_1 = require("../conversions/bytesToBase64Url.js");
const ps = (N) => (3 - (N % 3)) % 3;
const padBytes = (bytes) => {
const padding = ps(bytes.length);
const paddedBytes = new Uint8Array(bytes.length + padding);
paddedBytes.set(bytes, padding); // padding is the offset, this will leave the initialized zeros at the beginning
return paddedBytes;
};
const encodeWith = (code) => (rawB64) => {
if (code[0] === '1') {
return `${code}${rawB64}`;
}
else {
return `${code}${rawB64.slice(code.length)}`;
}
};
const encodeKey = (code, decodedKey) => {
const key = R.pipe(decodedKey, base64UrlToBytes_js_1.base64UrlToBytes, padBytes, bytesToBase64Url_js_1.bytesToBase64Url, encodeWith(code));
if ((0, isValidEd25519Cesr_js_1.isValidEd25519Cesr)(key) || (0, isValidSecp256k1Cesr_js_1.isValidSecp256k1Cesr)(key))
return key;
throw new Error("Can't encode key");
};
exports.encodeKey = encodeKey;