New address encoding (#356)

* [common] rework address encoding

* new address encoding

* replace offchain address by vtxo output key in DB

* merge migrations files into init one

* fix txbuilder fixtures

* fix transaction events
This commit is contained in:
Louis Singer
2024-10-18 16:50:07 +02:00
committed by GitHub
parent b1c9261f14
commit b536a9e652
58 changed files with 2243 additions and 1896 deletions

View File

@@ -1,8 +1,10 @@
package bitcointree
import (
"encoding/hex"
"fmt"
"github.com/ark-network/ark/common"
"github.com/ark-network/ark/common/tree"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
@@ -15,7 +17,7 @@ import (
// CraftSharedOutput returns the taproot script and the amount of the initial root output
func CraftSharedOutput(
cosigners []*secp256k1.PublicKey, aspPubkey *secp256k1.PublicKey, receivers []Receiver,
cosigners []*secp256k1.PublicKey, aspPubkey *secp256k1.PublicKey, receivers []tree.VtxoLeaf,
feeSatsPerNode uint64, roundLifetime int64,
) ([]byte, int64, error) {
aggregatedKey, _, err := createAggregatedKeyWithSweep(
@@ -32,7 +34,7 @@ func CraftSharedOutput(
amount := root.getAmount() + int64(feeSatsPerNode)
scriptPubKey, err := taprootOutputScript(aggregatedKey.FinalKey)
scriptPubKey, err := common.P2TRScript(aggregatedKey.FinalKey)
if err != nil {
return nil, 0, err
}
@@ -42,7 +44,7 @@ func CraftSharedOutput(
// CraftCongestionTree creates all the tree's transactions
func CraftCongestionTree(
initialInput *wire.OutPoint, cosigners []*secp256k1.PublicKey, aspPubkey *secp256k1.PublicKey, receivers []Receiver,
initialInput *wire.OutPoint, cosigners []*secp256k1.PublicKey, aspPubkey *secp256k1.PublicKey, receivers []tree.VtxoLeaf,
feeSatsPerNode uint64, roundLifetime int64,
) (tree.CongestionTree, error) {
aggregatedKey, sweepTapLeaf, err := createAggregatedKeyWithSweep(
@@ -108,8 +110,8 @@ type node interface {
}
type leaf struct {
vtxoScript VtxoScript
amount int64
amount int64
pubkey *secp256k1.PublicKey
}
type branch struct {
@@ -142,12 +144,7 @@ func (l *leaf) getAmount() int64 {
}
func (l *leaf) getOutputs() ([]*wire.TxOut, error) {
taprootKey, _, err := l.vtxoScript.TapTree()
if err != nil {
return nil, err
}
script, err := taprootOutputScript(taprootKey)
script, err := common.P2TRScript(l.pubkey)
if err != nil {
return nil, err
}
@@ -161,7 +158,7 @@ func (l *leaf) getOutputs() ([]*wire.TxOut, error) {
}
func (b *branch) getOutputs() ([]*wire.TxOut, error) {
sharedOutputScript, err := taprootOutputScript(b.aggregatedKey.FinalKey)
sharedOutputScript, err := common.P2TRScript(b.aggregatedKey.FinalKey)
if err != nil {
return nil, err
}
@@ -246,7 +243,7 @@ func getTx(
func createRootNode(
aggregatedKey *musig2.AggregateKey,
cosigners []*secp256k1.PublicKey,
receivers []Receiver,
receivers []tree.VtxoLeaf,
feeSatsPerNode uint64,
) (root node, err error) {
if len(receivers) == 0 {
@@ -255,9 +252,19 @@ func createRootNode(
nodes := make([]node, 0, len(receivers))
for _, r := range receivers {
pubkeyBytes, err := hex.DecodeString(r.Pubkey)
if err != nil {
return nil, err
}
pubkey, err := schnorr.ParsePubKey(pubkeyBytes)
if err != nil {
return nil, err
}
leafNode := &leaf{
vtxoScript: r.Script,
amount: int64(r.Amount),
amount: int64(r.Amount),
pubkey: pubkey,
}
nodes = append(nodes, leafNode)
}
@@ -339,9 +346,3 @@ func createUpperLevel(nodes []node, aggregatedKey *musig2.AggregateKey, cosigner
}
return pairs, nil
}
func taprootOutputScript(taprootKey *secp256k1.PublicKey) ([]byte, error) {
return txscript.NewScriptBuilder().AddOp(txscript.OP_1).AddData(
schnorr.SerializePubKey(taprootKey),
).Script()
}