mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 12:14:21 +01:00
* SDK - GetTransactionsStream add to rest/grpc no session, pattern or user messages provided * SDK - AppDataRepository added, store refactor This patch introduces a significant refactor of the Ark SDK codebase. The primary changes involve: 1. **Domain Package Introduction**: A new `domain` package has been created under `store`, encapsulating core data structures and interfaces such as `ConfigData`, `Transaction`, `SdkRepository`, and various repository interfaces. This enhances modularity and separation of concerns. 2. **Replacement of Store with Domain Objects**: All references to the `store` package's `StoreData` have been replaced with `domain.ConfigData`. This change reflects a shift towards using domain-driven design, where the core business logic is represented by domain objects. 3. **Repository Pattern Implementation**: The code has been refactored to use repositories for accessing and managing data. `ConfigRepository` and `TransactionRepository` interfaces are introduced, providing methods for managing configuration data and transactions, respectively. 4. **New Storage Implementations**: New storage implementations using BadgerDB are introduced for persisting transactions (`transaction_repository.go`). This change indicates a move towards more robust and scalable data storage solutions. 5. **Service Layer Introduction**: A service layer (`service.go`) is introduced, which acts as a facade for the underlying data repositories. This layer abstracts the data access logic and provides a unified interface for interacting with configuration and transaction data. 6. **Code Simplification and Cleanup**: The patch removes redundant code and simplifies the existing codebase by consolidating store-related logic and enhancing the overall structure of the code. 7. **Testing Enhancements**: Test cases are updated to reflect the changes in the data access layer, ensuring that the new repository pattern and domain objects are properly tested. Overall, this refactor aims to improve the maintainability, scalability, and testability of the codebase by adopting best practices such as domain-driven design, repository pattern, and separation of concerns. * 'SDK listen for tx stream This diff represents a substantial refactor and enhancement of the `ark-sdk` client, which involves modifications to multiple files, primarily in the `pkg/client-sdk` and `server/internal/core/application` directories. Here's a summary of the key changes: 1. **Configuration and Initialization**: - Refactor to replace file-based configuration store with a domain-based approach. - Introduced `ListenTransactionStream` configuration to allow real-time transaction event listening. 2. **Client SDK Enhancements**: - The client SDK now supports asynchronous transaction streaming and handling. - Introduced methods to get transaction event channels and stop the client gracefully. - Expanded the `ArkClient` interface to include these new functionalities. 3. **Transaction Handling**: - Added support for listening to and processing transaction events in real-time. - Implemented methods to handle different types of transactions, including boarding, redeem, and round transactions. - Expanded the `Transaction` struct to include more metadata, such as `BoardingVOut`. 4. **Repository Changes**: - Introduced `VtxoRepository` for managing VTXO-related operations. - Modified `AppDataRepository` to include `VtxoRepository`. 5. **Example and Test Adjustments**: - Updated example code to utilize the new transaction streaming capabilities. - Adjusted tests to reflect changes in client initialization and configuration handling. 6. **Dependency Updates**: - Updated `go.mod` and `go.sum` to include new dependencies such as `badger` for data storage and potentially updated versions of existing packages. These changes collectively enhance the functionality of the `ark-sdk` client by enabling real-time transaction handling, improving configuration management, and providing a more robust and scalable client architecture. This update seems to be aimed at improving the efficiency and responsiveness of the client application, likely in response to increased demands for real-time data processing and analysis.' * merge * go work sync * test fix * Tiny refactor (#2) * tiny refactor * renaming * Renamings * Fixes (#3) --------- Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
152 lines
4.7 KiB
Go
152 lines
4.7 KiB
Go
package wallet_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ark-network/ark/common"
|
|
"github.com/ark-network/ark/pkg/client-sdk/client"
|
|
inmemorystore "github.com/ark-network/ark/pkg/client-sdk/store/inmemory"
|
|
sdktypes "github.com/ark-network/ark/pkg/client-sdk/types"
|
|
"github.com/ark-network/ark/pkg/client-sdk/wallet"
|
|
singlekeywallet "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey"
|
|
inmemorywalletstore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store/inmemory"
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWallet(t *testing.T) {
|
|
ctx := context.Background()
|
|
key, _ := btcec.NewPrivateKey()
|
|
password := "password"
|
|
testStoreData := sdktypes.Config{
|
|
AspUrl: "localhost:7070",
|
|
AspPubkey: key.PubKey(),
|
|
WalletType: wallet.SingleKeyWallet,
|
|
ClientType: client.GrpcClient,
|
|
Network: common.LiquidRegTest,
|
|
RoundLifetime: 512,
|
|
RoundInterval: 10,
|
|
UnilateralExitDelay: 512,
|
|
Dust: 1000,
|
|
BoardingDescriptorTemplate: "tr(0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0,{ and(pk(873079a0091c9b16abd1f8c508320b07f0d50144d09ccd792ce9c915dac60465), pk(USER)), and(older(604672), pk(USER)) })",
|
|
ForfeitAddress: "bcrt1qzvqj",
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
chain string
|
|
args []interface{}
|
|
}{
|
|
{
|
|
name: "liquid" + wallet.SingleKeyWallet,
|
|
chain: "liquid",
|
|
args: []interface{}{common.LiquidRegTest},
|
|
},
|
|
{
|
|
name: "bitcoin" + wallet.SingleKeyWallet,
|
|
chain: "bitcoin",
|
|
args: []interface{}{common.LiquidRegTest},
|
|
},
|
|
}
|
|
|
|
for i := range tests {
|
|
tt := tests[i]
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
store, err := inmemorystore.NewConfigStore()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, store)
|
|
|
|
err = store.AddData(ctx, testStoreData)
|
|
require.NoError(t, err)
|
|
|
|
walletStore, err := inmemorywalletstore.NewWalletStore()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, walletStore)
|
|
|
|
var walletSvc wallet.WalletService
|
|
if tt.chain == "liquid" {
|
|
walletSvc, err = singlekeywallet.NewLiquidWallet(store, walletStore)
|
|
} else {
|
|
walletSvc, err = singlekeywallet.NewBitcoinWallet(store, walletStore)
|
|
}
|
|
require.NoError(t, err)
|
|
require.NotNil(t, walletSvc)
|
|
|
|
key, err := walletSvc.Create(ctx, password, "")
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, key)
|
|
|
|
offchainAddr, onchainAddr, err := walletSvc.NewAddress(ctx, false)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, offchainAddr)
|
|
require.NotEmpty(t, onchainAddr)
|
|
|
|
offchainAddrs, onchainAddrs, redemptionAddrs, err := walletSvc.GetAddresses(ctx)
|
|
require.NoError(t, err)
|
|
require.Len(t, offchainAddrs, 1)
|
|
require.Len(t, onchainAddrs, 1)
|
|
require.Len(t, redemptionAddrs, 1)
|
|
|
|
offchainAddr, onchainAddr, err = walletSvc.NewAddress(ctx, true)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, offchainAddr)
|
|
require.NotEmpty(t, onchainAddr)
|
|
|
|
expectedNumOfAddresses := 2
|
|
if strings.Contains(tt.name, wallet.SingleKeyWallet) {
|
|
expectedNumOfAddresses = 1
|
|
}
|
|
|
|
offchainAddrs, onchainAddrs, redemptionAddrs, err = walletSvc.GetAddresses(ctx)
|
|
require.NoError(t, err)
|
|
require.Len(t, offchainAddrs, expectedNumOfAddresses)
|
|
require.Len(t, onchainAddrs, expectedNumOfAddresses)
|
|
require.Len(t, redemptionAddrs, expectedNumOfAddresses)
|
|
|
|
num := 3
|
|
offchainAddrs, onchainAddrs, err = walletSvc.NewAddresses(ctx, false, num)
|
|
require.NoError(t, err)
|
|
require.Len(t, offchainAddrs, num)
|
|
require.Len(t, onchainAddrs, num)
|
|
|
|
expectedNumOfAddresses += num
|
|
if strings.Contains(tt.name, wallet.SingleKeyWallet) {
|
|
expectedNumOfAddresses = 1
|
|
}
|
|
offchainAddrs, onchainAddrs, redemptionAddrs, err = walletSvc.GetAddresses(ctx)
|
|
require.NoError(t, err)
|
|
require.Len(t, offchainAddrs, expectedNumOfAddresses)
|
|
require.Len(t, onchainAddrs, expectedNumOfAddresses)
|
|
require.Len(t, redemptionAddrs, expectedNumOfAddresses)
|
|
|
|
// Check no password is required to unlock if wallet is already unlocked.
|
|
alreadyUnlocked, err := walletSvc.Unlock(ctx, password)
|
|
require.NoError(t, err)
|
|
require.False(t, alreadyUnlocked)
|
|
|
|
alreadyUnlocked, err = walletSvc.Unlock(ctx, "")
|
|
require.NoError(t, err)
|
|
require.True(t, alreadyUnlocked)
|
|
|
|
// Check no password is required to lock if wallet is already locked.
|
|
err = walletSvc.Lock(ctx, password)
|
|
require.NoError(t, err)
|
|
|
|
err = walletSvc.Lock(ctx, "")
|
|
require.NoError(t, err)
|
|
|
|
locked := walletSvc.IsLocked()
|
|
require.True(t, locked)
|
|
|
|
_, err = walletSvc.Unlock(ctx, password)
|
|
require.NoError(t, err)
|
|
|
|
locked = walletSvc.IsLocked()
|
|
require.False(t, locked)
|
|
})
|
|
}
|
|
}
|