-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·110 lines (89 loc) · 2.59 KB
/
main.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
package shared
import (
"bytes"
"github.com/paydex-core/paydex-go/hash"
"github.com/paydex-core/paydex-go/keypair"
"github.com/paydex-core/paydex-go/support/errors"
"github.com/paydex-core/paydex-go/txnbuild"
"github.com/paydex-core/paydex-go/xdr"
)
// BuildTransaction is used in compliance server. The sequence number in built transaction will be equal 0!
func BuildTransaction(accountID, networkPassphrase string, operation []txnbuild.Operation, memo txnbuild.Memo) (string, error) {
// Sequence number is set to -1 here because txnbuild auto increments by 1.
tx := txnbuild.Transaction{
SourceAccount: &txnbuild.SimpleAccount{AccountID: accountID, Sequence: int64(-1)},
Operations: operation,
Timebounds: txnbuild.NewInfiniteTimeout(),
Network: networkPassphrase,
Memo: memo,
}
err := tx.Build()
if err != nil {
return "", errors.Wrap(err, "unable to build transaction")
}
err = tx.Sign()
if err != nil {
return "", errors.Wrap(err, "unable to build transaction")
}
txeB64, err := tx.Base64()
if err != nil {
return "", errors.Wrap(err, "unable to encode transaction envelope")
}
var txXDR xdr.TransactionEnvelope
err = xdr.SafeUnmarshalBase64(txeB64, &txXDR)
if err != nil {
return "", errors.Wrap(err, "unable to decode transaction envelope")
}
txB64, err := xdr.MarshalBase64(txXDR.Tx)
if err != nil {
return "", errors.Wrap(err, "unable to encode transaction")
}
return txB64, err
}
// TransactionHash returns transaction hash for a given Transaction based on the network
func TransactionHash(tx *xdr.Transaction, networkPassphrase string) ([32]byte, error) {
var txBytes bytes.Buffer
h := hash.Hash([]byte(networkPassphrase))
_, err := txBytes.Write(h[:])
if err != nil {
return [32]byte{}, err
}
_, err = xdr.Marshal(&txBytes, xdr.EnvelopeTypeEnvelopeTypeTx)
if err != nil {
return [32]byte{}, err
}
_, err = xdr.Marshal(&txBytes, tx)
if err != nil {
return [32]byte{}, err
}
return hash.Hash(txBytes.Bytes()), nil
}
// IsValidAccountID returns true if account ID is valid
func IsValidAccountID(accountID string) bool {
_, err := keypair.Parse(accountID)
if err != nil {
return false
}
if accountID[0] != 'G' {
return false
}
return true
}
// IsValidSecret returns true if secret is valid
func IsValidSecret(secret string) bool {
_, err := keypair.Parse(secret)
if err != nil {
return false
}
if secret[0] != 'S' {
return false
}
return true
}
// IsValidAssetCode returns true if asset code is valid
func IsValidAssetCode(code string) bool {
if len(code) < 1 || len(code) > 12 {
return false
}
return true
}