|
| 1 | +import { LitNodeClient } from "@lit-protocol/lit-node-client"; |
| 2 | +import { LitNetwork, LIT_RPC, } from "@lit-protocol/constants"; |
| 3 | +import { |
| 4 | + createSiweMessageWithRecaps, |
| 5 | + generateAuthSig, |
| 6 | + LitAbility, |
| 7 | + LitActionResource, |
| 8 | + LitPKPResource, |
| 9 | +} from "@lit-protocol/auth-helpers"; |
| 10 | +import { LitContracts } from "@lit-protocol/contracts-sdk"; |
| 11 | +import { LIT_NETWORKS_KEYS } from "@lit-protocol/types"; |
| 12 | +import * as ethers from "ethers"; |
| 13 | + |
| 14 | +import { getEnv } from "./utils/utils"; |
| 15 | +import { singleSig } from "./p2sh/single-sig"; |
| 16 | +import { multiSig } from "./p2sh/multi-sig"; |
| 17 | +import { oneOfOneMultiSig } from "./p2sh/1of1-multi-sig"; |
| 18 | +import { collaborativeMultiSig } from "./p2sh/collaborative"; |
| 19 | + |
| 20 | +const PKP_PUBLIC_KEY_1 = getEnv("PKP_PUBLIC_KEY_1"); |
| 21 | +const PKP_PUBLIC_KEY_2 = getEnv("PKP_PUBLIC_KEY_2"); |
| 22 | +const ETHEREUM_PRIVATE_KEY = getEnv("ETHEREUM_PRIVATE_KEY"); |
| 23 | +const BTC_DESTINATION_ADDRESS = getEnv("BTC_DESTINATION_ADDRESS"); |
| 24 | +const LIT_NETWORK = process.env["LIT_NETWORK"] as LIT_NETWORKS_KEYS || LitNetwork.Datil; |
| 25 | +const LIT_CAPACITY_CREDIT_TOKEN_ID = process.env["LIT_CAPACITY_CREDIT_TOKEN_ID"]; |
| 26 | + |
| 27 | +const ethersWallet = new ethers.Wallet( |
| 28 | + ETHEREUM_PRIVATE_KEY, |
| 29 | + new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE) |
| 30 | +); |
| 31 | + |
| 32 | +const address = ethersWallet.address; |
| 33 | + |
| 34 | +export const executeBtcSigning = async () => { |
| 35 | + let litNodeClient: LitNodeClient; |
| 36 | + |
| 37 | + try { |
| 38 | + litNodeClient = new LitNodeClient({ |
| 39 | + litNetwork: LIT_NETWORK, |
| 40 | + debug: false, |
| 41 | + }); |
| 42 | + await litNodeClient.connect(); |
| 43 | + |
| 44 | + const litContracts = new LitContracts({ |
| 45 | + signer: ethersWallet, |
| 46 | + network: LIT_NETWORK, |
| 47 | + debug: false, |
| 48 | + }); |
| 49 | + await litContracts.connect(); |
| 50 | + |
| 51 | + let capacityTokenId = LIT_CAPACITY_CREDIT_TOKEN_ID; |
| 52 | + if (!capacityTokenId) { |
| 53 | + console.log("🔄 No Capacity Credit provided, minting a new one..."); |
| 54 | + const mintResult = await litContracts.mintCapacityCreditsNFT({ |
| 55 | + requestsPerKilosecond: 10, |
| 56 | + daysUntilUTCMidnightExpiration: 1, |
| 57 | + }); |
| 58 | + capacityTokenId = mintResult.capacityTokenIdStr; |
| 59 | + console.log(`✅ Minted new Capacity Credit with ID: ${capacityTokenId}`); |
| 60 | + } else { |
| 61 | + console.log(`ℹ️ Using provided Capacity Credit with ID: ${LIT_CAPACITY_CREDIT_TOKEN_ID}`); |
| 62 | + } |
| 63 | + |
| 64 | + console.log("🔄 Creating capacityDelegationAuthSig..."); |
| 65 | + const { capacityDelegationAuthSig } = |
| 66 | + await litNodeClient.createCapacityDelegationAuthSig({ |
| 67 | + dAppOwnerWallet: ethersWallet, |
| 68 | + capacityTokenId, |
| 69 | + delegateeAddresses: [address], |
| 70 | + uses: "1", |
| 71 | + }); |
| 72 | + console.log("✅ Capacity Delegation Auth Sig created"); |
| 73 | + |
| 74 | + console.log("🔄 Getting Session Signatures..."); |
| 75 | + const sessionSigs = await litNodeClient.getSessionSigs({ |
| 76 | + chain: "ethereum", |
| 77 | + expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours |
| 78 | + capabilityAuthSigs: [capacityDelegationAuthSig], |
| 79 | + resourceAbilityRequests: [ |
| 80 | + { |
| 81 | + resource: new LitPKPResource("*"), |
| 82 | + ability: LitAbility.PKPSigning, |
| 83 | + }, |
| 84 | + { |
| 85 | + resource: new LitActionResource("*"), |
| 86 | + ability: LitAbility.LitActionExecution, |
| 87 | + }, |
| 88 | + ], |
| 89 | + authNeededCallback: async ({ |
| 90 | + resourceAbilityRequests, |
| 91 | + expiration, |
| 92 | + uri, |
| 93 | + }) => { |
| 94 | + const toSign = await createSiweMessageWithRecaps({ |
| 95 | + uri: uri!, |
| 96 | + expiration: expiration!, |
| 97 | + resources: resourceAbilityRequests!, |
| 98 | + walletAddress: address, |
| 99 | + nonce: await litNodeClient!.getLatestBlockhash(), |
| 100 | + litNodeClient, |
| 101 | + }); |
| 102 | + |
| 103 | + return await generateAuthSig({ |
| 104 | + signer: ethersWallet, |
| 105 | + toSign, |
| 106 | + }); |
| 107 | + }, |
| 108 | + }); |
| 109 | + console.log("✅ Got Session Signatures"); |
| 110 | + |
| 111 | + console.log("🔄 Executing Bitcoin Transaction..."); |
| 112 | + return await singleSig(litNodeClient, sessionSigs, PKP_PUBLIC_KEY_1, BTC_DESTINATION_ADDRESS); |
| 113 | + |
| 114 | + /** |
| 115 | + * Single Sig P2SH: |
| 116 | + return await singleSig(litNodeClient, sessionSigs, PKP_PUBLIC_KEY_1, BTC_DESTINATION_ADDRESS); |
| 117 | +
|
| 118 | + * Multi Sig P2SH: |
| 119 | + return await multiSig(litNodeClient, sessionSigs, PKP_PUBLIC_KEY_1, PKP_PUBLIC_KEY_2, BTC_DESTINATION_ADDRESS); |
| 120 | +
|
| 121 | + * 1of1 Multi Sig P2SH: |
| 122 | + return await oneOfOneMultiSig(litNodeClient, sessionSigs, PKP_PUBLIC_KEY_1, PKP_PUBLIC_KEY_2, BTC_DESTINATION_ADDRESS); |
| 123 | +
|
| 124 | + * Collaborative Multi Sig P2SH: |
| 125 | + return await collaborativeMultiSig(litNodeClient, sessionSigs, PKP_PUBLIC_KEY_1, PKP_PUBLIC_KEY_2, BTC_DESTINATION_ADDRESS); |
| 126 | + */ |
| 127 | + |
| 128 | + } catch (error) { |
| 129 | + console.error(error); |
| 130 | + } finally { |
| 131 | + litNodeClient!.disconnect(); |
| 132 | + } |
| 133 | +}; |
0 commit comments