UNPKG

@yubing744/rooch-sdk

Version:
57 lines (56 loc) 1.8 kB
import { fromB64 } from "../b64"; import { SIGNATURE_FLAG_TO_SCHEME } from "./signature"; import { Ed25519PublicKey, Ed25519Keypair } from "../keypairs"; import { LEGACY_PRIVATE_KEY_SIZE, PRIVATE_KEY_SIZE } from "./keypair"; function toParsedSignaturePubkeyPair(serializedSignature) { const bytes = serializedSignature; const signatureScheme = SIGNATURE_FLAG_TO_SCHEME[bytes[0]]; const SIGNATURE_SCHEME_TO_PUBLIC_KEY = { ED25519: 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 Ed25519PublicKey(pubKey); } throw new Error("Unknown public key schema"); } function fromExportedKeypair(keypair) { const secretKey = fromB64(keypair.privateKey); let pureSecretKey; switch (keypair.schema) { case "ED25519": pureSecretKey = secretKey; if (secretKey.length === LEGACY_PRIVATE_KEY_SIZE) { pureSecretKey = secretKey.slice(0, PRIVATE_KEY_SIZE); } return Ed25519Keypair.fromSecretKey(pureSecretKey); default: throw new Error(`Invalid keypair schema ${keypair.schema}`); } } export { fromExportedKeypair, publicKeyFromSerialized, toParsedSignaturePubkeyPair, toSingleSignaturePubkeyPair }; //# sourceMappingURL=utils.js.map