-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
144 lines (122 loc) · 4.06 KB
/
client.go
File metadata and controls
144 lines (122 loc) · 4.06 KB
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
// Copyright (c) 2026 Mintlayer Institutional FZCO
// Contact: hello@mintlayer.org
//
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package sdk
import (
"context"
"sync"
"github.com/mintlayer/go-sdk/indexer"
"github.com/mintlayer/go-sdk/node"
mintlayer "github.com/mintlayer/go-sdk/wasm"
"github.com/mintlayer/go-sdk/wallet"
)
// ── Convenience re-exports ────────────────────────────────────────────────────
//
// Callers that only need the top-level client can import "github.com/mintlayer/go-sdk"
// and use these aliases without also importing the sub-packages.
type (
Amount = mintlayer.Amount
Network = mintlayer.Network
SignatureHashType = mintlayer.SignatureHashType
SourceId = mintlayer.SourceId
TotalSupply = mintlayer.TotalSupply
FreezableToken = mintlayer.FreezableToken
TokenUnfreezable = mintlayer.TokenUnfreezable
TxAdditionalInfo = mintlayer.TxAdditionalInfo
PoolInfo = mintlayer.PoolInfo
OrderInfo = mintlayer.OrderInfo
)
// Network constants.
const (
Mainnet = mintlayer.Mainnet
Testnet = mintlayer.Testnet
Regtest = mintlayer.Regtest
Signet = mintlayer.Signet
)
// Amount constructors.
var (
NewAmount = mintlayer.NewAmount
NewAmountZero = mintlayer.NewAmountZero
)
// ── Config ────────────────────────────────────────────────────────────────────
// Config holds the top-level SDK configuration.
// Only sub-clients whose URL field is non-empty are constructed.
type Config struct {
// NodeURL is the base URL of the Mintlayer node daemon
// (e.g. "http://127.0.0.1:3030"). Leave empty to skip.
NodeURL string
// IndexerURL is the base URL of the indexer (api-web-server)
// (e.g. "http://127.0.0.1:3000"). Leave empty to skip.
IndexerURL string
// WalletURL is the base URL of the wallet RPC daemon
// (e.g. "http://127.0.0.1:3034"). Leave empty to skip.
WalletURL string
// Username and Password are used for HTTP Basic Auth on Node and Wallet.
Username string
Password string
}
// ── Client ────────────────────────────────────────────────────────────────────
// Client is the top-level Mintlayer SDK client.
//
// Each sub-client field is nil when its corresponding URL was not set in Config.
// WASM is nil until [Client.InitWASM] is called.
type Client struct {
Node *node.Client
Indexer *indexer.Client
Wallet *wallet.Client
WASM *mintlayer.Client // nil until InitWASM
mu sync.Mutex
wctx context.Context
}
// New creates a Client, constructing only the sub-clients whose URL is set in cfg.
func New(cfg Config) *Client {
c := &Client{}
if cfg.NodeURL != "" {
var opts []node.Option
if cfg.Username != "" {
opts = append(opts, node.WithBasicAuth(cfg.Username, cfg.Password))
}
c.Node = node.New(cfg.NodeURL, opts...)
}
if cfg.IndexerURL != "" {
c.Indexer = indexer.New(cfg.IndexerURL)
}
if cfg.WalletURL != "" {
var opts []wallet.Option
if cfg.Username != "" {
opts = append(opts, wallet.WithBasicAuth(cfg.Username, cfg.Password))
}
c.Wallet = wallet.New(cfg.WalletURL, opts...)
}
return c
}
// InitWASM initialises the embedded WASM cryptography runtime (~400ms).
// Subsequent calls are no-ops. Safe for concurrent use.
func (c *Client) InitWASM(ctx context.Context) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.WASM != nil {
return nil
}
wc, err := mintlayer.New(ctx)
if err != nil {
return err
}
c.WASM = wc
c.wctx = ctx
return nil
}
// Close releases WASM resources if [Client.InitWASM] was called.
// HTTP sub-clients (Node, Indexer, Wallet) require no teardown.
func (c *Client) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.WASM == nil {
return nil
}
err := c.WASM.Close()
c.WASM = nil
return err
}