-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathatomicTransactionBuilder.ts
More file actions
217 lines (194 loc) · 6.53 KB
/
atomicTransactionBuilder.ts
File metadata and controls
217 lines (194 loc) · 6.53 KB
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
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { TransactionType } from '@bitgo/sdk-core';
import { TransactionBuilder } from './transactionBuilder';
import { Transaction } from './transaction';
import { TransferableInput, Int, Id, TypeSymbols, Credential } from '@flarenetwork/flarejs';
import { DecodedUtxoObj } from './iface';
import utils from './utils';
// Interface for objects that can provide an amount
interface Amounter {
_type: TypeSymbols;
amount: () => bigint;
toBytes: () => Uint8Array;
}
export abstract class AtomicTransactionBuilder extends TransactionBuilder {
protected _externalChainId: Buffer;
protected recoverSigner = false;
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
this.transaction = new Transaction(_coinConfig);
}
/**
* Create inputs and outputs from UTXOs
* @param {bigint} amount Amount to transfer
* @return {
* inputs: TransferableInput[];
* outputs: TransferableInput[];
* credentials: Credential[];
* }
* @protected
*/
protected createInputOutput(amount: bigint): {
inputs: TransferableInput[];
outputs: TransferableInput[];
credentials: Credential[];
} {
const sender = (this.transaction as Transaction)._fromAddresses.slice();
if (this.recoverSigner) {
// switch first and last signer
const tmp = sender.pop();
sender.push(sender[0]);
if (tmp) {
sender[0] = tmp;
}
}
let totalAmount = BigInt(0);
const inputs: TransferableInput[] = [];
const outputs: TransferableInput[] = [];
const credentials: Credential[] = [];
(this.transaction as Transaction)._utxos.forEach((utxo: DecodedUtxoObj) => {
const utxoAmount = BigInt(utxo.amount);
totalAmount += utxoAmount;
// Create input
const input = {
_type: TypeSymbols.Input,
amount: () => utxoAmount,
sigIndices: sender.map((_, i) => i),
toBytes: () => new Uint8Array(),
};
// Create asset with Amounter interface
const assetId: Amounter = {
_type: TypeSymbols.BaseTx,
amount: () => utxoAmount,
toBytes: () => {
const bytes = new Uint8Array(Buffer.from((this.transaction as Transaction)._assetId, 'hex'));
return bytes;
},
};
// Create TransferableInput
const transferableInput = new TransferableInput(
{
_type: TypeSymbols.UTXOID,
txID: new Id(new Uint8Array(Buffer.from(utxo.txid, 'hex'))),
outputIdx: new Int(Number(utxo.outputidx)),
ID: () => utxo.txid,
toBytes: () => {
const txIdBytes = new Uint8Array(Buffer.from(utxo.txid, 'hex'));
const outputIdxBytes = new Uint8Array(4);
new DataView(outputIdxBytes.buffer).setInt32(0, Number(utxo.outputidx), true);
return Buffer.concat([txIdBytes, outputIdxBytes]);
},
},
new Id(new Uint8Array(Buffer.from(utxo.outputidx.toString()))),
assetId
);
// Set input properties
Object.assign(transferableInput, { input });
inputs.push(transferableInput);
// Create empty credential for each input
const emptySignatures = sender.map(() => utils.createNewSig(''));
credentials.push(new Credential(emptySignatures));
});
// Create output if there is change
if (totalAmount > amount) {
const changeAmount = totalAmount - amount;
const output = {
_type: TypeSymbols.BaseTx,
amount: () => changeAmount,
addresses: sender,
locktime: (this.transaction as Transaction)._locktime,
threshold: (this.transaction as Transaction)._threshold,
toBytes: () => new Uint8Array(),
};
// Create asset with Amounter interface
const assetId: Amounter = {
_type: TypeSymbols.BaseTx,
amount: () => changeAmount,
toBytes: () => {
const bytes = new Uint8Array(Buffer.from((this.transaction as Transaction)._assetId, 'hex'));
return bytes;
},
};
// Create TransferableOutput
const transferableOutput = new TransferableInput(
{
_type: TypeSymbols.UTXOID,
txID: new Id(new Uint8Array(32)),
outputIdx: new Int(0),
ID: () => '',
toBytes: () => {
const txIdBytes = new Uint8Array(32);
const outputIdxBytes = new Uint8Array(4);
return Buffer.concat([txIdBytes, outputIdxBytes]);
},
},
new Id(new Uint8Array([0])),
assetId
);
// Set output properties
Object.assign(transferableOutput, { output });
outputs.push(transferableOutput);
}
return {
inputs,
outputs,
credentials,
};
}
/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
this.buildFlareTransaction();
this.setTransactionType(this.transactionType);
if (this.hasSigner()) {
// Sign sequentially to ensure proper order
for (const keyPair of this._signer) {
await this.transaction.sign(keyPair);
}
}
return this.transaction;
}
/**
* Builds the avax transaction. transaction field is changed.
*/
protected abstract buildFlareTransaction(): void;
protected abstract get transactionType(): TransactionType;
/**
* Fee is fix for AVM atomic tx.
*
* @returns network.txFee
* @protected
*/
protected get fixedFee(): string {
return this.transaction._network.txFee;
}
/**
* Set the transaction type
*
* @param {TransactionType} transactionType The transaction type to be set
*/
setTransactionType(transactionType: TransactionType): void {
this.transaction._type = transactionType;
}
/**
* The internal chain is the one set for the coin in coinConfig.network. The external chain is the other chain involved.
* The external chain id is the source on import and the destination on export.
*
* @param {string} chainId - id of the external chain
*/
externalChainId(chainId: string | Buffer): this {
const newTargetChainId = typeof chainId === 'string' ? utils.cb58Decode(chainId) : Buffer.from(chainId);
this.validateChainId(newTargetChainId);
this._externalChainId = newTargetChainId;
return this;
}
/**
* Set the transaction fee
*
* @param {string | bigint} feeValue - the fee value
*/
fee(feeValue: string | bigint): this {
const fee = typeof feeValue === 'string' ? feeValue : feeValue.toString();
(this.transaction as Transaction)._fee.fee = fee;
return this;
}
}