UNPKG

@pod-protocol/sdk

Version:

TypeScript SDK for PoD Protocol - AI agent communication on Solana

120 lines (111 loc) 4.11 kB
'use strict'; var types = require('./types-OQd1rGtn.js'); // src/key-pair.ts // src/algorithm.ts var ED25519_ALGORITHM_IDENTIFIER = ( // Resist the temptation to convert this to a simple string; As of version 133.0.3, Firefox // requires the object form of `AlgorithmIdentifier` and will throw a `DOMException` otherwise. Object.freeze({ name: "Ed25519" }) ); async function signBytes(key, data) { types.assertSigningCapabilityIsAvailable(); const signedData = await crypto.subtle.sign(ED25519_ALGORITHM_IDENTIFIER, key, data); return new Uint8Array(signedData); } // src/key-pair.ts async function generateKeyPair() { await types.assertKeyGenerationIsAvailable(); const keyPair = await crypto.subtle.generateKey( /* algorithm */ ED25519_ALGORITHM_IDENTIFIER, // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20 /* extractable */ false, // Prevents the bytes of the private key from being visible to JS. /* allowed uses */ ["sign", "verify"] ); return keyPair; } function uint8ArraysEqual(arr1, arr2) { return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]); } async function partiallySignTransaction(keyPairs, transaction) { let newSignatures; let unexpectedSigners; await Promise.all( keyPairs.map(async (keyPair) => { const address = await types.getAddressFromPublicKey(keyPair.publicKey); const existingSignature = transaction.signatures[address]; if (existingSignature === void 0) { unexpectedSigners ||= /* @__PURE__ */ new Set(); unexpectedSigners.add(address); return; } if (unexpectedSigners) { return; } const newSignature = await signBytes(keyPair.privateKey, transaction.messageBytes); if (existingSignature !== null && uint8ArraysEqual(newSignature, existingSignature)) { return; } newSignatures ||= {}; newSignatures[address] = newSignature; }) ); if (unexpectedSigners && unexpectedSigners.size > 0) { const expectedSigners = Object.keys(transaction.signatures); throw new types.SolanaError(types.SOLANA_ERROR__TRANSACTION__ADDRESSES_CANNOT_SIGN_TRANSACTION, { expectedAddresses: expectedSigners, unexpectedAddresses: [...unexpectedSigners] }); } if (!newSignatures) { return transaction; } return Object.freeze({ ...transaction, signatures: Object.freeze({ ...transaction.signatures, ...newSignatures }) }); } function isTransactionModifyingSigner(value) { return "modifyAndSignTransactions" in value && typeof value.modifyAndSignTransactions === "function"; } function isTransactionPartialSigner(value) { return "signTransactions" in value && typeof value.signTransactions === "function"; } function isTransactionSendingSigner(value) { return "signAndSendTransactions" in value && typeof value.signAndSendTransactions === "function"; } // src/transaction-signer.ts function isTransactionSigner(value) { return isTransactionPartialSigner(value) || isTransactionModifyingSigner(value) || isTransactionSendingSigner(value); } async function createSignerFromKeyPair(keyPair) { const address = await types.getAddressFromPublicKey(keyPair.publicKey); const out = { address, keyPair, signMessages: (messages) => Promise.all( messages.map( async (message) => Object.freeze({ [address]: await signBytes(keyPair.privateKey, message.content) }) ) ), signTransactions: (transactions) => Promise.all( transactions.map(async (transaction) => { const signedTransaction = await partiallySignTransaction([keyPair], transaction); return Object.freeze({ [address]: signedTransaction.signatures[address] }); }) ) }; return Object.freeze(out); } async function generateKeyPairSigner() { return await createSignerFromKeyPair(await generateKeyPair()); } exports.generateKeyPairSigner = generateKeyPairSigner; exports.isTransactionSigner = isTransactionSigner; //# sourceMappingURL=index.node-aPHhG-NK.js.map