mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 04:34:19 +01:00
* [server] descriptor-based vtxo script * [server] fix unit tests * [sdk] descriptor based vtxo * empty config check & version flag support * fix: empty config check & version flag support (#309) * fix * [sdk] several fixes * [sdk][server] several fixes * [common][sdk] add reversible VtxoScript type, use it in async payment * [common] improve parser * [common] fix reversible vtxo parser * [sdk] remove logs * fix forfeit map * remove debug log * [sdk] do not allow reversible vtxo script in case of self-transfer * remove signing pubkey * remove signer public key, craft forfeit txs client side * go work sync * fix linter errors * rename MakeForfeitTxs to BuildForfeitTxs * fix conflicts * fix tests * comment VtxoScript type * revert ROUND_INTERVAL value --------- Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com> Co-authored-by: sekulicd <sekula87@gmail.com>
158 lines
3.8 KiB
Go
158 lines
3.8 KiB
Go
package descriptor
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
|
)
|
|
|
|
// tr(unspendable, { and(pk(user), pk(asp)), and(older(timeout), pk(user)) })
|
|
const DefaultVtxoDescriptorTemplate = "tr(%s,{ and(pk(%s), pk(%s)), and(older(%d), pk(%s)) })"
|
|
|
|
// tr(unspendable, { { and(pk(sender), pk(asp)), and(older(timeout), pk(sender)) }, and(pk(receiver), pk(asp)) })
|
|
const ReversibleVtxoScriptTemplate = "tr(%s,{ { and(pk(%s), pk(%s)), and(older(%d), pk(%s)) }, and(pk(%s), pk(%s)) })"
|
|
|
|
func ParseReversibleVtxoDescriptor(
|
|
desc TaprootDescriptor,
|
|
) (user, sender, asp *secp256k1.PublicKey, timeout uint, err error) {
|
|
if len(desc.ScriptTree) != 3 {
|
|
return nil, nil, nil, 0, errors.New("not a reversible vtxo script descriptor")
|
|
}
|
|
|
|
for _, leaf := range desc.ScriptTree {
|
|
if andLeaf, ok := leaf.(*And); ok {
|
|
if first, ok := andLeaf.First.(*PK); ok {
|
|
if second, ok := andLeaf.Second.(*PK); ok {
|
|
keyBytes, err := hex.DecodeString(first.Key.Hex)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
if sender == nil {
|
|
sender, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
} else {
|
|
user, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
}
|
|
|
|
if asp == nil {
|
|
keyBytes, err = hex.DecodeString(second.Key.Hex)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
|
|
asp, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if first, ok := andLeaf.First.(*Older); ok {
|
|
if second, ok := andLeaf.Second.(*PK); ok {
|
|
timeout = first.Timeout
|
|
keyBytes, err := hex.DecodeString(second.Key.Hex)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
|
|
sender, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, nil, 0, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if user == nil {
|
|
return nil, nil, nil, 0, errors.New("descriptor is invalid")
|
|
}
|
|
|
|
if asp == nil {
|
|
return nil, nil, nil, 0, errors.New("descriptor is invalid")
|
|
}
|
|
|
|
if timeout == 0 {
|
|
return nil, nil, nil, 0, errors.New("descriptor is invalid")
|
|
}
|
|
|
|
if sender == nil {
|
|
return nil, nil, nil, 0, errors.New("descriptor is invalid")
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func ParseDefaultVtxoDescriptor(
|
|
desc TaprootDescriptor,
|
|
) (user, asp *secp256k1.PublicKey, timeout uint, err error) {
|
|
if len(desc.ScriptTree) != 2 {
|
|
return nil, nil, 0, errors.New("not a default vtxo script descriptor")
|
|
}
|
|
|
|
for _, leaf := range desc.ScriptTree {
|
|
if andLeaf, ok := leaf.(*And); ok {
|
|
if first, ok := andLeaf.First.(*PK); ok {
|
|
if second, ok := andLeaf.Second.(*PK); ok {
|
|
keyBytes, err := hex.DecodeString(first.Key.Hex)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
user, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
keyBytes, err = hex.DecodeString(second.Key.Hex)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
asp, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
}
|
|
}
|
|
|
|
if first, ok := andLeaf.First.(*Older); ok {
|
|
if second, ok := andLeaf.Second.(*PK); ok {
|
|
timeout = first.Timeout
|
|
keyBytes, err := hex.DecodeString(second.Key.Hex)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
user, err = schnorr.ParsePubKey(keyBytes)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if user == nil {
|
|
return nil, nil, 0, errors.New("boarding descriptor is invalid")
|
|
}
|
|
|
|
if asp == nil {
|
|
return nil, nil, 0, errors.New("boarding descriptor is invalid")
|
|
}
|
|
|
|
if timeout == 0 {
|
|
return nil, nil, 0, errors.New("boarding descriptor is invalid")
|
|
}
|
|
|
|
return
|
|
}
|