mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 20:54:20 +01:00
Update client sdk (#207)
* Add bitcoin networks * Refactor client * Refactor explorer * Refactor store * Refactor wallet * Refactor sdk client * Refactor wasm & Update examples * Move common util funcs to internal/utils * Move to constants for service types * Add unit tests * Parallelize tests * Lint * Add job to gh action * go mod tidy * Fixes * Fixes * Fix compose file * Fixes * Fixes after review: * Drop factory pattern * Drop password from ark client methods * Make singlekey wallet manage store and wallet store instead of defining WalletStore as extension of Store * Move constants to arksdk module * Drop config and expect directory store and wallet as ark client factory args * Fix * Add constants for bitcoin/liquid explorer * Fix test * Fix wasm * Rename client.Client to client.ASPClient * Rename store.Store to store.ConfigStore * Rename wallet.Wallet to wallet.WalletService * Renamings * Lint * Fixes * Move everything to internal/utils & move ComputeVtxoTaprootScript to common * Go mod tidy
This commit is contained in:
committed by
GitHub
parent
e45bff3c70
commit
89df461623
@@ -5,6 +5,8 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/ark-network/ark-sdk/client"
|
||||
"github.com/ark-network/ark-sdk/internal/utils"
|
||||
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
||||
"github.com/ark-network/ark/common"
|
||||
"github.com/ark-network/ark/common/tree"
|
||||
@@ -17,11 +19,9 @@ import (
|
||||
|
||||
func (a *arkClient) handleRoundStream(
|
||||
ctx context.Context,
|
||||
paymentID string,
|
||||
vtxosToSign []vtxo,
|
||||
receivers []*arkv1.Output,
|
||||
paymentID string, vtxosToSign []*client.Vtxo, receivers []*arkv1.Output,
|
||||
) (string, error) {
|
||||
eventStream, err := a.innerClient.getEventStream(ctx, paymentID, &arkv1.GetEventStreamRequest{})
|
||||
eventsCh, err := a.client.GetEventStream(ctx, paymentID, &arkv1.GetEventStreamRequest{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -40,7 +40,12 @@ func (a *arkClient) handleRoundStream(
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
case event := <-eventStream.eventResp:
|
||||
case notify := <-eventsCh:
|
||||
if notify.Err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
event := notify.Event
|
||||
if e := event.GetRoundFailed(); e != nil {
|
||||
pingStop()
|
||||
return "", fmt.Errorf("round failed: %s", e.GetReason())
|
||||
@@ -51,7 +56,7 @@ func (a *arkClient) handleRoundStream(
|
||||
log.Info("a round finalization started")
|
||||
|
||||
signedForfeitTxs, err := a.handleRoundFinalization(
|
||||
e, vtxosToSign, receivers,
|
||||
ctx, e, vtxosToSign, receivers,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -63,7 +68,7 @@ func (a *arkClient) handleRoundStream(
|
||||
}
|
||||
|
||||
log.Info("finalizing payment... ")
|
||||
_, err = a.innerClient.finalizePayment(ctx, &arkv1.FinalizePaymentRequest{
|
||||
_, err = a.client.FinalizePayment(ctx, &arkv1.FinalizePaymentRequest{
|
||||
SignedForfeitTxs: signedForfeitTxs,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -77,15 +82,14 @@ func (a *arkClient) handleRoundStream(
|
||||
if event.GetRoundFinalized() != nil {
|
||||
return event.GetRoundFinalized().GetPoolTxid(), nil
|
||||
}
|
||||
case e := <-eventStream.err:
|
||||
return "", e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *arkClient) handleRoundFinalization(
|
||||
ctx context.Context,
|
||||
finalization *arkv1.RoundFinalizationEvent,
|
||||
vtxosToSign []vtxo,
|
||||
vtxosToSign []*client.Vtxo,
|
||||
receivers []*arkv1.Output,
|
||||
) ([]string, error) {
|
||||
if err := a.validateCongestionTree(finalization, receivers); err != nil {
|
||||
@@ -93,7 +97,7 @@ func (a *arkClient) handleRoundFinalization(
|
||||
}
|
||||
|
||||
return a.loopAndSign(
|
||||
finalization.GetForfeitTxs(), vtxosToSign, finalization.GetConnectors(),
|
||||
ctx, finalization.GetForfeitTxs(), vtxosToSign, finalization.GetConnectors(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -107,21 +111,18 @@ func (a *arkClient) validateCongestionTree(
|
||||
return err
|
||||
}
|
||||
|
||||
congestionTree, err := toCongestionTree(finalization.GetCongestionTree())
|
||||
congestionTree, err := utils.ToCongestionTree(
|
||||
finalization.GetCongestionTree(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
connectors := finalization.GetConnectors()
|
||||
|
||||
aspPubkey, err := secp256k1.ParsePubKey(a.aspPubKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !isOnchainOnly(receivers) {
|
||||
if !utils.IsOnchainOnly(receivers) {
|
||||
if err := tree.ValidateCongestionTree(
|
||||
congestionTree, poolTx, aspPubkey, int64(a.roundLifeTime),
|
||||
congestionTree, poolTx, a.StoreData.AspPubkey, a.RoundLifetime,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -131,7 +132,9 @@ func (a *arkClient) validateCongestionTree(
|
||||
return err
|
||||
}
|
||||
|
||||
if err := a.validateReceivers(ptx, receivers, &congestionTree, aspPubkey); err != nil {
|
||||
if err := a.validateReceivers(
|
||||
ptx, receivers, &congestionTree, a.StoreData.AspPubkey,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -147,7 +150,9 @@ func (a *arkClient) validateReceivers(
|
||||
aspPubkey *secp256k1.PublicKey,
|
||||
) error {
|
||||
for _, receiver := range receivers {
|
||||
isOnChain, onchainScript, userPubkey, err := decodeReceiverAddress(receiver.Address)
|
||||
isOnChain, onchainScript, userPubkey, err := utils.DecodeReceiverAddress(
|
||||
receiver.Address,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -157,7 +162,9 @@ func (a *arkClient) validateReceivers(
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := a.validateOffChainReceiver(congestionTree, receiver, userPubkey, aspPubkey); err != nil {
|
||||
if err := a.validateOffChainReceiver(
|
||||
congestionTree, receiver, userPubkey, aspPubkey,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -195,8 +202,9 @@ func (a *arkClient) validateOffChainReceiver(
|
||||
userPubkey, aspPubkey *secp256k1.PublicKey,
|
||||
) error {
|
||||
found := false
|
||||
outputTapKey, _, err := computeVtxoTaprootScript(
|
||||
userPubkey, aspPubkey, uint(a.unilateralExitDelay),
|
||||
net := a.explorer.GetNetwork()
|
||||
outputTapKey, _, _, _, err := tree.ComputeVtxoTaprootScript(
|
||||
userPubkey, aspPubkey, uint(a.UnilateralExitDelay), net,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -233,7 +241,8 @@ func (a *arkClient) validateOffChainReceiver(
|
||||
}
|
||||
|
||||
func (a *arkClient) loopAndSign(
|
||||
forfeitTxs []string, vtxosToSign []vtxo, connectors []string,
|
||||
ctx context.Context,
|
||||
forfeitTxs []string, vtxosToSign []*client.Vtxo, connectors []string,
|
||||
) ([]string, error) {
|
||||
signedForfeits := make([]string, 0)
|
||||
|
||||
@@ -254,8 +263,8 @@ func (a *arkClient) loopAndSign(
|
||||
for _, input := range pset.Inputs {
|
||||
inputTxid := chainhash.Hash(input.PreviousTxid).String()
|
||||
for _, coin := range vtxosToSign {
|
||||
if inputTxid == coin.txid {
|
||||
signedPset, err := a.signForfeitTx(forfeitTx, pset, connectorsTxids)
|
||||
if inputTxid == coin.Txid {
|
||||
signedPset, err := a.signForfeitTx(ctx, forfeitTx, pset, connectorsTxids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -269,7 +278,7 @@ func (a *arkClient) loopAndSign(
|
||||
}
|
||||
|
||||
func (a *arkClient) signForfeitTx(
|
||||
txStr string, tx *psetv2.Pset, connectorsTxids []string,
|
||||
ctx context.Context, txStr string, tx *psetv2.Pset, connectorsTxids []string,
|
||||
) (string, error) {
|
||||
connectorTxid := chainhash.Hash(tx.Inputs[0].PreviousTxid).String()
|
||||
connectorFound := false
|
||||
@@ -283,5 +292,5 @@ func (a *arkClient) signForfeitTx(
|
||||
return "", fmt.Errorf("connector txid %s not found in the connectors list", connectorTxid)
|
||||
}
|
||||
|
||||
return a.wallet.SignTransaction(a.explorerSvc, txStr)
|
||||
return a.wallet.SignTransaction(ctx, a.explorer, txStr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user