@pact-toolbox/signers
Version:
174 lines (167 loc) • 7.49 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __pact_toolbox_crypto = __toESM(require("@pact-toolbox/crypto"));
//#region src/command-signer.ts
/** Checks whether the provided value implements the {@link PactCommandSigner} interface. */
function isPactCommandSigner(value) {
return "signPactCommands" in value && typeof value.signPactCommands === "function";
}
/** Asserts that the provided value implements the {@link PactCommandSigner} interface. */
function assertIsPactCommandSigner(value) {
if (!isPactCommandSigner(value)) throw new Error("Value is not a PactCommandSigner");
}
async function partiallySignPactCommand(keyPairs, command) {
const cmd = JSON.stringify(command);
const cmdBytes = __pact_toolbox_crypto.utf8.encode(cmd);
const hash = (0, __pact_toolbox_crypto.blake2b)(cmdBytes, void 0, 32);
const keyPairMap = /* @__PURE__ */ new Map();
for (const keyPair of keyPairs) {
const pubKeyHex = await (0, __pact_toolbox_crypto.exportBase16Key)(keyPair.publicKey);
keyPairMap.set(pubKeyHex, keyPair);
}
const sigs = [];
for (const signer of command.signers) {
const pubKey = signer.pubKey;
const keyPair = keyPairMap.get(pubKey);
if (!keyPair) {
sigs.push({ pubKey });
continue;
}
const signatureBytes = await (0, __pact_toolbox_crypto.signBytes)(keyPair.privateKey, hash);
const signatureHex = __pact_toolbox_crypto.base16.decode(signatureBytes);
sigs.push({
pubKey,
sig: signatureHex
});
}
return {
cmd,
hash,
sigs
};
}
function isTransactionFullSig(sig) {
return "sig" in sig && sig.sig !== void 0;
}
function isFullySignedTransaction(transaction) {
return typeof transaction === "object" && transaction !== null && "cmd" in transaction && "hash" in transaction && "sigs" in transaction && Array.isArray(transaction.sigs) && transaction.sigs.every(isTransactionFullSig);
}
function finalizeTransaction(transaction) {
return Object.freeze({
cmd: transaction.cmd,
hash: typeof transaction.hash !== "string" && transaction.hash instanceof Uint8Array ? __pact_toolbox_crypto.base64Url.decode(transaction.hash) : transaction.hash,
sigs: transaction.sigs.filter((sig) => "sig" in sig)
});
}
//#endregion
//#region src/message-signer.ts
/** Checks whether the provided value implements the {@link MessageSigner} interface. */
function isMessageSigner(value) {
return "signMessages" in value && typeof value.signMessages === "function";
}
/** Asserts that the provided value implements the {@link MessageSigner} interface. */
function assertIsMessageSigner(value) {
if (!isMessageSigner(value)) throw new Error("Value is not a MessageSigner");
}
//#endregion
//#region src/keypair-signer.ts
/** Checks whether the provided value implements the {@link KeyPairSigner} interface. */
function isKeyPairSigner(value) {
return "keyPair" in value && typeof value.keyPair === "object" && isMessageSigner(value) && isPactCommandSigner(value);
}
/** Asserts that the provided value implements the {@link KeyPairSigner} interface. */
function assertIsKeyPairSigner(value) {
if (!isKeyPairSigner(value)) throw new Error("Value is not a KeyPairSigner");
}
/** Creates a KeyPairSigner from the provided Crypto KeyPair. */
async function createSignerFromKeyPair(keyPair) {
const address = await (0, __pact_toolbox_crypto.exportBase16Key)(keyPair.publicKey);
const out = {
address,
keyPair,
signMessages: (messages) => {
return Promise.all(messages.map(async (message) => Object.freeze({ [address]: await (0, __pact_toolbox_crypto.signBytes)(keyPair.privateKey, message.content) })));
},
signPactCommands: (commands) => {
return Promise.all(commands.map((cmd) => partiallySignPactCommand([keyPair], cmd)));
}
};
return Object.freeze(out);
}
/** Securely generates a signer capable of signing messages and transactions using a Crypto KeyPair. */
async function generateKeyPairSigner() {
return createSignerFromKeyPair(await (0, __pact_toolbox_crypto.generateKeyPair)());
}
/** Creates a signer capable of signing messages and transactions using the 64 bytes of a KeyPair. */
async function createKeyPairSignerFromBytes(bytes, extractable) {
return createSignerFromKeyPair(await (0, __pact_toolbox_crypto.createKeyPairFromBytes)(bytes, extractable));
}
/** Creates a signer capable of signing messages and transactions using the 32 bytes of a private key. */
async function createKeyPairSignerFromPrivateKeyBytes(bytes, extractable) {
return createSignerFromKeyPair(await (0, __pact_toolbox_crypto.createKeyPairFromPrivateKeyBytes)(bytes, extractable));
}
async function createKeyPairSignerFromBase16PrivateKey(privateKey, extractable) {
return createKeyPairSignerFromPrivateKeyBytes(__pact_toolbox_crypto.base16.encode(privateKey), extractable);
}
//#endregion
//#region src/noop.ts
/** Creates a NoopSigner from the provided Address. */
function createNoopSigner(address) {
const out = {
address,
signMessages: (messages) => Promise.resolve(messages.map(() => Object.freeze({}))),
signPactCommands: (commands) => Promise.resolve(commands.map(() => Object.freeze({})))
};
return Object.freeze(out);
}
//#endregion
//#region src/signable-message.ts
/**
* Creates a signable message from a provided content.
* If a string is provided, it will be UTF-8 encoded.
*/
function createSignableMessage(content, signatures = {}) {
return Object.freeze({
content: typeof content === "string" ? new __pact_toolbox_crypto.TextEncoder().encode(content) : content,
signatures: Object.freeze({ ...signatures })
});
}
//#endregion
exports.assertIsKeyPairSigner = assertIsKeyPairSigner;
exports.assertIsMessageSigner = assertIsMessageSigner;
exports.assertIsPactCommandSigner = assertIsPactCommandSigner;
exports.createKeyPairSignerFromBase16PrivateKey = createKeyPairSignerFromBase16PrivateKey;
exports.createKeyPairSignerFromBytes = createKeyPairSignerFromBytes;
exports.createKeyPairSignerFromPrivateKeyBytes = createKeyPairSignerFromPrivateKeyBytes;
exports.createNoopSigner = createNoopSigner;
exports.createSignableMessage = createSignableMessage;
exports.createSignerFromKeyPair = createSignerFromKeyPair;
exports.finalizeTransaction = finalizeTransaction;
exports.generateKeyPairSigner = generateKeyPairSigner;
exports.isFullySignedTransaction = isFullySignedTransaction;
exports.isKeyPairSigner = isKeyPairSigner;
exports.isMessageSigner = isMessageSigner;
exports.isPactCommandSigner = isPactCommandSigner;
exports.isTransactionFullSig = isTransactionFullSig;
exports.partiallySignPactCommand = partiallySignPactCommand;
//# sourceMappingURL=index.node.cjs.map