Files
ark/client/covenantless/init.go
Louis Singer 4da76ec88b New boarding protocol (#279)
* [domain] add reverse boarding inputs in Payment struct

* [tx-builder] support reverse boarding script

* [wallet] add GetTransaction

* [api-spec][application] add reverse boarding support in covenantless

* [config] add reverse boarding config

* [api-spec] add ReverseBoardingAddress RPC

* [domain][application] support empty forfeits txs in EndFinalization events

* [tx-builder] optional connector output in round tx

* [btc-embedded] fix getTx and taproot finalizer

* whitelist ReverseBoardingAddress RPC

* [test] add reverse boarding integration test

* [client] support reverse boarding

* [sdk] support reverse boarding

* [e2e] add sleep time after faucet

* [test] run using bitcoin-core RPC

* [tx-builder] fix GetSweepInput

* [application][tx-builder] support reverse onboarding in covenant

* [cli] support reverse onboarding in covenant CLI

* [test] rework integration tests

* [sdk] remove onchain wallet, replace by onboarding address

* remove old onboarding protocols

* [sdk] Fix RegisterPayment

* [e2e] add more funds to covenant ASP

* [e2e] add sleeping time

* several fixes

* descriptor boarding

* remove boarding delay from info

* [sdk] implement descriptor boarding

* go mod tidy

* fixes and revert error msgs

* move descriptor pkg to common

* add replace in go.mod

* [sdk] fix unit tests

* rename DescriptorInput --> BoardingInput

* genrest in SDK

* remove boarding input from domain

* remove all "reverse boarding"

* rename "onboarding" ==> "boarding"

* remove outdate payment unit test

* use tmpfs docker volument for compose testing files

* several fixes
2024-09-04 19:21:26 +02:00

129 lines
3.2 KiB
Go

package covenantless
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
"github.com/ark-network/ark/client/utils"
"github.com/ark-network/ark/common"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/urfave/cli/v2"
)
var explorerUrls = map[string]string{
common.Bitcoin.Name: "https://blockstream.info/api",
common.BitcoinTestNet.Name: "https://blockstream.info/testnet/api",
common.BitcoinRegTest.Name: "http://localhost:3000",
common.BitcoinSigNet.Name: "https://mutinynet.com/api",
}
func (c *clArkBitcoinCLI) Init(ctx *cli.Context) error {
key := ctx.String("prvkey")
net := strings.ToLower(ctx.String("network"))
url := ctx.String("asp-url")
explorer := ctx.String("explorer")
var explorerURL string
if len(url) <= 0 {
return fmt.Errorf("invalid ark url")
}
if net != common.Bitcoin.Name && net != common.BitcoinTestNet.Name && net != common.BitcoinRegTest.Name && net != common.BitcoinSigNet.Name {
return fmt.Errorf("invalid network")
}
if len(explorer) > 0 {
explorerURL = explorer
} else {
explorerURL = explorerUrls[net]
}
if err := connectToAsp(ctx, net, url, explorerURL); err != nil {
return err
}
password, err := utils.ReadPassword(ctx, false)
if err != nil {
return err
}
return initWallet(ctx, key, password)
}
func generateRandomPrivateKey() (*secp256k1.PrivateKey, error) {
privKey, err := btcec.NewPrivateKey()
if err != nil {
return nil, err
}
return privKey, nil
}
func connectToAsp(ctx *cli.Context, net, url, explorer string) error {
client, close, err := getClient(url)
if err != nil {
return err
}
defer close()
resp, err := client.GetInfo(ctx.Context, &arkv1.GetInfoRequest{})
if err != nil {
return err
}
return utils.SetState(ctx, map[string]string{
utils.ASP_URL: url,
utils.NETWORK: net,
utils.ASP_PUBKEY: resp.Pubkey,
utils.ROUND_LIFETIME: strconv.Itoa(int(resp.GetRoundLifetime())),
utils.UNILATERAL_EXIT_DELAY: strconv.Itoa(int(resp.GetUnilateralExitDelay())),
utils.MIN_RELAY_FEE: strconv.Itoa(int(resp.MinRelayFee)),
utils.EXPLORER: explorer,
utils.BOARDING_TEMPLATE: resp.GetBoardingDescriptorTemplate(),
})
}
func initWallet(ctx *cli.Context, key string, password []byte) error {
var privateKey *secp256k1.PrivateKey
if len(key) <= 0 {
privKey, err := generateRandomPrivateKey()
if err != nil {
return err
}
privateKey = privKey
} else {
privKeyBytes, err := hex.DecodeString(key)
if err != nil {
return err
}
privateKey = secp256k1.PrivKeyFromBytes(privKeyBytes)
}
cypher := utils.NewAES128Cypher()
buf := privateKey.Serialize()
encryptedPrivateKey, err := cypher.Encrypt(buf, password)
if err != nil {
return err
}
passwordHash := utils.HashPassword([]byte(password))
pubkey := privateKey.PubKey().SerializeCompressed()
state := map[string]string{
utils.ENCRYPTED_PRVKEY: hex.EncodeToString(encryptedPrivateKey),
utils.PASSWORD_HASH: hex.EncodeToString(passwordHash),
utils.PUBKEY: hex.EncodeToString(pubkey),
}
if err := utils.SetState(ctx, state); err != nil {
return err
}
fmt.Println("wallet initialized")
return nil
}