forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_merge.go
80 lines (71 loc) · 2.32 KB
/
account_merge.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
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// AccountMerge represents the Stellar merge account operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html
type AccountMerge struct {
Destination string
SourceAccount string
}
// BuildXDR for AccountMerge returns a fully configured XDR Operation.
func (am *AccountMerge) BuildXDR(withMuxedAccounts bool) (xdr.Operation, error) {
var xdrOp xdr.MuxedAccount
var err error
if withMuxedAccounts {
err = xdrOp.SetAddress(am.Destination)
} else {
err = xdrOp.SetEd25519Address(am.Destination)
}
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set destination address")
}
opType := xdr.OperationTypeAccountMerge
body, err := xdr.NewOperationBody(opType, xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR OperationBody")
}
op := xdr.Operation{Body: body}
if withMuxedAccounts {
SetOpSourceMuxedAccount(&op, am.SourceAccount)
} else {
SetOpSourceAccount(&op, am.SourceAccount)
}
return op, nil
}
// FromXDR for AccountMerge initialises the txnbuild struct from the corresponding xdr Operation.
func (am *AccountMerge) FromXDR(xdrOp xdr.Operation, withMuxedAccounts bool) error {
if xdrOp.Body.Type != xdr.OperationTypeAccountMerge {
return errors.New("error parsing account_merge operation from xdr")
}
am.SourceAccount = accountFromXDR(xdrOp.SourceAccount, withMuxedAccounts)
if xdrOp.Body.Destination != nil {
if withMuxedAccounts {
am.Destination = xdrOp.Body.Destination.Address()
} else {
aid := xdrOp.Body.Destination.ToAccountId()
am.Destination = aid.Address()
}
}
return nil
}
// Validate for AccountMerge validates the required struct fields. It returns an error if any of the fields are
// invalid. Otherwise, it returns nil.
func (am *AccountMerge) Validate(withMuxedAccounts bool) error {
var err error
if withMuxedAccounts {
_, err = xdr.AddressToAccountId(am.Destination)
} else {
_, err = xdr.AddressToMuxedAccount(am.Destination)
}
if err != nil {
return NewValidationError("Destination", err.Error())
}
return nil
}
// GetSourceAccount returns the source account of the operation, or the empty string if not
// set.
func (am *AccountMerge) GetSourceAccount() string {
return am.SourceAccount
}