-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsafe_withdraw.js
181 lines (169 loc) · 5.74 KB
/
safe_withdraw.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
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
const {
MixinApi,
MixinCashier,
buildSafeTransactionRecipient,
getUnspentOutputsForRecipients,
buildSafeTransaction,
encodeSafeTransaction,
signSafeTransaction,
blake3Hash,
} = require('..');
const { v4 } = require('uuid');
const keystore = require('../keystore.json');
const withdrawal_asset_id = 'b91e18ff-a9ae-3dc7-8679-e935d9a4b34b';
const withdrawal_amount = '1';
const withdrawal_memo = 'memo';
const withdrawal_destination = '';
const spendPrivateKey = '';
const main = async () => {
const client = MixinApi({ keystore });
const asset = await client.safe.fetchAsset(withdrawal_asset_id);
const chain = asset.chain_id === asset.asset_id ? asset : await client.safe.fetchAsset(asset.chain_id);
const fees = await client.safe.fetchFee(asset.asset_id, withdrawal_destination);
const assetFee = fees.find(f => f.asset_id === asset.asset_id);
const chainFee = fees.find(f => f.asset_id === chain.asset_id);
const fee = assetFee ?? chainFee;
console.log(fee);
// withdrawal with chain asset as fee
if (fee.asset_id !== asset.asset_id) {
const outputs = await client.utxo.safeOutputs({
asset: withdrawal_asset_id,
state: 'unspent',
});
const feeOutputs = await client.utxo.safeOutputs({
asset: fee.asset_id,
state: 'unspent',
});
console.log(outputs, feeOutputs);
let recipients = [
// withdrawal output, must be put first
{
amount: withdrawal_amount,
destination: withdrawal_destination,
tag: withdrawal_memo,
},
];
const { utxos, change } = getUnspentOutputsForRecipients(outputs, recipients);
if (!change.isZero() && !change.isNegative()) {
// add change output if needed
recipients.push(buildSafeTransactionRecipient(outputs[0].receivers, outputs[0].receivers_threshold, change.toString()));
}
// the index of ghost keys must be the same with the index of outputs
// but withdrawal output doesnt need ghost key, so index + 1
const ghosts = await client.utxo.ghostKey(
recipients
.filter(r => 'members' in r)
.map((r, i) => ({
hint: v4(),
receivers: r.members,
index: i + 1,
})),
);
// spare the 0 inedx for withdrawal output, withdrawal output doesnt need ghost key
const tx = buildSafeTransaction(utxos, recipients, [undefined, ...ghosts], 'mainnet-transaction-extra');
console.log(tx);
const raw = encodeSafeTransaction(tx);
const ref = blake3Hash(Buffer.from(raw, 'hex')).toString('hex');
const feeRecipients = [
// fee output
buildSafeTransactionRecipient([MixinCashier], 1, fee.amount),
];
const { utxos: feeUtxos, change: feeChange } = getUnspentOutputsForRecipients(feeOutputs, feeRecipients);
if (!feeChange.isZero() && !feeChange.isNegative()) {
// add fee change output if needed
feeRecipients.push(buildSafeTransactionRecipient(feeOutputs[0].receivers, feeOutputs[0].receivers_threshold, feeChange.toString()));
}
const feeGhosts = await client.utxo.ghostKey(
feeRecipients.map((r, i) => ({
hint: v4(),
receivers: r.members,
index: i,
})),
);
const feeTx = buildSafeTransaction(feeUtxos, feeRecipients, feeGhosts, 'mainnet-fee-transaction-extra', [ref]);
console.log(feeTx);
const feeRaw = encodeSafeTransaction(feeTx);
console.log(feeRaw);
const txId = v4();
const feeId = v4();
console.log(txId, feeId);
let txs = await client.utxo.verifyTransaction([
{
raw,
request_id: txId,
},
{
raw: feeRaw,
request_id: feeId,
},
]);
const signedRaw = signSafeTransaction(tx, txs[0].views, spendPrivateKey);
const signedFeeRaw = signSafeTransaction(feeTx, txs[1].views, spendPrivateKey);
const res = await client.utxo.sendTransactions([
{
raw: signedRaw,
request_id: txId,
},
{
raw: signedFeeRaw,
request_id: feeId,
},
]);
console.log(res);
}
// withdrawal with asset as fee
else {
const outputs = await client.utxo.safeOutputs({
asset: withdrawal_asset_id,
state: 'unspent',
});
console.log(outputs);
let recipients = [
// withdrawal output, must be put first
{
amount: withdrawal_amount,
destination: withdrawal_destination,
tag: withdrawal_memo,
},
// fee output
buildSafeTransactionRecipient([MixinCashier], 1, fee.amount),
];
const { utxos, change } = getUnspentOutputsForRecipients(outputs, recipients);
if (!change.isZero() && !change.isNegative()) {
// add change output if needed
recipients.push(buildSafeTransactionRecipient(outputs[0].receivers, outputs[0].receivers_threshold, change.toString()));
}
// the index of ghost keys must be the same with the index of outputs
// but withdrawal output doesnt need ghost key, so index + 1
const ghosts = await client.utxo.ghostKey(
recipients
.filter(r => 'members' in r)
.map((r, i) => ({
hint: v4(),
receivers: r.members,
index: i + 1,
})),
);
// spare the 0 inedx for withdrawal output, withdrawal output doesnt need ghost key
const tx = buildSafeTransaction(utxos, recipients, [undefined, ...ghosts], 'mainnet-transaction-extra');
console.log(tx);
const raw = encodeSafeTransaction(tx);
const request_id = v4();
console.log(request_id);
let txs = await client.utxo.verifyTransaction([
{
raw,
request_id,
},
]);
const signedRaw = signSafeTransaction(tx, txs[0].views, spendPrivateKey);
const res = await client.utxo.sendTransactions([
{
raw: signedRaw,
request_id,
},
]);
console.log(res);
}
};
main();