mirror of
https://github.com/aljazceru/ark.git
synced 2026-02-06 20:14:44 +01:00
* api-spec: move the api-spec to root and init go.mod * go mod tidy * move buf files in the root as well * gh action for api-spec changes only * gh action for api-spec on push and pr * introduce go.work and remove all replaces * solve dependencies and force btcd/btcec@v2.3.3 * go work sync * force btcd/btcec@v2.3.3 * go mod tidy
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package covenantless
|
|
|
|
import (
|
|
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
|
"github.com/ark-network/ark/client/utils"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func (c *clArkBitcoinCLI) ClaimAsync(ctx *cli.Context) error {
|
|
client, cancel, err := getClientFromState(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cancel()
|
|
|
|
myselfOffchain, _, _, err := getAddress(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
vtxos, err := getVtxos(ctx, nil, client, myselfOffchain, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var pendingBalance uint64
|
|
var pendingVtxos []vtxo
|
|
for _, vtxo := range vtxos {
|
|
if vtxo.pending {
|
|
pendingBalance += vtxo.amount
|
|
pendingVtxos = append(pendingVtxos, vtxo)
|
|
}
|
|
}
|
|
if pendingBalance == 0 {
|
|
return nil
|
|
}
|
|
|
|
receiver := receiver{
|
|
To: myselfOffchain,
|
|
Amount: pendingBalance,
|
|
}
|
|
return selfTransferAllPendingPayments(
|
|
ctx, client, pendingVtxos, receiver,
|
|
)
|
|
}
|
|
|
|
func selfTransferAllPendingPayments(
|
|
ctx *cli.Context, client arkv1.ArkServiceClient,
|
|
pendingVtxos []vtxo, myself receiver,
|
|
) error {
|
|
inputs := make([]*arkv1.Input, 0, len(pendingVtxos))
|
|
|
|
for _, coin := range pendingVtxos {
|
|
inputs = append(inputs, &arkv1.Input{
|
|
Txid: coin.txid,
|
|
Vout: coin.vout,
|
|
})
|
|
}
|
|
|
|
receiversOutput := []*arkv1.Output{
|
|
{
|
|
Address: myself.To,
|
|
Amount: myself.Amount,
|
|
},
|
|
}
|
|
|
|
secKey, err := utils.PrivateKeyFromPassword(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
registerResponse, err := client.RegisterPayment(
|
|
ctx.Context, &arkv1.RegisterPaymentRequest{Inputs: inputs},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = client.ClaimPayment(ctx.Context, &arkv1.ClaimPaymentRequest{
|
|
Id: registerResponse.GetId(),
|
|
Outputs: []*arkv1.Output{{Address: myself.To, Amount: myself.Amount}},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
poolTxID, err := handleRoundStream(
|
|
ctx, client, registerResponse.GetId(),
|
|
pendingVtxos, secKey, receiversOutput,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return utils.PrintJSON(map[string]interface{}{
|
|
"pool_txid": poolTxID,
|
|
})
|
|
}
|