mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 20:24:21 +01:00
* update UnrollClosure * update TrustedOnboarding flow + add TrustedOnboarding e2e test * fix linter: grpc.Dial * add comment * integration tests: faucet the ASP with 2 utxos
145 lines
2.8 KiB
Go
145 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
|
|
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
|
"github.com/ark-network/ark/common/tree"
|
|
"github.com/urfave/cli/v2"
|
|
"github.com/vulpemventures/go-elements/payment"
|
|
"github.com/vulpemventures/go-elements/psetv2"
|
|
)
|
|
|
|
const (
|
|
minRelayFee = 30
|
|
)
|
|
|
|
var (
|
|
amountOnboardFlag = cli.Uint64Flag{
|
|
Name: "amount",
|
|
Usage: "amount to onboard in sats",
|
|
}
|
|
trustedOnboardFlag = cli.BoolFlag{
|
|
Name: "trusted",
|
|
Usage: "trusted onboard",
|
|
}
|
|
)
|
|
|
|
var onboardCommand = cli.Command{
|
|
Name: "onboard",
|
|
Usage: "Onboard the Ark by lifting your funds",
|
|
Action: onboardAction,
|
|
Flags: []cli.Flag{&amountOnboardFlag, &trustedOnboardFlag, &passwordFlag},
|
|
}
|
|
|
|
func onboardAction(ctx *cli.Context) error {
|
|
isTrusted := ctx.Bool("trusted")
|
|
|
|
amount := ctx.Uint64("amount")
|
|
|
|
if !isTrusted && amount <= 0 {
|
|
return fmt.Errorf("missing amount flag (--amount)")
|
|
}
|
|
|
|
_, net := getNetwork(ctx)
|
|
|
|
userPubKey, err := getWalletPublicKey(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client, cancel, err := getClientFromState(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cancel()
|
|
|
|
if isTrusted {
|
|
resp, err := client.TrustedOnboarding(ctx.Context, &arkv1.TrustedOnboardingRequest{
|
|
UserPubkey: hex.EncodeToString(userPubKey.SerializeCompressed()),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return printJSON(map[string]interface{}{
|
|
"onboard_address": resp.Address,
|
|
})
|
|
}
|
|
|
|
aspPubkey, err := getAspPublicKey(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
roundLifetime, err := getRoundLifetime(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
unilateralExitDelay, err := getUnilateralExitDelay(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
congestionTreeLeaf := tree.Receiver{
|
|
Pubkey: hex.EncodeToString(userPubKey.SerializeCompressed()),
|
|
Amount: amount,
|
|
}
|
|
|
|
treeFactoryFn, sharedOutputScript, sharedOutputAmount, err := tree.CraftCongestionTree(
|
|
net.AssetID, aspPubkey, []tree.Receiver{congestionTreeLeaf},
|
|
minRelayFee, roundLifetime, unilateralExitDelay,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pay, err := payment.FromScript(sharedOutputScript, net, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
address, err := pay.TaprootAddress()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
onchainReceiver := receiver{
|
|
To: address,
|
|
Amount: sharedOutputAmount,
|
|
}
|
|
|
|
pset, err := sendOnchain(ctx, []receiver{onchainReceiver})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ptx, _ := psetv2.NewPsetFromBase64(pset)
|
|
utx, _ := ptx.UnsignedTx()
|
|
txid := utx.TxHash().String()
|
|
|
|
congestionTree, err := treeFactoryFn(psetv2.InputArgs{
|
|
Txid: txid,
|
|
TxIndex: 0,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = client.Onboard(ctx.Context, &arkv1.OnboardRequest{
|
|
BoardingTx: pset,
|
|
CongestionTree: castCongestionTree(congestionTree),
|
|
UserPubkey: hex.EncodeToString(userPubKey.SerializeCompressed()),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println("onboard_txid:", txid)
|
|
|
|
return nil
|
|
}
|