-
Notifications
You must be signed in to change notification settings - Fork 2
/
p2tr_ptlc.ts
269 lines (227 loc) · 9.98 KB
/
p2tr_ptlc.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { strict as assert } from 'assert';
import * as bitcoin from 'bitcoinjs-lib';
import { randomBytes } from 'crypto';
import { ECPairFactory, ECPairInterface } from 'ecpair';
import * as curve from 'tiny-secp256k1';
import { btc, createTaprootOutput, ecPrivateMul, input, negateIfOddPubkey, send, sleep, tapLeaf } from './btc';
const ECPair = ECPairFactory(curve);
const network = bitcoin.networks.testnet;
const hashtype = bitcoin.Transaction.SIGHASH_DEFAULT;
function bip340Hash(R: Uint8Array, P: Uint8Array, m: Uint8Array): Uint8Array {
return bitcoin.crypto.taggedHash('BIP0340/challenge', Buffer.concat([R.slice(-32), P.slice(-32), m]));
}
class Adaptor {
public s: Uint8Array;
public R: Uint8Array;
public T: Uint8Array;
public constructor(s: Uint8Array, R: Uint8Array, T: Uint8Array) {
this.s = s;
this.R = R;
this.T = T;
}
public serialize(): string {
const b = Buffer.allocUnsafe(98);
Buffer.from(this.s).copy(b, 0);
Buffer.from(this.R).copy(b, 32);
Buffer.from(this.T).copy(b, 65);
return b.toString('hex');
}
public static deserialize(s: string): Adaptor {
const b = Buffer.from(s, 'hex');
assert(b.length === 98);
return new Adaptor(b.subarray(0, 32), b.subarray(32, 65), b.subarray(65));
}
public static sign(m: Uint8Array, keypair: ECPairInterface, T: Uint8Array): Adaptor {
let k = randomBytes(32);
// R = T + k*G
let RT = curve.pointAddScalar(T, k);
while (RT[0] & 1) {
k = randomBytes(32);
RT = curve.pointAddScalar(T, k);
}
// k + e*d
return new Adaptor(
curve.privateAdd(
k,
ecPrivateMul(bip340Hash(RT, keypair.publicKey, m), negateIfOddPubkey(keypair.privateKey)),
),
curve.pointFromScalar(k),
T,
);
}
public verify(m: Buffer, P: Buffer): boolean {
const RT = curve.pointAdd(this.R, this.T);
if (RT[0] !== 2) {
return false;
}
const Pclone = Buffer.from(P);
Pclone[0] = 0x02;
const eP = curve.pointMultiply(Pclone, bip340Hash(RT, Pclone, m));
// negate to get -e*P, which we add to s*G to get R' = s*G - e*P
eP[0] ^= 1;
const Rp = curve.pointAdd(curve.pointFromScalar(this.s), eP);
return !Buffer.compare(this.R, Rp);
}
public solve(t: Buffer): Buffer {
if (Buffer.compare(this.T, curve.pointFromScalar(t))) {
throw new Error('Invalid scalar for adaptor signature');
}
return Buffer.concat([curve.pointAdd(this.R, this.T).slice(-32), curve.privateAdd(this.s, t)]);
}
public extract(sig: Buffer): Buffer {
return Buffer.from(curve.privateSub(sig.subarray(32), this.s));
}
}
const internalKey = Buffer.from(curve.pointFromScalar(bitcoin.crypto.sha256(Buffer.from('unknown key'))));
internalKey[0] = 0x02;
function lockupAddress(
key1: Buffer,
key2: Buffer,
): ReturnType<typeof createTaprootOutput> & { leafScript: Buffer; first: boolean } {
const xonly1 = key1.subarray(-32);
const xonly2 = key2.subarray(-32);
const first = xonly1 < xonly2;
const [k1, k2] = first ? [xonly1, xonly2] : [xonly2, xonly1];
const leafScript = bitcoin.script.compile([k1, bitcoin.opcodes.OP_CHECKSIGVERIFY, k2, bitcoin.opcodes.OP_CHECKSIG]);
return { ...createTaprootOutput(internalKey, tapLeaf(leafScript)), leafScript, first };
}
const input_sat = 10000;
const fee_rate = 1;
const tx_size = 153;
/*
const tx_size =
4 + // ver
(1 + 1) / 4 + // segwit
1 + // input count
32 + 4 + 1 + 4 + // input 0
1 + // ouput count
8 + 1 + (1 + 1 + 32) + // output 0
(
1 + // witness stack
1 + 64 + // witness elem (sig1)
1 + 64 + // witness elem (sig2)
1 + (1 + 32 + 1 + 1 + 32 + 1) + // witness elem (tapscript)
1 + 33 // witness elem (control)
) / 4
+ 4 // locktime
*/
const fee_sat = tx_size * fee_rate;
const reltimelock = 144;
if (process.argv[2] == 'bob') {
bob();
} else {
alice();
}
async function alice() {
console.log('User: alice, run "node p2tr_ptlc bob" for the "bob" user');
const keypair = ECPair.makeRandom({ network });
console.log('Public key', keypair.publicKey.toString('hex'));
const otherKey = ECPair.fromPublicKey(Buffer.from(await input("give me bob's public method key: "), 'hex'));
const lockup = lockupAddress(keypair.publicKey, otherKey.publicKey);
console.log('Lockup address', lockup.address);
// create lockup transaction and ask the wallet to fill in inputs and change (fundrawtransaction)
const ltx = new bitcoin.Transaction();
ltx.version = 2;
ltx.addOutput(lockup.scriptPubKey, input_sat);
console.log(`paste the transaction from
bitcoin-cli -testnet fundrawtransaction ${ltx.toHex()} | jq -r '.hex' | bitcoin-cli -testnet -stdin signrawtransactionwithwallet
`);
const ltxfhex = await input('here: ');
const ltxf = bitcoin.Transaction.fromHex(ltxfhex);
const vout = ltxf.outs.findIndex(x => !x.script.compare(lockup.scriptPubKey));
console.log('give this output point to bob so he can sign the refund tx', ltxf.getId() + ':' + vout);
// craft refund tx (sequence=reltimelock, output=alice)
const txr = new bitcoin.Transaction();
txr.version = 2;
txr.addInput(Buffer.from(ltxf.getId(), 'hex').reverse(), vout, reltimelock);
txr.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, keypair.publicKey.subarray(-32)]), input_sat - fee_sat);
const sighashr = txr.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));
const refsig = Buffer.from(await input('paste the refund sig: '), 'hex');
if (otherKey.verifySchnorr(sighashr, refsig)) {
console.log('valid signature!');
} else {
throw new Error('invalid refund signature');
}
const myrefsig = keypair.signSchnorr(sighashr);
const control = Buffer.allocUnsafe(33);
control[0] = 0xc0 | lockup.parity;
internalKey.copy(control, 1, 1);
txr.setWitness(0, [...(lockup.first ? [refsig, myrefsig] : [myrefsig, refsig]), lockup.leafScript, control]);
console.log('keep the refund tx, it can be sent after ' + reltimelock + ' blocks:', txr.toHex());
console.log('lockup sent', await send(ltxfhex));
const T = Buffer.from(await input("give bob's payment point: "), 'hex');
// craft payout tx (sequence=0xffffffff,output=bob)
const txp = new bitcoin.Transaction();
txp.version = 2;
txp.addInput(Buffer.from(ltxf.getId(), 'hex').reverse(), vout);
txp.addOutput(
bitcoin.script.compile([bitcoin.opcodes.OP_1, otherKey.publicKey.subarray(-32)]),
input_sat - fee_sat,
);
const sighashp = txp.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));
const a = Adaptor.sign(sighashp, keypair, T);
console.log('adaptor signature for bob', a.serialize());
console.log('checking the chain every 5 seconds to see if bob claimed the funds');
let bob_spending: string;
while (true) {
await sleep(5000);
try {
bob_spending = await btc('getrawtransaction', txp.getId());
console.log('transaction found!');
break;
} catch (e) {
console.log('nothing found yet...');
}
}
const tx = bitcoin.Transaction.fromHex(bob_spending);
const bob_sig = tx.ins[0].witness[lockup.first ? 1 : 0];
const t = a.extract(bob_sig);
console.log("extracted t from bob's signature:", t.toString('hex'));
if (T.compare(curve.pointFromScalar(t))) {
throw new Error('t*G != T, evil bob stole my private key');
} else {
console.log('t is valid!');
}
}
async function bob() {
console.log('User: bob');
const keypair = ECPair.makeRandom({ network });
const t = randomBytes(32);
const T = Buffer.from(curve.pointFromScalar(t));
console.log('Public key', keypair.publicKey.toString('hex'));
const otherKey = ECPair.fromPublicKey(Buffer.from(await input("give me alice's public key: "), 'hex'));
const lockup = lockupAddress(keypair.publicKey, otherKey.publicKey);
console.log('Lockup address', lockup.address);
const [txid, vout] = (await input("give me alice's lockup transaction output point: ")).split(':');
// craft refund tx (sequence=reltimelock, output=alice)
const txr = new bitcoin.Transaction();
txr.version = 2;
txr.addInput(Buffer.from(txid, 'hex').reverse(), parseInt(vout), reltimelock);
txr.addOutput(
bitcoin.script.compile([bitcoin.opcodes.OP_1, otherKey.publicKey.subarray(-32)]),
input_sat - fee_sat,
);
const sighashr = txr.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));
console.log('refund signature for alice', keypair.signSchnorr(sighashr).toString('hex'));
console.log('Adaptor payment point: ', T.toString('hex'));
// craft payout tx (sequence=0xffffffff,output=bob)
const txp = new bitcoin.Transaction();
txp.version = 2;
txp.addInput(Buffer.from(txid, 'hex').reverse(), parseInt(vout));
txp.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, keypair.publicKey.subarray(-32)]), input_sat - fee_sat);
const sighashp = txp.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));
const a = Adaptor.deserialize(await input("give me alice's adaptor signature: "));
if (T.compare(a.T) || !a.verify(sighashp, otherKey.publicKey)) {
throw new Error('invalid adaptor sig');
} else {
console.log('valid adaptor!');
}
const sig1 = keypair.signSchnorr(sighashp);
const sig2 = a.solve(t);
const control = Buffer.allocUnsafe(33);
control[0] = 0xc0 | lockup.parity;
internalKey.copy(control, 1, 1);
txp.setWitness(0, [...(lockup.first ? [sig2, sig1] : [sig1, sig2]), lockup.leafScript, control]);
console.log('payout sent', await send(txp.toHex()));
console.log('alice should have got the exact same payment secret', t.toString('hex'));
}