@yubing744/rooch-sdk
Version:
77 lines (76 loc) • 2.87 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_exports = {};
__export(utils_exports, {
fromExportedKeypair: () => fromExportedKeypair,
publicKeyFromSerialized: () => publicKeyFromSerialized,
toParsedSignaturePubkeyPair: () => toParsedSignaturePubkeyPair,
toSingleSignaturePubkeyPair: () => toSingleSignaturePubkeyPair
});
module.exports = __toCommonJS(utils_exports);
var import_b64 = require("../b64");
var import_signature = require("./signature");
var import_keypairs = require("../keypairs");
var import_keypair = require("./keypair");
function toParsedSignaturePubkeyPair(serializedSignature) {
const bytes = serializedSignature;
const signatureScheme = import_signature.SIGNATURE_FLAG_TO_SCHEME[bytes[0]];
const SIGNATURE_SCHEME_TO_PUBLIC_KEY = {
ED25519: import_keypairs.Ed25519PublicKey
};
const PublicKey = SIGNATURE_SCHEME_TO_PUBLIC_KEY[signatureScheme];
const signature = bytes.slice(1, bytes.length - PublicKey.SIZE);
const pubkeyBytes = bytes.slice(1 + signature.length);
const pubKey = new PublicKey(pubkeyBytes);
return [
{
signatureScheme,
signature,
pubKey
}
];
}
function toSingleSignaturePubkeyPair(serializedSignature) {
const res = toParsedSignaturePubkeyPair(serializedSignature);
if (res.length !== 1) {
throw Error("Expected a single signature");
}
return res[0];
}
function publicKeyFromSerialized(schema, pubKey) {
if (schema === "ED25519") {
return new import_keypairs.Ed25519PublicKey(pubKey);
}
throw new Error("Unknown public key schema");
}
function fromExportedKeypair(keypair) {
const secretKey = (0, import_b64.fromB64)(keypair.privateKey);
let pureSecretKey;
switch (keypair.schema) {
case "ED25519":
pureSecretKey = secretKey;
if (secretKey.length === import_keypair.LEGACY_PRIVATE_KEY_SIZE) {
pureSecretKey = secretKey.slice(0, import_keypair.PRIVATE_KEY_SIZE);
}
return import_keypairs.Ed25519Keypair.fromSecretKey(pureSecretKey);
default:
throw new Error(`Invalid keypair schema ${keypair.schema}`);
}
}
//# sourceMappingURL=utils.js.map