-
Notifications
You must be signed in to change notification settings - Fork 0
/
taproot-action.ts
197 lines (177 loc) · 6.6 KB
/
taproot-action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
bitcoin.initEccLib(ecc);
const signTaprootTransaction = async (
PRIVATE_KEY: string,
TRANSACTION_HEX: string,
SIGHASH: string,
BROADCAST: boolean
) => {
if (PRIVATE_KEY.startsWith("0x") || PRIVATE_KEY.startsWith("0X")) {
PRIVATE_KEY = PRIVATE_KEY.slice(2);
}
console.log("🔄 Signing the transaction");
const TRANSACTION = bitcoin.Transaction.fromHex(TRANSACTION_HEX);
const hashBuffer = Buffer.from(SIGHASH, "hex");
const signature = Buffer.from(
signSchnorr(hashBuffer, Buffer.from(PRIVATE_KEY, "hex"))
);
TRANSACTION.setWitness(0, [signature]);
console.log("✅ Taproot transaction signed");
console.log("🔄 Broadcasting transaction...");
const signedTx = TRANSACTION.toHex();
console.log("signedTx: ", signedTx);
let response = `signedTx: ${signedTx}`;
if (BROADCAST == true) {
const broadcastResponse = await fetch(`${BTC_ENDPOINT}/api/tx`, {
method: "POST",
headers: { "Content-Type": "text/plain" },
body: signedTx,
});
const txid = await broadcastResponse.text();
console.log(
`✅ Transaction broadcast successfully. TXID: ${BTC_ENDPOINT}/tx/${txid}`
);
response = `txid: ${txid}`;
}
return response;
};
function getFirstSessionSig(pkpSessionSigs: any) {
const sessionSigsEntries = Object.entries(pkpSessionSigs);
if (sessionSigsEntries.length === 0) {
throw new Error(
`Invalid pkpSessionSigs, length zero: ${JSON.stringify(
pkpSessionSigs
)}`
);
}
const [[, sessionSig]] = sessionSigsEntries;
return sessionSig;
}
function getPkpAddressFromSessionSig(pkpSessionSig: any) {
const sessionSignedMessage = JSON.parse(pkpSessionSig.signedMessage);
const capabilities = sessionSignedMessage.capabilities;
if (!capabilities || capabilities.length === 0) {
throw new Error(
`Capabilities in the session's signedMessage is empty, but required.`
);
}
const delegationAuthSig = capabilities.find(
(capability: { algo: string }) => capability.algo === "LIT_BLS"
);
if (!delegationAuthSig) {
throw new Error(
"SessionSig is not from a PKP; no LIT_BLS capabilities found"
);
}
const pkpAddress = delegationAuthSig.address;
console.log(`pkpAddress to permit decryption: ${pkpAddress}`);
return pkpAddress;
}
function getPkpAccessControlCondition(pkpAddress: string) {
if (!ethers.utils.isAddress(pkpAddress)) {
throw new Error(
`pkpAddress is not a valid Ethereum Address: ${pkpAddress}`
);
}
return {
contractAddress: "",
standardContractType: "",
chain: "ethereum",
method: "",
parameters: [":userAddress"],
returnValueTest: {
comparator: "=",
value: pkpAddress,
},
};
}
/**
* Main execution function that handles Taproot wallet creation and transaction signing through a PKP
* @async
* @function go
*/
/**
* Creates a new wallet and encrypts it within the action
* @async
* @method createWallet
* @param {string} method - Should be "createWallet"
* @param {Object} pkpSessionSigs - Session signatures for PKP authentication
* @returns {Object} Object containing:
* - publicKey: The wallet's public key
* - ciphertext: The encrypted wallet data
* - dataToEncryptHash: Hash of the encrypted data
*/
/**
* Signs a taproot transaction
* @async
* @method signTaprootTxn
* @param {string} method - Should be "signTaprootTxn"
* @param {Object} pkpSessionSigs - Session signatures for PKP authentication
* @param {string} ciphertext - The encrypted wallet data
* @param {string} dataToEncryptHash - Hash of the encrypted data
* @param {string} transactionHex - Transaction data in hexadecimal format
* @param {string} sigHash - Signature hash type
* @param {boolean} broadcast - Whether to broadcast the transaction
* @returns {Object} Decrypted data response
*/
const go = async () => {
try {
const LIT_PREFIX = "lit_";
if (method === "createWallet") {
const sessionSig = getFirstSessionSig(pkpSessionSigs);
const pkpAddress = getPkpAddressFromSessionSig(sessionSig);
const ACC = getPkpAccessControlCondition(pkpAddress);
const result = await Lit.Actions.runOnce(
{ waitForResponse: true, name: "encryptedPrivateKey" },
async () => {
const wallet = ethers.Wallet.createRandom();
const publicKey = wallet.publicKey;
const privateKey = wallet.privateKey;
const { ciphertext, dataToEncryptHash } =
await Lit.Actions.encrypt({
accessControlConditions: [ACC],
to_encrypt: new TextEncoder().encode(
`${LIT_PREFIX}${privateKey}`
),
});
return JSON.stringify({
ciphertext,
dataToEncryptHash,
publicKey: publicKey.toString(),
});
}
);
Lit.Actions.setResponse({ response: result });
} else if (method === "signTaprootTxn") {
const sessionSig = getFirstSessionSig(pkpSessionSigs);
const pkpAddress = getPkpAddressFromSessionSig(sessionSig);
const ACC = getPkpAccessControlCondition(pkpAddress);
let decryptedPrivateKey;
decryptedPrivateKey = await Lit.Actions.decryptAndCombine({
accessControlConditions: [ACC],
ciphertext: ciphertext,
dataToEncryptHash: dataToEncryptHash,
authSig: null,
chain: "ethereum",
});
if (!decryptedPrivateKey) {
console.log("decryptedPrivateKey is empty");
return; // Exit the nodes which don't have the decryptedData
}
const privateKey = decryptedPrivateKey.startsWith(LIT_PREFIX)
? decryptedPrivateKey.slice(LIT_PREFIX.length)
: decryptedPrivateKey;
const response = await signTaprootTransaction(
privateKey,
transactionHex,
sigHash,
broadcast
);
Lit.Actions.setResponse({
response: response,
});
}
} catch (error: any) {
Lit.Actions.setResponse({ response: error.message });
}
};
go();