mirror of
https://github.com/aljazceru/ark.git
synced 2026-02-03 02:24:40 +01:00
* Fixes * Fixes to domain layer: * Add Leaf bool field to know to fix the returned list of leaves * Add non-persisted UnsignedForfeitTxs to RoundFinalizationStarted * Store only error msg when round fails instead of full error * Fix wallet interface: * Add Close() to close conn with wallet * Add GetAsset() to fix missing asset err when calling Transfer() * Fix gocron scheduler to correctly run/build the project * Fix badger repo implementation: * Fix datadirs of projection stores * Return error if current round not found * Fix round event deserialization * Fix TxBuilder interface & dummy impl: * Pass asp pubkey as arg of the defined functions * Fix connectorsToInputArgs to return the right number of ins * Fix getTxid() to return the id of an hex encoded tx too * Fix createConnectors() to return a tx if there's only 1 connector * Add leaf bool field to psetWithLevel in case a leaf is not in the last level * Fix node's isLeaf() check * Move to hex encoded pubkeys instead of ark encoded * Fix app layer: * Add Start() and Stop() to the interface & Expect raw pubkeys instead of strings as args * Source & cache pubkey from wallet at startup * Drop usage of scheduler and schedule next task based on occurred round events * Increase verbosity * Use hex instead of ark encoding to store receveirs' pubkeys * Lower faucet amount from 100k to 10k sats in total * Fix finalizeRound() to persist round events even if it failed * Add view() to forfeitTxMap to enrich RoundFinalizationEvent with unsigned forfeit txs * Add app config * Fix interface layer: * Remove repo manager from handler factory * Fix GetEventStream to forward events to stream once they arrive from app layer * Return missing unsigned forfeit txs in RoundFinalizationEvent * Fix extracting user pubkey from address * Add log interceptors * Add config struct * Add factory * Clean interface * Add config and launcher * Tidy deps & Set defaut round interval to 30secs for dev mode
224 lines
5.0 KiB
Go
224 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
|
"github.com/ark-network/ark/common"
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
"github.com/urfave/cli/v2"
|
|
"github.com/vulpemventures/go-elements/psetv2"
|
|
)
|
|
|
|
type receiver struct {
|
|
To string `json:"to"`
|
|
Amount uint64 `json:"amount"`
|
|
}
|
|
|
|
var (
|
|
receiversFlag = cli.StringFlag{
|
|
Name: "receivers",
|
|
Usage: "receivers of the send transaction, JSON encoded: '[{\"to\": \"<...>\", \"amount\": <...>}, ...]'",
|
|
Value: "",
|
|
Required: true,
|
|
}
|
|
)
|
|
|
|
var sendCommand = cli.Command{
|
|
Name: "send",
|
|
Usage: "Send VTXOs to a list of addresses",
|
|
Action: sendAction,
|
|
Flags: []cli.Flag{&receiversFlag},
|
|
}
|
|
|
|
func sendAction(ctx *cli.Context) error {
|
|
receivers := ctx.String("receivers")
|
|
|
|
// parse json encoded receivers
|
|
var receiversJSON []receiver
|
|
if err := json.Unmarshal([]byte(receivers), &receiversJSON); err != nil {
|
|
return fmt.Errorf("invalid receivers: %s", err)
|
|
}
|
|
|
|
if len(receiversJSON) <= 0 {
|
|
return fmt.Errorf("no receivers specified")
|
|
}
|
|
|
|
aspPubKey, err := getServiceProviderPublicKey()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
receiversOutput := make([]*arkv1.Output, 0)
|
|
sumOfReceivers := uint64(0)
|
|
|
|
for _, receiver := range receiversJSON {
|
|
_, userKey, aspKey, err := common.DecodeAddress(receiver.To)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid receiver address: %s", err)
|
|
}
|
|
|
|
if !bytes.Equal(aspPubKey.SerializeCompressed(), aspKey.SerializeCompressed()) {
|
|
return fmt.Errorf("invalid receiver address '%s': must be associated with the connected service provider", receiver.To)
|
|
}
|
|
|
|
if receiver.Amount <= 0 {
|
|
return fmt.Errorf("invalid amount: %d", receiver.Amount)
|
|
}
|
|
|
|
encodedKey := hex.EncodeToString(userKey.SerializeCompressed())
|
|
receiversOutput = append(receiversOutput, &arkv1.Output{
|
|
Pubkey: encodedKey,
|
|
Amount: uint64(receiver.Amount),
|
|
})
|
|
sumOfReceivers += receiver.Amount
|
|
}
|
|
client, close, err := getArkClient(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer close()
|
|
|
|
vtxos, err := getVtxos(ctx, client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
selectedCoins, changeAmount, err := coinSelect(vtxos, sumOfReceivers)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if changeAmount > 0 {
|
|
walletPrvKey, err := privateKeyFromPassword()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
walletPubKey := walletPrvKey.PubKey()
|
|
encodedPubKey := hex.EncodeToString(walletPubKey.SerializeCompressed())
|
|
|
|
changeReceiver := &arkv1.Output{
|
|
Pubkey: encodedPubKey,
|
|
Amount: changeAmount,
|
|
}
|
|
receiversOutput = append(receiversOutput, changeReceiver)
|
|
}
|
|
|
|
inputs := make([]*arkv1.Input, 0, len(selectedCoins))
|
|
|
|
for _, coin := range selectedCoins {
|
|
inputs = append(inputs, &arkv1.Input{
|
|
Txid: coin.txid,
|
|
Vout: coin.vout,
|
|
})
|
|
}
|
|
|
|
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: receiversOutput,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
stream, err := client.GetEventStream(ctx.Context, &arkv1.GetEventStreamRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pingStop := ping(ctx, client, &arkv1.PingRequest{
|
|
PaymentId: registerResponse.GetId(),
|
|
})
|
|
|
|
for {
|
|
event, err := stream.Recv()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if event.GetRoundFailed() != nil {
|
|
return fmt.Errorf("round failed: %s", event.GetRoundFailed().GetReason())
|
|
}
|
|
|
|
if event.GetRoundFinalization() != nil {
|
|
pingStop()
|
|
forfeits := event.GetRoundFinalization().GetForfeitTxs()
|
|
signedForfeits := make([]string, 0)
|
|
|
|
for _, forfeit := range forfeits {
|
|
pset, err := psetv2.NewPsetFromBase64(forfeit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// check if it contains one of the input to sign
|
|
for _, input := range pset.Inputs {
|
|
inputTxid := chainhash.Hash(input.PreviousTxid).String()
|
|
|
|
for _, coin := range selectedCoins {
|
|
if inputTxid == coin.txid {
|
|
// TODO: sign the vtxo input
|
|
signedForfeits = append(signedForfeits, forfeit)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(signedForfeits) == 0 {
|
|
continue
|
|
}
|
|
|
|
_, err := client.FinalizePayment(ctx.Context, &arkv1.FinalizePaymentRequest{
|
|
SignedForfeitTxs: signedForfeits,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
if event.GetRoundFinalized() != nil {
|
|
return printJSON(map[string]interface{}{
|
|
"paymentId": registerResponse.GetId(),
|
|
"poolTxId": event.GetRoundFinalized().GetPoolTxid(),
|
|
})
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// send 1 ping message every 5 seconds to signal to the ark service that we are still alive
|
|
// returns a function that can be used to stop the pinging
|
|
func ping(ctx *cli.Context, client arkv1.ArkServiceClient, req *arkv1.PingRequest) func() {
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
go func(t *time.Ticker) {
|
|
for range t.C {
|
|
_, err := client.Ping(ctx.Context, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}(ticker)
|
|
|
|
return ticker.Stop
|
|
}
|