-
Notifications
You must be signed in to change notification settings - Fork 496
/
configureissuer.go
168 lines (147 loc) Β· 4.96 KB
/
configureissuer.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
package configureissuer
import (
"net/http"
"net/url"
"time"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/log"
"github.com/stellar/go/txnbuild"
)
type Options struct {
AssetCode string
BaseURL string
HorizonURL string
IssuerAccountSecret string
NetworkPassphrase string
}
func Setup(opts Options) {
hClient := &horizonclient.Client{
HorizonURL: opts.HorizonURL,
HTTP: &http.Client{Timeout: 30 * time.Second},
}
if opts.HorizonURL == horizonclient.DefaultTestNetClient.HorizonURL && opts.NetworkPassphrase == network.TestNetworkPassphrase {
hClient = horizonclient.DefaultTestNetClient
}
issuerKP := keypair.MustParse(opts.IssuerAccountSecret)
err := setup(opts, hClient)
if err != nil {
log.Error(errors.Wrap(err, "setting up issuer account"))
log.Fatal("Couldn't complete setup!")
}
log.Infof("πππ Successfully configured asset issuer for %s:%s", opts.AssetCode, issuerKP.Address())
}
func setup(opts Options, hClient horizonclient.ClientInterface) error {
issuerKP, err := keypair.ParseFull(opts.IssuerAccountSecret)
if err != nil {
log.Fatal(errors.Wrap(err, "parsing secret"))
}
issuerAcc, err := getOrFundIssuerAccount(issuerKP.Address(), hClient)
if err != nil {
return errors.Wrap(err, "getting or funding issuer account")
}
asset := txnbuild.CreditAsset{
Code: opts.AssetCode,
Issuer: issuerKP.Address(),
}
assetResults, err := hClient.Assets(horizonclient.AssetRequest{
ForAssetCode: asset.Code,
ForAssetIssuer: asset.Issuer,
Limit: 1,
})
if err != nil {
return errors.Wrap(err, "getting list of assets")
}
u, err := url.Parse(opts.BaseURL)
if err != nil {
return errors.Wrap(err, "parsing base url")
}
homeDomain := u.Hostname()
if issuerAcc.Flags.AuthRequired && issuerAcc.Flags.AuthRevocable && issuerAcc.HomeDomain == homeDomain && len(assetResults.Embedded.Records) > 0 {
log.Warn("Account already configured. Aborting without performing any action.")
return nil
}
trustorKP, err := keypair.Random()
if err != nil {
return errors.Wrap(err, "generating keypair")
}
tx, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: issuerAcc,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
&txnbuild.SetOptions{
SetFlags: []txnbuild.AccountFlag{
txnbuild.AuthRequired,
txnbuild.AuthRevocable,
},
HomeDomain: &homeDomain,
},
&txnbuild.BeginSponsoringFutureReserves{
SponsoredID: trustorKP.Address(),
SourceAccount: issuerKP.Address(),
},
&txnbuild.CreateAccount{
Destination: trustorKP.Address(),
Amount: "0",
SourceAccount: asset.Issuer,
},
// a trustline is generated to the desired so horizon creates entry at `{horizon-url}/assets`. This was added as many Wallets reach that endpoint to check if a given asset exists.
&txnbuild.ChangeTrust{
Line: asset.MustToChangeTrustAsset(),
SourceAccount: trustorKP.Address(),
},
&txnbuild.SetOptions{
MasterWeight: txnbuild.NewThreshold(0),
LowThreshold: txnbuild.NewThreshold(1),
MediumThreshold: txnbuild.NewThreshold(1),
HighThreshold: txnbuild.NewThreshold(1),
Signer: &txnbuild.Signer{Address: issuerKP.Address(), Weight: txnbuild.Threshold(10)},
SourceAccount: trustorKP.Address(),
},
&txnbuild.EndSponsoringFutureReserves{
SourceAccount: trustorKP.Address(),
},
},
BaseFee: 300,
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(300)},
})
if err != nil {
return errors.Wrap(err, "building transaction")
}
tx, err = tx.Sign(opts.NetworkPassphrase, issuerKP, trustorKP)
if err != nil {
return errors.Wrap(err, "signing transaction")
}
_, err = hClient.SubmitTransaction(tx)
if err != nil {
return errors.Wrap(err, "submitting transaction")
}
return nil
}
func getOrFundIssuerAccount(issuerAddress string, hClient horizonclient.ClientInterface) (*horizon.Account, error) {
issuerAcc, err := hClient.AccountDetail(horizonclient.AccountRequest{
AccountID: issuerAddress,
})
if err != nil {
if !horizonclient.IsNotFoundError(err) || hClient != horizonclient.DefaultTestNetClient {
return nil, errors.Wrapf(err, "getting detail for account %s", issuerAddress)
}
log.Info("Issuer account not found π on network, will fund it using friendbot.")
_, err = hClient.Fund(issuerAddress)
if err != nil {
return nil, errors.Wrap(err, "funding account with friendbot")
}
log.Info("π Successfully funded account using friendbot.")
}
// now the account should be funded by the friendbot already
issuerAcc, err = hClient.AccountDetail(horizonclient.AccountRequest{
AccountID: issuerAddress,
})
if err != nil {
return nil, errors.Wrapf(err, "getting detail for account %s", issuerAddress)
}
return &issuerAcc, nil
}