mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 12:44:19 +01:00
Add integration tests and regtest support (#128)
* regtest support + integration tests (e2e) * add integration CI * add PR trigger on integration CI * wait for ocean to be unlocked at startup * integration tests: add tests flags and build docker images at startup * use nigiri chopsticks-liquid * fix after reviews * Update client/init.go Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com> Signed-off-by: Louis Singer <41042567+louisinger@users.noreply.github.com> * do not trigger integration on PR --------- Signed-off-by: Louis Singer <41042567+louisinger@users.noreply.github.com> Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -11,15 +13,10 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/vulpemventures/go-elements/network"
|
||||
)
|
||||
|
||||
var (
|
||||
passwordFlag = cli.StringFlag{
|
||||
Name: "password",
|
||||
Usage: "password to encrypt private key",
|
||||
Required: true,
|
||||
}
|
||||
|
||||
privateKeyFlag = cli.StringFlag{
|
||||
Name: "prvkey",
|
||||
Usage: "optional, private key to encrypt",
|
||||
@@ -34,35 +31,54 @@ var (
|
||||
Usage: "the url of the ASP to connect to",
|
||||
Required: true,
|
||||
}
|
||||
explorerFlag = cli.StringFlag{
|
||||
Name: "explorer",
|
||||
Usage: "the url of the explorer to use",
|
||||
}
|
||||
)
|
||||
|
||||
var initCommand = cli.Command{
|
||||
Name: "init",
|
||||
Usage: "Initialize your Ark wallet with an encryption password, and connect it to an ASP",
|
||||
Action: initAction,
|
||||
Flags: []cli.Flag{&passwordFlag, &privateKeyFlag, &networkFlag, &urlFlag},
|
||||
Flags: []cli.Flag{&passwordFlag, &privateKeyFlag, &networkFlag, &urlFlag, &explorerFlag},
|
||||
}
|
||||
|
||||
func initAction(ctx *cli.Context) error {
|
||||
key := ctx.String("prvkey")
|
||||
password := ctx.String("password")
|
||||
net := strings.ToLower(ctx.String("network"))
|
||||
url := ctx.String("ark-url")
|
||||
explorer := ctx.String("explorer")
|
||||
|
||||
var explorerURL string
|
||||
|
||||
if len(password) <= 0 {
|
||||
return fmt.Errorf("invalid password")
|
||||
}
|
||||
if len(url) <= 0 {
|
||||
return fmt.Errorf("invalid ark url")
|
||||
}
|
||||
if net != "mainnet" && net != "testnet" {
|
||||
if net != "mainnet" && net != "testnet" && net != "regtest" {
|
||||
return fmt.Errorf("invalid network")
|
||||
}
|
||||
|
||||
if err := connectToAsp(ctx.Context, net, url); err != nil {
|
||||
if len(explorer) > 0 {
|
||||
explorerURL = explorer
|
||||
_, network := networkFromString(net)
|
||||
if err := testEsploraEndpoint(network, explorerURL); err != nil {
|
||||
return fmt.Errorf("failed to connect with explorer: %s", err)
|
||||
}
|
||||
} else {
|
||||
explorerURL = explorerUrl[net]
|
||||
}
|
||||
|
||||
if err := connectToAsp(ctx.Context, net, url, explorerURL); err != nil {
|
||||
return err
|
||||
}
|
||||
return initWallet(ctx, key, password)
|
||||
|
||||
password, err := readPassword(ctx, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return initWallet(key, password)
|
||||
}
|
||||
|
||||
func generateRandomPrivateKey() (*secp256k1.PrivateKey, error) {
|
||||
@@ -73,7 +89,7 @@ func generateRandomPrivateKey() (*secp256k1.PrivateKey, error) {
|
||||
return privKey, nil
|
||||
}
|
||||
|
||||
func connectToAsp(ctx context.Context, net, url string) error {
|
||||
func connectToAsp(ctx context.Context, net, url, explorer string) error {
|
||||
client, close, err := getClient(url)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -91,10 +107,11 @@ func connectToAsp(ctx context.Context, net, url string) error {
|
||||
ASP_PUBKEY: resp.Pubkey,
|
||||
ROUND_LIFETIME: strconv.Itoa(int(resp.GetRoundLifetime())),
|
||||
UNILATERAL_EXIT_DELAY: strconv.Itoa(int(resp.GetUnilateralExitDelay())),
|
||||
EXPLORER: explorer,
|
||||
})
|
||||
}
|
||||
|
||||
func initWallet(ctx *cli.Context, key, password string) error {
|
||||
func initWallet(key string, password []byte) error {
|
||||
var privateKey *secp256k1.PrivateKey
|
||||
if len(key) <= 0 {
|
||||
privKey, err := generateRandomPrivateKey()
|
||||
@@ -113,7 +130,7 @@ func initWallet(ctx *cli.Context, key, password string) error {
|
||||
|
||||
cypher := newAES128Cypher()
|
||||
buf := privateKey.Serialize()
|
||||
encryptedPrivateKey, err := cypher.encrypt(buf, []byte(password))
|
||||
encryptedPrivateKey, err := cypher.encrypt(buf, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -134,3 +151,20 @@ func initWallet(ctx *cli.Context, key, password string) error {
|
||||
fmt.Println("wallet initialized")
|
||||
return nil
|
||||
}
|
||||
|
||||
func testEsploraEndpoint(net *network.Network, url string) error {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/asset/%s", url, net.AssetID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf(string(body))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user