Files
ark/server/test/e2e/test_utils.go
Pietralberto Mazza 7f937e8418 Vars and fields renaming (#387)
* Rename asp > server

* Rename pool > round

* Consolidate naming for pubkey/prvkey vars and types

* Fix

* Fix

* Fix wasm

* Rename congestionTree > vtxoTree

* Fix wasm

* Rename payment > request

* Rename congestionTree > vtxoTree after syncing with master

* Fix Send API in SDK

* Fix wasm

* Fix wasm

* Fixes

* Fixes after review

* Fix

* Fix naming

* Fix

* Fix e2e tests
2024-11-26 15:57:16 +01:00

136 lines
2.8 KiB
Go

package e2e
import (
"fmt"
"io"
"os/exec"
"strings"
"sync"
"time"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
)
const (
Password = "password"
// #nosec G101
NostrTestingSecretKey = "07959d1d2bc6507403449c556585d463a9ca4374eb0ec07b3929088ce6c34a7e"
)
type ArkBalance struct {
Offchain struct {
Total int `json:"total"`
} `json:"offchain_balance"`
Onchain struct {
Spendable int `json:"spendable_amount"`
Locked []struct {
Amount int `json:"amount"`
SpendableAt string `json:"spendable_at"`
} `json:"locked_amount"`
} `json:"onchain_balance"`
}
type ArkReceive struct {
Offchain string `json:"offchain_address"`
Boarding string `json:"boarding_address"`
}
func GenerateBlock() error {
if _, err := RunCommand("nigiri", "rpc", "--liquid", "generatetoaddress", "1", "el1qqwk722tghgkgmh3r2ph4d2apwj0dy9xnzlenzklx8jg3z299fpaw56trre9gpk6wmw0u4qycajqeva3t7lzp7wnacvwxha59r"); err != nil {
return err
}
if _, err := RunCommand("nigiri", "rpc", "generatetoaddress", "1", "bcrt1qe8eelqalnch946nzhefd5ajhgl2afjw5aegc59"); err != nil {
return err
}
time.Sleep(6 * time.Second)
return nil
}
func RunDockerExec(container string, arg ...string) (string, error) {
args := append([]string{"exec", "-t", container}, arg...)
return RunCommand("docker", args...)
}
func RunCommand(name string, arg ...string) (string, error) {
errb := new(strings.Builder)
cmd := newCommand(name, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return "", err
}
if err := cmd.Start(); err != nil {
return "", err
}
output := new(strings.Builder)
errorb := new(strings.Builder)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
if _, err := io.Copy(output, stdout); err != nil {
fmt.Fprintf(errb, "error reading stdout: %s", err)
}
}()
go func() {
defer wg.Done()
if _, err := io.Copy(errorb, stderr); err != nil {
fmt.Fprintf(errb, "error reading stderr: %s", err)
}
}()
wg.Wait()
if err := cmd.Wait(); err != nil {
if errMsg := errorb.String(); len(errMsg) > 0 {
return "", fmt.Errorf("%s", errMsg)
}
if outMsg := output.String(); len(outMsg) > 0 {
return "", fmt.Errorf("%s", outMsg)
}
return "", err
}
if errMsg := errb.String(); len(errMsg) > 0 {
return "", fmt.Errorf("%s", errMsg)
}
return strings.Trim(output.String(), "\n"), nil
}
func newCommand(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
return cmd
}
// nostr
// use nak utils https://github.com/fiatjaf/nak
func GetNostrKeys() (secretKey, pubkey string, npub string, err error) {
secretKey = NostrTestingSecretKey
pubkey, err = nostr.GetPublicKey(secretKey)
if err != nil {
return
}
npub, err = nip19.EncodePublicKey(pubkey)
if err != nil {
return
}
return
}