forked from echovl/cardano-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_builder.go
346 lines (295 loc) · 10.3 KB
/
tx_builder.go
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package cardano
import (
"fmt"
"math"
"github.com/safanaj/cardano-go/crypto"
"golang.org/x/crypto/blake2b"
)
// TxBuilder is a transaction builder.
type TxBuilder struct {
tx *Tx
protocol *ProtocolParams
pkeys []crypto.PrvKey
changeReceiver *Address
additionalWitnesses uint
additionalFee Coin
}
// NewTxBuilder returns a new instance of TxBuilder.
func NewTxBuilder(protocol *ProtocolParams) *TxBuilder {
return &TxBuilder{
protocol: protocol,
pkeys: []crypto.PrvKey{},
tx: &Tx{
IsValid: true,
},
}
}
// AddInputs adds inputs to the transaction.
func (tb *TxBuilder) AddInputs(inputs ...*TxInput) {
tb.tx.Body.Inputs = append(tb.tx.Body.Inputs, inputs...)
}
// AddOutputs adds outputs to the transaction.
func (tb *TxBuilder) AddOutputs(outputs ...*TxOutput) {
tb.tx.Body.Outputs = append(tb.tx.Body.Outputs, outputs...)
}
// SetTtl sets the transaction's time to live.
func (tb *TxBuilder) SetTTL(ttl uint64) {
tb.tx.Body.TTL = NewUint64(ttl)
}
// SetFee sets the transactions's fee.
func (tb *TxBuilder) SetFee(fee Coin) {
tb.tx.Body.Fee = fee
}
// SetAdditionalWitnesses sets future witnesses for a partially signed transction.
// This is useful to compute the real length and so fee in advance
func (tb *TxBuilder) SetAdditionalWitnesses(witnesses uint) {
tb.additionalWitnesses = witnesses
}
// SetAdditionalFee sets arbitrary additional fee coins, like a tip or amount to burn.
// This is useful to allow workaround around the auto computation of the minimal fee
func (tb *TxBuilder) SetAdditionalFee(additionalFee Coin) {
tb.additionalFee = additionalFee
}
// AddAuxiliaryData adds auxiliary data to the transaction.
func (tb *TxBuilder) AddAuxiliaryData(data *AuxiliaryData) {
tb.tx.AuxiliaryData = data
}
// AddCertificate adds a certificate to the transaction.
func (tb *TxBuilder) AddCertificate(cert Certificate) {
tb.tx.Body.Certificates = append(tb.tx.Body.Certificates, cert)
}
// AddNativeScript adds a native script to the transaction.
func (tb *TxBuilder) AddNativeScript(script NativeScript) {
tb.tx.WitnessSet.Scripts = append(tb.tx.WitnessSet.Scripts, script)
}
// Mint adds a new multiasset to mint.
func (tb *TxBuilder) Mint(asset *Mint) {
tb.tx.Body.Mint = asset
}
// AddChangeIfNeeded instructs the builder to calculate the required fee for the
// transaction and to add an aditional output for the change if there is any.
func (tb *TxBuilder) AddChangeIfNeeded(changeAddr Address) {
tb.changeReceiver = &changeAddr
}
func (tb *TxBuilder) calculateAmounts() (*Value, *Value) {
input, output := NewValue(0), NewValue(tb.totalDeposits())
for _, in := range tb.tx.Body.Inputs {
input = input.Add(in.Amount)
}
for _, out := range tb.tx.Body.Outputs {
output = output.Add(out.Amount)
}
if tb.tx.Body.Mint != nil {
input = input.Add(NewValueWithAssets(0, tb.tx.Body.Mint.MultiAsset()))
}
return input, output
}
func (tb *TxBuilder) totalDeposits() Coin {
certs := tb.tx.Body.Certificates
var deposit Coin
if len(certs) != 0 {
for _, cert := range certs {
if cert.Type == StakeRegistration {
deposit += tb.protocol.KeyDeposit
}
}
}
return deposit
}
// MinFee computes the minimal fee required for the transaction.
// This assumes that the inputs-outputs are defined and signing keys are present.
func (tb *TxBuilder) MinFee() (Coin, error) {
// Set a temporary realistic fee in order to serialize a valid transaction
currentFee := tb.tx.Body.Fee
tb.tx.Body.Fee = 200000
if err := tb.build(); err != nil {
return 0, err
}
minFee := tb.calculateMinFee()
tb.tx.Body.Fee = currentFee
return minFee, nil
}
// MinCoinsForTxOut computes the minimal amount of coins required for a given transaction output.
// More info could be found in
// <https://github.com/input-output-hk/cardano-ledger/blob/master/doc/explanations/min-utxo-alonzo.rst>
func (tb *TxBuilder) MinCoinsForTxOut(txOut *TxOutput) Coin {
var size uint
if txOut.Amount.OnlyCoin() {
size = 1
} else {
numAssets := txOut.Amount.MultiAsset.numAssets()
assetsLength := txOut.Amount.MultiAsset.assetsLength()
numPIDs := txOut.Amount.MultiAsset.numPIDs()
size = 6 + uint(math.Trunc(
float64(numAssets*12+assetsLength+numPIDs*28+7)/8,
))
}
return Coin(utxoEntrySizeWithoutVal+size) * tb.protocol.CoinsPerUTXOWord
}
// Calculate minimum lovelace a transaction output needs to hold post alonzo.
// This implementation is copied from the origianl Haskell implementation:
// https://github.com/input-output-hk/cardano-ledger/blob/eb053066c1d3bb51fb05978eeeab88afc0b049b2/eras/babbage/impl/src/Cardano/Ledger/Babbage/Rules/Utxo.hs#L242-L265
// TODO:
// calculateMinFee computes the minimal fee required for the transaction.
func (tb *TxBuilder) calculateMinFee() Coin {
// checking for additionalWitnesses we gonna add fake/empty VKeyWitnesses just to guess the future length and so cost/fee
if tb.additionalWitnesses > 0 {
// we can assume the list of VKeyWitnessSet is not a nil value, as `build()` method is always allocating a slice
additionalVKeyWitnessSet := make([]VKeyWitness, tb.additionalWitnesses)
for i := uint(0); i < tb.additionalWitnesses; i++ {
additionalVKeyWitnessSet[i] = VKeyWitness{
VKey: crypto.PubKey(make([]byte, 32)),
Signature: make([]byte, 64),
}
}
tb.tx.WitnessSet.VKeyWitnessSet = append(tb.tx.WitnessSet.VKeyWitnessSet, additionalVKeyWitnessSet...)
}
// checking for auxiliary_data and if present add a fake 32 bytes for the future hash
if tb.tx.AuxiliaryData != nil {
fakeAuxDataHash32 := Hash32(make([]byte, 32))
tb.tx.Body.AuxiliaryDataHash = &fakeAuxDataHash32
}
txLength := uint64(len(tb.tx.Bytes()))
// restore tx as it was before to add fake additionalWitnesses
if tb.additionalWitnesses > 0 {
tb.tx.WitnessSet.VKeyWitnessSet = tb.tx.WitnessSet.VKeyWitnessSet[:len(tb.tx.WitnessSet.VKeyWitnessSet)-int(tb.additionalWitnesses)]
}
if tb.tx.AuxiliaryData != nil {
tb.tx.Body.AuxiliaryDataHash = nil
}
//////// the below is just an old a bit arbitrary approach, here just for memories/references,
//////// replaced by fake/empty additionalWitnesses approach
// // for each additional witnesses there will be an additional 100 bytes,
// // (32 public key, 64 signature, 4 index/key in cbor)
// txLength += uint64(tb.additionalWitnesses * 100)
// // apparently that is not enough, so just consider 1 additional byte
// // for each additional witness after the first
// if tb.additionalWitnesses > 1 {
// txLength += uint64(tb.additionalWitnesses - 1)
// }
return tb.protocol.MinFeeA*Coin(txLength) + tb.protocol.MinFeeB + tb.additionalFee
}
// Sign adds signing keys to create signatures for the witness set.
func (tb *TxBuilder) Sign(privateKeys ...crypto.PrvKey) {
tb.pkeys = append(tb.pkeys, privateKeys...)
}
// Reset resets the builder to its initial state.
func (tb *TxBuilder) Reset() {
tb.tx = &Tx{IsValid: true}
tb.pkeys = []crypto.PrvKey{}
tb.changeReceiver = nil
}
// Build returns a new transaction using the inputs, outputs and keys provided.
func (tb *TxBuilder) Build() (*Tx, error) {
inputAmount, outputAmount := tb.calculateAmounts()
// Check input-output value conservation
if tb.changeReceiver == nil {
totalProduced := outputAmount.Add(NewValue(tb.tx.Body.Fee))
if inputOutputCmp := totalProduced.Cmp(inputAmount); inputOutputCmp == 1 || inputOutputCmp == 2 {
return nil, fmt.Errorf(
"insufficient input in transaction, got %v want %v",
inputAmount,
totalProduced,
)
} else if inputOutputCmp == -1 {
return nil, fmt.Errorf(
"fee too small, got %v want %v",
tb.tx.Body.Fee,
inputAmount.Sub(totalProduced),
)
}
}
if tb.changeReceiver != nil {
err := tb.addChangeIfNeeded(inputAmount, outputAmount)
if err != nil {
return nil, err
}
}
if err := tb.build(); err != nil {
return nil, err
}
return tb.tx, nil
}
func (tb *TxBuilder) addChangeIfNeeded(inputAmount, outputAmount *Value) error {
// Temporary fee to serialize a valid transaction
tb.tx.Body.Fee = 2e5
// TODO: We should build a fake tx with hardcoded data like signatures, hashes, etc
if err := tb.build(); err != nil {
return err
}
minFee := tb.calculateMinFee()
outputAmount = outputAmount.Add(NewValue(minFee))
if inputOutputCmp := inputAmount.Cmp(outputAmount); inputOutputCmp == -1 || inputOutputCmp == 2 {
return fmt.Errorf(
"insufficient input in transaction, got %v want atleast %v",
inputAmount,
outputAmount,
)
} else if inputOutputCmp == 0 {
tb.tx.Body.Fee = minFee
return nil
}
// Construct change output
changeAmount := inputAmount.Sub(outputAmount)
changeOutput := NewTxOutput(*tb.changeReceiver, changeAmount)
changeMinCoins := tb.MinCoinsForTxOut(changeOutput)
if changeAmount.Coin < changeMinCoins {
if changeAmount.OnlyCoin() {
tb.tx.Body.Fee = minFee + changeAmount.Coin // burn change
return nil
}
return fmt.Errorf(
"insufficient input for change output with multiassets, got %v want %v",
inputAmount.Coin,
inputAmount.Coin+changeMinCoins-changeAmount.Coin,
)
}
tb.tx.Body.Outputs = append([]*TxOutput{changeOutput}, tb.tx.Body.Outputs...)
newMinFee := tb.calculateMinFee()
changeAmount.Coin = changeAmount.Coin + minFee - newMinFee
if changeAmount.Coin < changeMinCoins {
if changeAmount.OnlyCoin() {
tb.tx.Body.Fee = newMinFee + changeAmount.Coin // burn change
tb.tx.Body.Outputs = tb.tx.Body.Outputs[1:] // remove change output
return nil
}
return fmt.Errorf(
"insufficient input for change output with multiassets, got %v want %v",
inputAmount.Coin,
changeMinCoins,
)
}
tb.tx.Body.Fee = newMinFee
return nil
}
func (tb *TxBuilder) build() error {
if err := tb.buildBody(); err != nil {
return err
}
txHash, err := tb.tx.Hash()
if err != nil {
return err
}
// Create witness set
tb.tx.WitnessSet.VKeyWitnessSet = make([]VKeyWitness, len(tb.pkeys))
for i, pkey := range tb.pkeys {
tb.tx.WitnessSet.VKeyWitnessSet[i] = VKeyWitness{
VKey: pkey.PubKey(),
// for transaction we use Extended version of Sign method, not the ed25519 signing way
Signature: pkey.SignExtended(txHash),
}
}
return nil
}
func (tb *TxBuilder) buildBody() error {
if tb.tx.AuxiliaryData != nil {
auxBytes, err := cborEnc.Marshal(tb.tx.AuxiliaryData)
if err != nil {
return err
}
auxHash := blake2b.Sum256(auxBytes)
auxHash32 := Hash32(auxHash[:])
tb.tx.Body.AuxiliaryDataHash = &auxHash32
}
return nil
}