-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener.js
98 lines (79 loc) · 3.65 KB
/
listener.js
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
import fs from 'fs';
import ethers from 'ethers';
import BitcoinHelpers from '../tbtc.js/src/BitcoinHelpers.js';
import EthereumHelpers from '../tbtc.js/src/EthereumHelpers.js';
import TBTCSystem from "@keep-network/tbtc/artifacts/TBTCSystem.json";
import VendingMachine from "@keep-network/tbtc/artifacts/VendingMachine.json";
import TBTCToken from "@keep-network/tbtc/artifacts/TBTCToken.json";
import TBTCDepositToken from "@keep-network/tbtc/artifacts/TBTCDepositToken.json";
import Deposit from "@keep-network/tbtc/artifacts/Deposit.json";
import BondedECDSAKeep from "@keep-network/keep-ecdsa/artifacts/BondedECDSAKeep.json";
import DepositLog from "@keep-network/tbtc/artifacts/DepositLog.json";
if (process.argv.length < 3 || !process.argv[2]) {
console.error('node listener.js [redeemer-addr]');
process.exit(1);
}
async function main() {
try {
const ip = new ethers.providers.InfuraProvider('ropsten', process.env.INFURA_API);
const tbtcSysContract = new ethers.Contract(TBTCSystem.networks["3"].address, TBTCSystem.abi, ip);
const vendingContract = new ethers.Contract(VendingMachine.networks["3"].address, VendingMachine.abi, ip);
const tokenContract = new ethers.Contract(TBTCToken.networks["3"].address, TBTCToken.abi, ip);
const tdtContract = new ethers.Contract(TBTCDepositToken.networks["3"].address, TBTCDepositToken.abi, ip);
const depositLogContract = new ethers.Contract(TBTCSystem.networks["3"].address, DepositLog.abi, ip);
BitcoinHelpers.setElectrumConfig({
testnetWS: {
server: "tn.not.fyi",
port: 55002,
protocol: "ssl"
}
});
const redeemerAddr = process.argv[2];
const redemptionReqs = await depositLogContract.queryFilter(depositLogContract.filters.RedemptionRequested(null, redeemerAddr, null));
//console.log(redemptionReqs);
depositLogContract.on(depositLogContract.filters.RedemptionRequested(null, redeemerAddr, null), async (...args) => {
const [ depositAddr, requester, digest, utxoValue, redeemerOutputScript, requestedFee, outpoint] = args;
console.log(`redeeming ${depositAddr} for ${requester}`);
const outputVal = utxoValue.sub(requestedFee);
const unsignedTransaction = BitcoinHelpers.Transaction.constructOneInputOneOutputWitnessTransaction(
outpoint.replace("0x", ""),
0,
outputVal.toNumber(),
EthereumHelpers.bytesToRaw(redeemerOutputScript)
);
console.log(`unsignedTransaction: ${unsignedTransaction}`);
const d = new ethers.Contract(depositAddr, Deposit.abi, ip);
const k = new ethers.Contract(await d.getKeepAddress(), BondedECDSAKeep.abi, ip);
const depositPks = await tbtcSysContract.queryFilter(tbtcSysContract.filters.RegisteredPubkey(d.address));
if (depositPks.length < 1) {
console.log(`could not find PK for deposit ${depositAddr}`);
return;
}
const pk = depositPks[depositPks.length - 1].args;
// 0. depositAddr, 1. X, 2. Y, 3. timestamp
console.log(`waiting for signature`);
k.once(k.filters.SignatureSubmitted(digest), async (dig, r, s, recovery) => {
console.log(`got signature for ${d.address} spending ${outputVal.toString()}`)
const signedTransaction = BitcoinHelpers.Transaction.addWitnessSignature(
unsignedTransaction,
0,
r.replace("0x", ""),
s.replace("0x", ""),
BitcoinHelpers.publicKeyPointToPublicKeyString(
pk[1],
pk[2]
)
);
console.log(`broadcasting signedTransaction: ${signedTransaction}`);
const bTx = await BitcoinHelpers.Transaction.broadcast(signedTransaction);
console.log(`txid: ${bTx.transactionID}`);
});
});
} catch(err) {
console.error(`Could not authorize: ${err.message}`)
process.exit(1);
}
}
main().catch(err => {
console.error(err);
})