-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransaction_builder_next.go
209 lines (190 loc) · 6.21 KB
/
transaction_builder_next.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
package api
import (
"encoding/hex"
"errors"
"fmt"
account "github.com/MixinNetwork/mobilecoin-account"
"github.com/MixinNetwork/mobilecoin-account/types"
)
const (
BULLETPROOF_DOMAIN_TAG = "mc_bulletproof_transcript"
AMOUNT_VALUE_DOMAIN_TAG = "mc_amount_value"
AMOUNT_TOKEN_ID_DOMAIN_TAG = "mc_amount_token_id"
AMOUNT_BLINDING_DOMAIN_TAG = "mc_amount_blinding"
HASH_TO_POINT_DOMAIN_TAG = "mc_onetime_key_hash_to_point"
HASH_TO_SCALAR_DOMAIN_TAG = "mc_onetime_key_hash_to_scalar"
RING_MLSAG_CHALLENGE_DOMAIN_TAG = "mc_ring_mlsag_challenge"
TXOUT_CONFIRMATION_NUMBER_DOMAIN_TAG = "mc_tx_out_confirmation_number"
AMOUNT_SHARED_SECRET_DOMAIN_TAG = "mc_amount_shared_secret"
AMOUNT_BLINDING_FACTORS_DOMAIN_TAG = "mc_amount_blinding_factors"
MILLIMOB_TO_PICOMOB = 1_000_000_000
PICOMOB = 1_000_000_000_000 // precision = 12
MOB_MINIMUM_FEE = 400_000_000
MINIMUM_EUSD = 1_000_000
MAX_TOMBSTONE_BLOCKS = 20160
MAX_INPUTS = 16
RING_SIZE = 11 // Each input ring must contain this many elements.
)
type UTXO struct {
TransactionHash string
Index uint32
Amount uint64
PrivateKey string
ScriptPubKey string
}
type Output struct {
TransactionHash string
RawTransaction string
SharedSecret string
Fee uint64
OutputIndex int64
OutputHash string
ChangeIndex int64
ChangeHash string
ChangeAmount uint64
}
func TransactionBuilderBuild(inputs []*UTXO, proofs *Proofs, output string, amount, fee uint64, tombstone, memo uint64, tokenID, version uint, changeStr string) (*Output, error) {
recipient, err := account.DecodeB58Code(output)
if err != nil {
return nil, err
}
change, err := account.DecodeB58Code(changeStr)
if err != nil {
return nil, err
}
var totalAmount uint64
for _, input := range inputs {
totalAmount += input.Amount
}
changeAmount := totalAmount - amount - fee
if changeAmount < MOB_MINIMUM_FEE {
changeAmount = 0
fee += changeAmount
}
if changeAmount > 0 && changeAmount < MILLIMOB_TO_PICOMOB {
return nil, errors.New("invalid change amount")
}
if totalAmount != (amount + fee + changeAmount) {
return nil, errors.New("invalid amount")
}
inputCs, err := BuildRingElements(inputs, proofs)
if err != nil {
return nil, err
}
txC, err := MCTransactionBuilderCreateC(inputCs, amount, changeAmount, fee, tombstone, memo, tokenID, version, recipient, change)
if err != nil {
return nil, err
}
return &Output{
TransactionHash: hex.EncodeToString(txC.TxOut.PublicKey.GetData()),
RawTransaction: hex.EncodeToString(txC.Tx),
SharedSecret: hex.EncodeToString(txC.ShareSecretOut),
Fee: fee,
OutputIndex: 0,
OutputHash: hex.EncodeToString(txC.TxOut.PublicKey.GetData()),
ChangeIndex: 0,
ChangeHash: hex.EncodeToString(txC.TxOutChange.PublicKey.GetData()),
ChangeAmount: changeAmount,
}, nil
}
func UnmarshalTx(tx *types.Tx) *Tx {
return &Tx{
Prefix: UnmarshalPrefix(tx.Prefix),
Signature: UnmarshalSignatureRctBulletproofs(tx.Signature),
}
}
func UnmarshalPrefix(prefix *types.TxPrefix) *TxPrefix {
ins := make([]*TxIn, len(prefix.Inputs))
for i, in := range prefix.Inputs {
ring := make([]*TxOut, len(in.Ring))
for i, r := range in.Ring {
ring[i] = UnmarshalTxOut(r)
}
proofs := make([]*TxOutMembershipProof, len(in.Proofs))
for i, p := range in.Proofs {
proofs[i] = UnmarshalTxOutMembershipProof(p)
}
ins[i] = &TxIn{
Ring: ring,
Proofs: proofs,
}
}
outs := make([]*TxOut, len(prefix.Outputs))
for i, out := range prefix.Outputs {
outs[i] = UnmarshalTxOut(out)
}
return &TxPrefix{
Inputs: ins,
Outputs: outs,
Fee: FeeValue(prefix.Fee),
TombstoneBlock: TombstoneValue(prefix.TombstoneBlock),
}
}
func UnmarshalTxOut(out *types.TxOut) *TxOut {
txOut := &TxOut{
TargetKey: hex.EncodeToString(out.TargetKey.GetData()),
PublicKey: hex.EncodeToString(out.PublicKey.GetData()),
EFogHint: hex.EncodeToString(out.EFogHint.GetData()),
EMemo: hex.EncodeToString(out.EMemo.GetData()),
}
if v1 := out.GetMaskedAmountV1(); v1 != nil {
txOut.Amount = &Amount{
Commitment: hex.EncodeToString(v1.Commitment.GetData()),
MaskedValue: MaskedValue(v1.MaskedValue),
MaskedTokenID: hex.EncodeToString(v1.MaskedTokenId),
Version: 1,
}
}
if v2 := out.GetMaskedAmountV2(); v2 != nil {
txOut.Amount = &Amount{
Commitment: hex.EncodeToString(v2.Commitment.GetData()),
MaskedValue: MaskedValue(v2.MaskedValue),
MaskedTokenID: hex.EncodeToString(v2.MaskedTokenId),
Version: 2,
}
}
return txOut
}
func UnmarshalTxOutMembershipProof(proof *types.TxOutMembershipProof) *TxOutMembershipProof {
elements := make([]*TxOutMembershipElement, len(proof.Elements))
for i, e := range proof.Elements {
elements[i] = &TxOutMembershipElement{
Range: &Range{
From: fmt.Sprint(e.Range.From),
To: fmt.Sprint(e.Range.To),
},
Hash: hex.EncodeToString(e.Hash.GetData()),
}
}
return &TxOutMembershipProof{
Index: fmt.Sprint(proof.Index),
HighestIndex: fmt.Sprint(proof.HighestIndex),
Elements: elements,
}
}
func UnmarshalSignatureRctBulletproofs(signature *types.SignatureRctBulletproofs) *SignatureRctBulletproofs {
signatures := make([]*RingMLSAG, len(signature.RingSignatures))
for i, s := range signature.RingSignatures {
signatures[i] = UnmarshalRingMLSAG(s)
}
commitments := make([]string, len(signature.PseudoOutputCommitments))
for i, c := range signature.PseudoOutputCommitments {
commitments[i] = hex.EncodeToString(c.GetData())
}
return &SignatureRctBulletproofs{
RingSignatures: signatures,
PseudoOutputCommitments: commitments,
RangeProofs: hex.EncodeToString(signature.RangeProofBytes),
}
}
func UnmarshalRingMLSAG(mlsag *types.RingMLSAG) *RingMLSAG {
responses := make([]string, len(mlsag.Responses))
for i, resp := range mlsag.Responses {
responses[i] = hex.EncodeToString(resp.GetData())
}
return &RingMLSAG{
CZero: hex.EncodeToString(mlsag.CZero.GetData()),
Responses: responses,
KeyImage: hex.EncodeToString(mlsag.KeyImage.GetData()),
}
}