Skip to content

Commit eccb2ef

Browse files
authored
Tiny refactor (ark-network#2)
* tiny refactor * renaming * Renamings
1 parent 9fd01cf commit eccb2ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1333
-1435
lines changed

client/main.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/ark-network/ark/common"
1414
arksdk "github.com/ark-network/ark/pkg/client-sdk"
1515
"github.com/ark-network/ark/pkg/client-sdk/store"
16-
"github.com/ark-network/ark/pkg/client-sdk/store/domain"
16+
"github.com/ark-network/ark/pkg/client-sdk/types"
1717
"github.com/btcsuite/btcd/btcutil/psbt"
1818
"github.com/urfave/cli/v2"
1919
"golang.org/x/term"
@@ -230,7 +230,7 @@ func config(ctx *cli.Context) error {
230230
"boarding_descriptor_template": cfgData.BoardingDescriptorTemplate,
231231
"explorer_url": cfgData.ExplorerURL,
232232
"forfeit_address": cfgData.ForfeitAddress,
233-
"listen_transaction_stream": cfgData.ListenTransactionStream,
233+
"listen_transaction_stream": cfgData.WithTransactionFeed,
234234
}
235235

236236
return printJSON(cfg)
@@ -375,16 +375,16 @@ func redeem(ctx *cli.Context) error {
375375

376376
func getArkSdkClient(ctx *cli.Context) (arksdk.ArkClient, error) {
377377
dataDir := ctx.String(datadirFlag.Name)
378-
sdkRepository, err := store.NewService(store.Config{
379-
ConfigStoreType: store.FileStore,
380-
AppDataStoreType: store.Badger,
378+
sdkRepository, err := store.NewStore(store.Config{
379+
ConfigStoreType: types.FileStore,
380+
AppDataStoreType: types.KVStore,
381381
BaseDir: dataDir,
382382
})
383383
if err != nil {
384384
return nil, err
385385
}
386386

387-
cfgData, err := sdkRepository.ConfigRepository().GetData(context.Background())
387+
cfgData, err := sdkRepository.ConfigStore().GetData(context.Background())
388388
if err != nil {
389389
return nil, err
390390
}
@@ -410,8 +410,8 @@ func getArkSdkClient(ctx *cli.Context) (arksdk.ArkClient, error) {
410410
}
411411

412412
func loadOrCreateClient(
413-
loadFunc, newFunc func(domain.SdkRepository) (arksdk.ArkClient, error),
414-
sdkRepository domain.SdkRepository,
413+
loadFunc, newFunc func(types.Store) (arksdk.ArkClient, error),
414+
sdkRepository types.Store,
415415
) (arksdk.ArkClient, error) {
416416
client, err := loadFunc(sdkRepository)
417417
if err != nil {
@@ -423,7 +423,7 @@ func loadOrCreateClient(
423423
return client, err
424424
}
425425

426-
func getNetwork(ctx *cli.Context, cfgData *domain.ConfigData) (string, error) {
426+
func getNetwork(ctx *cli.Context, cfgData *types.Config) (string, error) {
427427
if cfgData == nil {
428428
return ctx.String(networkFlag.Name), nil
429429
}

go.work.sum

+146
Large diffs are not rendered by default.

pkg/client-sdk/ark_sdk.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55

66
"github.com/ark-network/ark/pkg/client-sdk/client"
7-
"github.com/ark-network/ark/pkg/client-sdk/store/domain"
7+
"github.com/ark-network/ark/pkg/client-sdk/types"
88
)
99

1010
type ArkClient interface {
11-
GetConfigData(ctx context.Context) (*domain.ConfigData, error)
11+
GetConfigData(ctx context.Context) (*types.Config, error)
1212
Init(ctx context.Context, args InitArgs) error
1313
InitWithWallet(ctx context.Context, args InitWithWalletArgs) error
1414
IsLocked(ctx context.Context) bool
@@ -28,8 +28,8 @@ type ArkClient interface {
2828
Claim(ctx context.Context) (string, error)
2929
ListVtxos(ctx context.Context) (spendable, spent []client.Vtxo, err error)
3030
Dump(ctx context.Context) (seed string, err error)
31-
GetTransactionHistory(ctx context.Context) ([]domain.Transaction, error)
32-
GetTransactionEventChannel() chan domain.TransactionEvent
31+
GetTransactionHistory(ctx context.Context) ([]types.Transaction, error)
32+
GetTransactionEventChannel() chan types.TransactionEvent
3333
Stop() error
3434
}
3535

pkg/client-sdk/client.go

+72-73
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
"github.com/ark-network/ark/pkg/client-sdk/client"
1212
"github.com/ark-network/ark/pkg/client-sdk/explorer"
1313
"github.com/ark-network/ark/pkg/client-sdk/internal/utils"
14-
"github.com/ark-network/ark/pkg/client-sdk/store"
15-
"github.com/ark-network/ark/pkg/client-sdk/store/domain"
14+
"github.com/ark-network/ark/pkg/client-sdk/types"
1615
"github.com/ark-network/ark/pkg/client-sdk/wallet"
1716
singlekeywallet "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey"
1817
walletstore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store"
@@ -29,8 +28,8 @@ const (
2928
// wallet
3029
SingleKeyWallet = wallet.SingleKeyWallet
3130
// store
32-
FileStore = store.FileStore
33-
InMemoryStore = store.InMemoryStore
31+
FileStore = types.FileStore
32+
InMemoryStore = types.InMemoryStore
3433
// explorer
3534
BitcoinExplorer = explorer.BitcoinExplorer
3635
LiquidExplorer = explorer.LiquidExplorer
@@ -54,25 +53,65 @@ var (
5453
)
5554

5655
type arkClient struct {
57-
*domain.ConfigData
58-
wallet wallet.WalletService
59-
sdkRepository domain.SdkRepository
60-
explorer explorer.Explorer
61-
client client.ASPClient
56+
*types.Config
57+
wallet wallet.WalletService
58+
store types.Store
59+
explorer explorer.Explorer
60+
client client.ASPClient
6261

6362
txStreamCtxCancel context.CancelFunc
6463
}
6564

6665
func (a *arkClient) GetConfigData(
6766
_ context.Context,
68-
) (*domain.ConfigData, error) {
69-
if a.ConfigData == nil {
67+
) (*types.Config, error) {
68+
if a.Config == nil {
7069
return nil, fmt.Errorf("client sdk not initialized")
7170
}
72-
return a.ConfigData, nil
71+
return a.Config, nil
7372
}
7473

75-
func (a *arkClient) InitWithWallet(
74+
func (a *arkClient) Unlock(ctx context.Context, pasword string) error {
75+
_, err := a.wallet.Unlock(ctx, pasword)
76+
return err
77+
}
78+
79+
func (a *arkClient) Lock(ctx context.Context, pasword string) error {
80+
return a.wallet.Lock(ctx, pasword)
81+
}
82+
83+
func (a *arkClient) IsLocked(ctx context.Context) bool {
84+
return a.wallet.IsLocked()
85+
}
86+
87+
func (a *arkClient) Dump(ctx context.Context) (string, error) {
88+
return a.wallet.Dump(ctx)
89+
}
90+
91+
func (a *arkClient) Receive(ctx context.Context) (string, string, error) {
92+
offchainAddr, boardingAddr, err := a.wallet.NewAddress(ctx, false)
93+
if err != nil {
94+
return "", "", err
95+
}
96+
97+
return offchainAddr, boardingAddr, nil
98+
}
99+
100+
func (a *arkClient) GetTransactionEventChannel() chan types.TransactionEvent {
101+
return a.store.TransactionStore().GetEventChannel()
102+
}
103+
104+
func (a *arkClient) Stop() error {
105+
if a.Config.WithTransactionFeed {
106+
a.txStreamCtxCancel()
107+
}
108+
109+
a.store.Close()
110+
111+
return nil
112+
}
113+
114+
func (a *arkClient) initWithWallet(
76115
ctx context.Context, args InitWithWalletArgs,
77116
) error {
78117
if err := args.validate(); err != nil {
@@ -107,7 +146,7 @@ func (a *arkClient) InitWithWallet(
107146
return fmt.Errorf("failed to parse asp pubkey: %s", err)
108147
}
109148

110-
storeData := domain.ConfigData{
149+
storeData := types.Config{
111150
AspUrl: args.AspUrl,
112151
AspPubkey: aspPubkey,
113152
WalletType: args.Wallet.GetType(),
@@ -119,26 +158,27 @@ func (a *arkClient) InitWithWallet(
119158
Dust: info.Dust,
120159
BoardingDescriptorTemplate: info.BoardingDescriptorTemplate,
121160
ForfeitAddress: info.ForfeitAddress,
161+
WithTransactionFeed: args.ListenTransactionStream,
122162
}
123-
if err := a.sdkRepository.ConfigRepository().AddData(ctx, storeData); err != nil {
163+
if err := a.store.ConfigStore().AddData(ctx, storeData); err != nil {
124164
return err
125165
}
126166

127167
if _, err := args.Wallet.Create(ctx, args.Password, args.Seed); err != nil {
128168
//nolint:all
129-
a.sdkRepository.ConfigRepository().CleanData(ctx)
169+
a.store.ConfigStore().CleanData(ctx)
130170
return err
131171
}
132172

133-
a.ConfigData = &storeData
173+
a.Config = &storeData
134174
a.wallet = args.Wallet
135175
a.explorer = explorerSvc
136176
a.client = clientSvc
137177

138178
return nil
139179
}
140180

141-
func (a *arkClient) Init(
181+
func (a *arkClient) init(
142182
ctx context.Context, args InitArgs,
143183
) error {
144184
if err := args.validate(); err != nil {
@@ -173,7 +213,7 @@ func (a *arkClient) Init(
173213
return fmt.Errorf("failed to parse asp pubkey: %s", err)
174214
}
175215

176-
cfgData := domain.ConfigData{
216+
cfgData := types.Config{
177217
AspUrl: args.AspUrl,
178218
AspPubkey: aspPubkey,
179219
WalletType: args.WalletType,
@@ -186,72 +226,31 @@ func (a *arkClient) Init(
186226
BoardingDescriptorTemplate: info.BoardingDescriptorTemplate,
187227
ExplorerURL: args.ExplorerURL,
188228
ForfeitAddress: info.ForfeitAddress,
229+
WithTransactionFeed: args.ListenTransactionStream,
189230
}
190-
walletSvc, err := getWallet(a.sdkRepository.ConfigRepository(), &cfgData, supportedWallets)
231+
walletSvc, err := getWallet(a.store.ConfigStore(), &cfgData, supportedWallets)
191232
if err != nil {
192233
return err
193234
}
194235

195-
if err := a.sdkRepository.ConfigRepository().AddData(ctx, cfgData); err != nil {
236+
if err := a.store.ConfigStore().AddData(ctx, cfgData); err != nil {
196237
return err
197238
}
198239

199240
if _, err := walletSvc.Create(ctx, args.Password, args.Seed); err != nil {
200241
//nolint:all
201-
a.sdkRepository.ConfigRepository().CleanData(ctx)
242+
a.store.ConfigStore().CleanData(ctx)
202243
return err
203244
}
204245

205-
a.ConfigData = &cfgData
246+
a.Config = &cfgData
206247
a.wallet = walletSvc
207248
a.explorer = explorerSvc
208249
a.client = clientSvc
209250

210251
return nil
211252
}
212253

213-
func (a *arkClient) Unlock(ctx context.Context, pasword string) error {
214-
_, err := a.wallet.Unlock(ctx, pasword)
215-
return err
216-
}
217-
218-
func (a *arkClient) Lock(ctx context.Context, pasword string) error {
219-
return a.wallet.Lock(ctx, pasword)
220-
}
221-
222-
func (a *arkClient) IsLocked(ctx context.Context) bool {
223-
return a.wallet.IsLocked()
224-
}
225-
226-
func (a *arkClient) Dump(ctx context.Context) (string, error) {
227-
return a.wallet.Dump(ctx)
228-
}
229-
230-
func (a *arkClient) Receive(ctx context.Context) (string, string, error) {
231-
offchainAddr, boardingAddr, err := a.wallet.NewAddress(ctx, false)
232-
if err != nil {
233-
return "", "", err
234-
}
235-
236-
return offchainAddr, boardingAddr, nil
237-
}
238-
239-
func (a *arkClient) GetTransactionEventChannel() chan domain.TransactionEvent {
240-
return a.sdkRepository.AppDataRepository().TransactionRepository().GetEventChannel()
241-
}
242-
243-
func (a *arkClient) Stop() error {
244-
if a.ConfigData.ListenTransactionStream {
245-
a.txStreamCtxCancel()
246-
}
247-
248-
if err := a.sdkRepository.AppDataRepository().Stop(); err != nil {
249-
return err
250-
}
251-
252-
return nil
253-
}
254-
255254
func (a *arkClient) ping(
256255
ctx context.Context, paymentID string,
257256
) func() {
@@ -289,11 +288,11 @@ func getExplorer(explorerURL, network string) (explorer.Explorer, error) {
289288
}
290289

291290
func getWallet(
292-
configRepo domain.ConfigRepository, data *domain.ConfigData, supportedWallets utils.SupportedType[struct{}],
291+
configStore types.ConfigStore, data *types.Config, supportedWallets utils.SupportedType[struct{}],
293292
) (wallet.WalletService, error) {
294293
switch data.WalletType {
295294
case wallet.SingleKeyWallet:
296-
return getSingleKeyWallet(configRepo, data.Network.Name)
295+
return getSingleKeyWallet(configStore, data.Network.Name)
297296
default:
298297
return nil, fmt.Errorf(
299298
"unsuported wallet type '%s', please select one of: %s",
@@ -303,24 +302,24 @@ func getWallet(
303302
}
304303

305304
func getSingleKeyWallet(
306-
configRepo domain.ConfigRepository, network string,
305+
configStore types.ConfigStore, network string,
307306
) (wallet.WalletService, error) {
308-
walletStore, err := getWalletStore(configRepo.GetType(), configRepo.GetDatadir())
307+
walletStore, err := getWalletStore(configStore.GetType(), configStore.GetDatadir())
309308
if err != nil {
310309
return nil, err
311310
}
312311

313312
if strings.Contains(network, "liquid") {
314-
return singlekeywallet.NewLiquidWallet(configRepo, walletStore)
313+
return singlekeywallet.NewLiquidWallet(configStore, walletStore)
315314
}
316-
return singlekeywallet.NewBitcoinWallet(configRepo, walletStore)
315+
return singlekeywallet.NewBitcoinWallet(configStore, walletStore)
317316
}
318317

319318
func getWalletStore(storeType, datadir string) (walletstore.WalletStore, error) {
320319
switch storeType {
321-
case store.InMemoryStore:
320+
case types.InMemoryStore:
322321
return inmemorystore.NewWalletStore()
323-
case store.FileStore:
322+
case types.FileStore:
324323
return filestore.NewWalletStore(datadir)
325324
default:
326325
return nil, fmt.Errorf("unknown wallet store type")

0 commit comments

Comments
 (0)