-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
235 lines (213 loc) · 5.86 KB
/
utils.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
import {AssetDOGE, assetToBase} from '@xchainjs/xchain-util';
import * as Dogecoin from 'bitcoinjs-lib';
import coininfo from 'coininfo';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import accumulative from 'coinselect/accumulative';
import {assetAmount} from '../../../helpers/amount-helper';
import {compileMemo} from '../../../helpers/calculate-utxo-fee';
import {UTXO} from '../../xchain-bitcoin/src';
import {
Address,
Balance,
Network,
TxHash,
TxParams,
} from '../../xchain-client/src';
import {DOGE_DECIMAL} from './const';
import * as nodeApi from './node-api';
import * as sochain from './sochain-api';
import {BroadcastTxParams} from './types/common';
import {AddressParams, DogeAddressUTXO} from './types/sochain-api-types';
/**
* Get the average value of an array.
*
* @param {number[]} array
* @returns {number} The average value.
*/
export function arrayAverage(array: number[]): number {
let sum = 0;
array.forEach((value) => {
sum += value;
});
return sum / array.length;
}
/**
* Get Dogecoin network to be used with bitcoinjs.
*
* @param {Network} network
* @returns {Dogecoin.networks.Network} The Doge network.
*/
export const dogeNetwork = (network: Network): Dogecoin.networks.Network => {
switch (network) {
case Network.Mainnet:
return coininfo.dogecoin.main.toBitcoinJS();
case Network.Testnet: {
// Latest coininfo on NPM doesn't contain dogetest config information
const bip32 = {
private: 0x04358394,
public: 0x043587cf,
};
const {test} = coininfo.dogecoin;
test.versions.bip32 = bip32;
return test.toBitcoinJS();
}
default:
throw new Error('unknown network: ' + network);
}
};
/**
* Get the balances of an address.
*
* @param {AddressParams} params
* @returns {Balance[]} The balances of the given address.
*/
export const getBalance = async (params: AddressParams): Promise<Balance[]> => {
try {
const balance = await sochain.getBalance(params);
return [
{
asset: AssetDOGE,
amount: balance,
},
];
} catch (error) {
throw new Error(`Could not get balances for address ${params.address}`);
}
};
/**
* Validate the Doge address.
*
* @param {string} address
* @param {Network} network
* @returns {boolean} `true` or `false`.
*/
export const validateAddress = (
network: Network,
address: Address,
): boolean => {
try {
Dogecoin.address.toOutputScript(address, dogeNetwork(network));
return true;
} catch (error) {
return false;
}
};
/**
* Scan UTXOs from sochain.
*
* @param {AddressParams} params
* @returns {UTXO[]} The UTXOs of the given address.
*/
export const scanUTXOs = async (params: AddressParams): Promise<UTXO[]> => {
const unspent: DogeAddressUTXO[] = await sochain.getUnspentTxs(params);
const utxos: UTXO[] = [];
for (const utxo of unspent) {
utxos.push({
hash: utxo.txid,
index: utxo.output_no,
value: assetToBase(assetAmount(utxo.value, DOGE_DECIMAL))
.amount()
.toNumber(),
txHex: utxo.txid,
witnessUtxo: null,
});
}
return utxos;
};
/**
* Build transcation.
*
* @param {BuildParams} params The transaction build options.
* @returns {Transaction}
*/
export const buildTx = async ({
amount,
recipient,
memo,
feeRate,
sender,
network,
sochainUrl,
}: TxParams & {
feeRate: number;
sender: Address;
network: Network;
sochainUrl: string;
}): Promise<{psbt: Dogecoin.Psbt; utxos: UTXO[]}> => {
if (!validateAddress(network, recipient)) throw new Error('Invalid address');
const utxos = await scanUTXOs({sochainUrl, network, address: sender});
if (utxos.length === 0) throw new Error('No utxos to send');
const feeRateWhole = Number(feeRate.toFixed(0));
const compiledMemo = memo ? compileMemo(memo) : null;
const targetOutputs = [];
// 1. output to recipient
targetOutputs.push({
address: recipient,
value: amount.amount().toNumber(),
});
// 2. add output memo to targets (optional)
if (compiledMemo) {
targetOutputs.push({script: compiledMemo, value: 0});
}
const {inputs, outputs} = accumulative(utxos, targetOutputs, feeRateWhole);
// .inputs and .outputs will be undefined if no solution was found
if (!inputs || !outputs)
throw new Error('Balance insufficient for transaction');
const psbt = new Dogecoin.Psbt({network: dogeNetwork(network)}); // Network-specific
psbt.setMaximumFeeRate(5500000);
const params = {sochainUrl, network, address: sender};
for (const utxo of inputs) {
psbt.addInput({
hash: utxo.hash,
index: utxo.index,
nonWitnessUtxo: Buffer.from(
(await sochain.getTx({hash: utxo.hash, ...params})).tx_hex,
'hex',
),
});
}
// Outputs
outputs.forEach((output: Dogecoin.PsbtTxOutput) => {
if (!output.address) {
// an empty address means this is the change address
output.address = sender;
}
if (!output.script) {
psbt.addOutput(output);
return;
}
if (compiledMemo) {
// we need to add the compiled memo this way to
// avoid dust error tx when accumulating memo output with 0 value
psbt.addOutput({script: compiledMemo, value: 0});
}
});
return {psbt, utxos};
};
/**
* Broadcast the transaction.
*
* @param {BroadcastTxParams} params The transaction broadcast options.
* @returns {TxHash} The transaction hash.
*/
export const broadcastTx = (params: BroadcastTxParams): Promise<TxHash> => {
if (params.network === Network.Testnet) {
return nodeApi.broadcastTxToSochain(params);
}
return nodeApi.broadcastTxToBlockCypher(params);
};
/**
* Get address prefix based on the network.
*
*
**/
export const getPrefix = (network: Network) => {
switch (network) {
case Network.Mainnet:
case Network.Testnet:
return 'n';
default:
throw new Error('unknown error');
}
};