Files
ark/pkg/client-sdk/internal/utils/utils.go
Pietralberto Mazza 8de2df3d7f Covenant-less ark sdk (#225)
* Add method to compute vtxo taproot script

* Drop debug logs

* Refactor client:
* Remove dep from stubs in interface
* Remove redeem branch, out of scope (moved to ark client)

* Add example for covenant and covenantless sdk

* Simplify explorer - No need for bitcoin and liquid impls

* Refactor wallet:
* wallet struct for common operations (create, lock/unlock, getType, isLocked)
* liquidWallet struct for liquid operations (derive/get addresses, sign tx)
* bitcoinWallet struct for bitcoin operations (derive/get addresses, sign tx)

* Update utils:
* drop methods to parse tree (moved to ark client)
* add methods for encryption, network parsing, key generation
* add methods for covenant/covenantless redeem branches (move to common?)

* Add support for covenantless sdk:
* move interface to dedicated file
* arkCLient struct for common operations (Init, lock/unlock, get config data, receive)
* covenantArkClient struct for covenant operations (onboard, balance, send, redeem)
* covenantlessArkClient struct for covenantless operations (onboard, balance, send, redeem)

* Fix wasm

* Fixes

* Make explorer use utils.Cache

* Renamings

* Lint

* Fix e2e tests

* Fix e2e test
2024-08-07 01:37:18 +02:00

242 lines
5.5 KiB
Go

package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"fmt"
"runtime/debug"
"sort"
"github.com/ark-network/ark-sdk/client"
"github.com/ark-network/ark/common"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/vulpemventures/go-elements/address"
"github.com/vulpemventures/go-elements/network"
"golang.org/x/crypto/scrypt"
)
func CoinSelect(
vtxos []client.Vtxo, amount, dust uint64, sortByExpirationTime bool,
) ([]client.Vtxo, uint64, error) {
selected := make([]client.Vtxo, 0)
notSelected := make([]client.Vtxo, 0)
selectedAmount := uint64(0)
if sortByExpirationTime {
// sort vtxos by expiration (older first)
sort.SliceStable(vtxos, func(i, j int) bool {
if vtxos[i].ExpiresAt == nil || vtxos[j].ExpiresAt == nil {
return false
}
return vtxos[i].ExpiresAt.Before(*vtxos[j].ExpiresAt)
})
}
for _, vtxo := range vtxos {
if selectedAmount >= amount {
notSelected = append(notSelected, vtxo)
break
}
selected = append(selected, vtxo)
selectedAmount += vtxo.Amount
}
if selectedAmount < amount {
return nil, 0, fmt.Errorf("not enough funds to cover amount%d", amount)
}
change := selectedAmount - amount
if change < dust {
if len(notSelected) > 0 {
selected = append(selected, notSelected[0])
change += notSelected[0].Amount
}
}
return selected, change, nil
}
func DecodeReceiverAddress(addr string) (
bool, []byte, *secp256k1.PublicKey, error,
) {
outputScript, err := address.ToOutputScript(addr)
if err != nil {
_, userPubkey, _, err := common.DecodeAddress(addr)
if err != nil {
return false, nil, nil, err
}
return false, nil, userPubkey, nil
}
return true, outputScript, nil, nil
}
func IsOnchainOnly(receivers []client.Output) bool {
for _, receiver := range receivers {
isOnChain, _, _, err := DecodeReceiverAddress(receiver.Address)
if err != nil {
continue
}
if !isOnChain {
return false
}
}
return true
}
func NetworkFromString(net string) common.Network {
switch net {
case common.Liquid.Name:
return common.Liquid
case common.LiquidTestNet.Name:
return common.LiquidTestNet
case common.LiquidRegTest.Name:
return common.LiquidRegTest
case common.BitcoinTestNet.Name:
return common.BitcoinTestNet
case common.BitcoinRegTest.Name:
return common.BitcoinRegTest
case common.Bitcoin.Name:
fallthrough
default:
return common.Bitcoin
}
}
func ToElementsNetwork(net common.Network) network.Network {
switch net.Name {
case common.Liquid.Name:
return network.Liquid
case common.LiquidTestNet.Name:
return network.Testnet
case common.LiquidRegTest.Name:
return network.Regtest
default:
return network.Liquid
}
}
func ToBitcoinNetwork(net common.Network) chaincfg.Params {
switch net.Name {
case common.Bitcoin.Name:
return chaincfg.MainNetParams
case common.BitcoinTestNet.Name:
return chaincfg.TestNet3Params
case common.BitcoinRegTest.Name:
return chaincfg.RegressionNetParams
default:
return chaincfg.MainNetParams
}
}
func GenerateRandomPrivateKey() (*secp256k1.PrivateKey, error) {
privKey, err := btcec.NewPrivateKey()
if err != nil {
return nil, err
}
return privKey, nil
}
func HashPassword(password []byte) []byte {
hash := sha256.Sum256(password)
return hash[:]
}
func EncryptAES128(privateKey, password []byte) ([]byte, error) {
// Due to https://github.com/golang/go/issues/7168.
// This call makes sure that memory is freed in case the GC doesn't do that
// right after the encryption/decryption.
defer debug.FreeOSMemory()
if len(privateKey) == 0 {
return nil, fmt.Errorf("missing plaintext private key")
}
if len(password) == 0 {
return nil, fmt.Errorf("missing encryption password")
}
key, salt, err := deriveKey(password, nil)
if err != nil {
return nil, err
}
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, privateKey, nil)
ciphertext = append(ciphertext, salt...)
return ciphertext, nil
}
func DecryptAES128(encrypted, password []byte) ([]byte, error) {
defer debug.FreeOSMemory()
if len(encrypted) == 0 {
return nil, fmt.Errorf("missing encrypted mnemonic")
}
if len(password) == 0 {
return nil, fmt.Errorf("missing decryption password")
}
salt := encrypted[len(encrypted)-32:]
data := encrypted[:len(encrypted)-32]
key, _, err := deriveKey(password, salt)
if err != nil {
return nil, err
}
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce, text := data[:gcm.NonceSize()], data[gcm.NonceSize():]
plaintext, err := gcm.Open(nil, nonce, text, nil)
if err != nil {
return nil, fmt.Errorf("invalid password")
}
return plaintext, nil
}
// deriveKey derives a 32 byte array key from a custom passhprase
func deriveKey(password, salt []byte) ([]byte, []byte, error) {
if salt == nil {
salt = make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
return nil, nil, err
}
}
// 2^20 = 1048576 recommended length for key-stretching
// check the doc for other recommended values:
// https://godoc.org/golang.org/x/crypto/scrypt
key, err := scrypt.Key(password, salt, 1048576, 8, 1, 32)
if err != nil {
return nil, nil, err
}
return key, salt, nil
}