Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Co-authored-by: Louis Singer <[email protected]>
  • Loading branch information
altafan and louisinger committed Sep 19, 2024
1 parent 0096767 commit c0be278
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
44 changes: 27 additions & 17 deletions pkg/client-sdk/covenantless_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,20 @@ func (a *covenantlessArkClient) ListVtxos(
return
}

_, pubkey, _, err := common.DecodeAddress(offchainAddrs[0])
if err != nil {
return
}

myPubkey := schnorr.SerializePubKey(pubkey)

// The ASP returns the vtxos sent to others via async payments as spendable
// because they are actually revertable. Since we do not provide any revert
// feature, we want to ignore them.
// To understand if the output of a redeem tx is sent or received we look at
// the inputs and check if they are owned by us.
// The auxiliary variables below are used to make these checks in an
// efficient way.
indexedSpentVtxos := make(map[string]client.Vtxo)
pendingVtxos := make([]client.Vtxo, 0)
keyTempl := "%s:%d"
for _, addr := range offchainAddrs {
spendable, spent, err := a.client.ListVtxos(ctx, addr)
if err != nil {
Expand All @@ -167,23 +171,29 @@ func (a *covenantlessArkClient) ListVtxos(
spendableVtxos = append(spendableVtxos, v)
continue
}
pendingVtxos = append(pendingVtxos, v)
script, err := bitcointree.ParseVtxoScript(v.Descriptor)
if err != nil {
return nil, nil, err
}

if reversibleVtxo, ok := script.(*bitcointree.ReversibleVtxoScript); ok {
if !bytes.Equal(schnorr.SerializePubKey(reversibleVtxo.Sender), myPubkey) {
spendableVtxos = append(spendableVtxos, v)
}
}

}
for _, v := range spent {
indexedSpentVtxos[fmt.Sprintf(keyTempl, v.Txid, v.VOut)] = v
spentVtxos = append(spentVtxos, v)
}
}
script, err := bitcointree.ParseVtxoScript(v.Descriptor)
if err != nil {
return nil, nil, err
}

for _, vtxo := range pendingVtxos {
ptx, err := psbt.NewFromRawBytes(strings.NewReader(vtxo.RedeemTx), true)
if err != nil {
return nil, nil, err
}
input := ptx.UnsignedTx.TxIn[0].PreviousOutPoint
key := fmt.Sprintf(keyTempl, input.Hash.String(), input.Index)
if _, ok := indexedSpentVtxos[key]; !ok {
spendableVtxos = append(spendableVtxos, vtxo)
if reversibleVtxo, ok := script.(*bitcointree.ReversibleVtxoScript); ok {
if !bytes.Equal(schnorr.SerializePubKey(reversibleVtxo.Sender), myPubkey) {
spentVtxos = append(spentVtxos, v)
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/test/e2e/covenantless/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestReactToSpentVtxosRedemption(t *testing.T) {
}

func TestReactToAsyncSpentVtxosRedemption(t *testing.T) {
t.Run("receveir claimed funds", func(t *testing.T) {
t.Run("receiver claimed funds", func(t *testing.T) {
ctx := context.Background()
sdkClient, grpcClient := setupArkSDK(t)
defer grpcClient.Close()
Expand Down

0 comments on commit c0be278

Please sign in to comment.