Files
ark/common/encoding.go
Pietralberto Mazza 89df461623 Update client sdk (#207)
* Add bitcoin networks

* Refactor client

* Refactor explorer

* Refactor store

* Refactor wallet

* Refactor sdk client

* Refactor wasm & Update examples

* Move common util funcs to internal/utils

* Move to constants for service types

* Add unit tests

* Parallelize tests

* Lint

* Add job to gh action

* go mod tidy

* Fixes

* Fixes

* Fix compose file

* Fixes

* Fixes after review:
* Drop factory pattern
* Drop password from ark client methods
* Make singlekey wallet manage store and wallet store instead of defining WalletStore as extension of Store
* Move constants to arksdk module
* Drop config and expect directory store and wallet as ark client factory args

* Fix

* Add constants for bitcoin/liquid explorer

* Fix test

* Fix wasm

* Rename client.Client to client.ASPClient

* Rename store.Store to store.ConfigStore

* Rename wallet.Wallet to wallet.WalletService

* Renamings

* Lint

* Fixes

* Move everything to internal/utils & move ComputeVtxoTaprootScript to common

* Go mod tidy
2024-07-30 16:08:23 +02:00

66 lines
1.4 KiB
Go

package common
import (
"fmt"
"github.com/btcsuite/btcd/btcutil/bech32"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
func EncodeAddress(
hrp string, userKey, aspKey *secp256k1.PublicKey,
) (addr string, err error) {
if userKey == nil {
err = fmt.Errorf("missing public key")
return
}
if aspKey == nil {
err = fmt.Errorf("missing asp public key")
return
}
if hrp != Liquid.Addr && hrp != LiquidTestNet.Addr {
err = fmt.Errorf("invalid prefix")
return
}
combinedKey := append(
aspKey.SerializeCompressed(), userKey.SerializeCompressed()...,
)
grp, err := bech32.ConvertBits(combinedKey, 8, 5, true)
if err != nil {
return
}
addr, err = bech32.EncodeM(hrp, grp)
return
}
func DecodeAddress(
addr string,
) (hrp string, userKey *secp256k1.PublicKey, aspKey *secp256k1.PublicKey, err error) {
prefix, buf, err := bech32.DecodeNoLimit(addr)
if err != nil {
return
}
if prefix != Liquid.Addr && prefix != LiquidTestNet.Addr && prefix != LiquidRegTest.Addr {
err = fmt.Errorf("invalid prefix")
return
}
grp, err := bech32.ConvertBits(buf, 5, 8, false)
if err != nil {
return
}
aKey, err := secp256k1.ParsePubKey(grp[:33])
if err != nil {
err = fmt.Errorf("failed to parse public key: %s", err)
return
}
uKey, err := secp256k1.ParsePubKey(grp[33:])
if err != nil {
err = fmt.Errorf("failed to parse asp public key: %s", err)
return
}
hrp = prefix
userKey = uKey
aspKey = aKey
return
}