forked from zahin-mohammad/recover-btc-with-backup-bitgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
131 lines (119 loc) · 3.22 KB
/
index.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
import * as dotenv from "dotenv";
import {BaseCoin, EnvironmentName} from "@bitgo/sdk-core";
import { BitGoAPI } from '@bitgo/sdk-api';
import {command, run} from "cmd-ts";
import {
accessTokenFlag,
backupKeyFlag,
envFlag,
feeFlag,
oneTimePasscodeFlag,
passwordFlag,
recoveryBalanceFlag,
recoveryDestinationFlag,
userKeyFlag,
walletIdFlag,
} from "./common";
import {AbstractUtxoCoin,} from "@bitgo/abstract-utxo";
import * as utxolib from "@bitgo/utxo-lib";
import * as btcSdk from "@bitgo/sdk-coin-btc";
dotenv.config();
function assertIsUtxo(coin: BaseCoin): asserts coin is AbstractUtxoCoin {
if (!(coin instanceof AbstractUtxoCoin)) {
throw new Error("coin must be a utxo coin");
}
}
export async function main(args: {
env: EnvironmentName;
accessToken: string;
otp?: string;
walletId: string;
walletPassword: string;
recoveryDestination: string;
userKey: string;
backupKey: string;
balance: string;
fee: string;
}) {
const sdk = new BitGoAPI({ env: args.env, accessToken: args.accessToken });
btcSdk.register(sdk);
const coin = sdk.coin(args.env === 'prod' ? "btc" : "tbtc");
assertIsUtxo(coin);
if (args.otp){
await sdk.unlock({ otp: args.otp });
}
const wallet = await sdk.wallets().get({ id: args.walletId, gpk: true });
let userKeyWif: string;
try {
userKeyWif = sdk.decrypt({
input: args.userKey,
password: args.walletPassword,
});
} catch (e) {
console.log("Failed to decrypt user key.", e);
throw e;
}
let backupKeyWif: string;
try {
backupKeyWif = sdk.decrypt({
input: args.backupKey,
password: args.walletPassword,
});
} catch (e) {
console.log("Failed to decrypt backup key.", e);
throw e;
}
const createdTx = await wallet.createTransaction({
recipients: {
[args.recoveryDestination]: Number(args.balance),
},
fee: Number(args.fee),
bitgoFee:{
amount: 0,
address: '',
}
});
const halfSignedTx = await wallet.signTransaction({
transactionHex: createdTx.transactionHex,
unspents: createdTx.unspents,
signingKey: userKeyWif,
validate: true
});
const signedTx = await wallet.signTransaction({
transactionHex: halfSignedTx.tx,
unspents: createdTx.unspents,
signingKey: backupKeyWif,
validate: true,
fullLocalSigning: true,
});
const txb = utxolib.bitgo.createTransactionBuilderFromTransaction(utxolib.bitgo.createTransactionFromHex(signedTx.tx, coin.network));
const completedTx = txb.build();
const completedRawTx = completedTx.toHex();
console.log('----------------------------------')
console.log(`Signed Raw Tx: ${completedRawTx}`);
}
const app = command({
name: "recover-v1-safe-wallet -h",
args: {
env: envFlag,
walletId: walletIdFlag,
accessToken: accessTokenFlag,
otp: oneTimePasscodeFlag,
walletPassword: passwordFlag,
recoveryDestination: recoveryDestinationFlag,
userKey: userKeyFlag,
backupKey: backupKeyFlag,
balance: recoveryBalanceFlag,
fee: feeFlag,
},
handler: async (args) => {
try {
await main(args);
} catch (e) {
console.trace(e);
}
},
});
run(app, process.argv.slice(2))
.then(() => console.log("done"))
.catch((e) => console.trace(e));