mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 20:24:21 +01:00
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:
@@ -1,25 +1,55 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
||||
"github.com/ark-network/ark/common"
|
||||
"github.com/ark-network/ark/common/tree"
|
||||
"github.com/ark-network/ark/server/internal/core/application"
|
||||
"github.com/ark-network/ark/server/internal/core/domain"
|
||||
"github.com/ark-network/ark/server/internal/core/ports"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
)
|
||||
|
||||
// From interface type to app type
|
||||
|
||||
func parseAddress(addr string) (string, *secp256k1.PublicKey, *secp256k1.PublicKey, error) {
|
||||
func parseAddress(addr string) (*common.Address, error) {
|
||||
if len(addr) <= 0 {
|
||||
return "", nil, nil, fmt.Errorf("missing address")
|
||||
return nil, fmt.Errorf("missing address")
|
||||
}
|
||||
return common.DecodeAddress(addr)
|
||||
}
|
||||
|
||||
func parseAsyncPaymentInputs(ins []*arkv1.AsyncPaymentInput) ([]application.AsyncPaymentInput, error) {
|
||||
if len(ins) <= 0 {
|
||||
return nil, fmt.Errorf("missing inputs")
|
||||
}
|
||||
|
||||
inputs := make([]application.AsyncPaymentInput, 0, len(ins))
|
||||
for _, input := range ins {
|
||||
forfeitLeafHash, err := chainhash.NewHashFromStr(input.GetForfeitLeafHash())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid forfeit leaf hash: %s", err)
|
||||
}
|
||||
|
||||
inputs = append(inputs, application.AsyncPaymentInput{
|
||||
Input: ports.Input{
|
||||
VtxoKey: domain.VtxoKey{
|
||||
Txid: input.GetInput().GetOutpoint().GetTxid(),
|
||||
VOut: input.GetInput().GetOutpoint().GetVout(),
|
||||
},
|
||||
Descriptor: input.GetInput().GetDescriptor_(),
|
||||
},
|
||||
ForfeitLeafHash: *forfeitLeafHash,
|
||||
})
|
||||
}
|
||||
|
||||
return inputs, nil
|
||||
}
|
||||
|
||||
func parseInputs(ins []*arkv1.Input) ([]ports.Input, error) {
|
||||
if len(ins) <= 0 {
|
||||
return nil, fmt.Errorf("missing inputs")
|
||||
@@ -39,26 +69,43 @@ func parseInputs(ins []*arkv1.Input) ([]ports.Input, error) {
|
||||
return inputs, nil
|
||||
}
|
||||
|
||||
func parseReceiver(out *arkv1.Output) (domain.Receiver, error) {
|
||||
decodedAddr, err := common.DecodeAddress(out.GetAddress())
|
||||
if err != nil {
|
||||
// onchain address
|
||||
return domain.Receiver{
|
||||
Amount: out.GetAmount(),
|
||||
OnchainAddress: out.GetAddress(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return domain.Receiver{
|
||||
Amount: out.GetAmount(),
|
||||
Pubkey: hex.EncodeToString(schnorr.SerializePubKey(decodedAddr.VtxoTapKey)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseReceivers(outs []*arkv1.Output) ([]domain.Receiver, error) {
|
||||
receivers := make([]domain.Receiver, 0, len(outs))
|
||||
for _, out := range outs {
|
||||
if out.GetAmount() == 0 {
|
||||
return nil, fmt.Errorf("missing output amount")
|
||||
}
|
||||
if len(out.GetAddress()) <= 0 && len(out.GetDescriptor_()) <= 0 {
|
||||
if len(out.GetAddress()) <= 0 {
|
||||
return nil, fmt.Errorf("missing output destination")
|
||||
}
|
||||
|
||||
receivers = append(receivers, domain.Receiver{
|
||||
Descriptor: out.GetDescriptor_(),
|
||||
Amount: out.GetAmount(),
|
||||
OnchainAddress: out.GetAddress(),
|
||||
})
|
||||
rcv, err := parseReceiver(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
receivers = append(receivers, rcv)
|
||||
}
|
||||
return receivers, nil
|
||||
}
|
||||
|
||||
// From app typeto interface type
|
||||
// From app type to interface type
|
||||
|
||||
type vtxoList []domain.Vtxo
|
||||
|
||||
@@ -70,15 +117,15 @@ func (v vtxoList) toProto() []*arkv1.Vtxo {
|
||||
Txid: vv.Txid,
|
||||
Vout: vv.VOut,
|
||||
},
|
||||
Descriptor_: vv.Descriptor,
|
||||
Amount: vv.Amount,
|
||||
RoundTxid: vv.RoundTxid,
|
||||
Spent: vv.Spent,
|
||||
ExpireAt: vv.ExpireAt,
|
||||
SpentBy: vv.SpentBy,
|
||||
Swept: vv.Swept,
|
||||
RedeemTx: vv.RedeemTx,
|
||||
Pending: vv.Pending,
|
||||
Amount: vv.Amount,
|
||||
RoundTxid: vv.RoundTxid,
|
||||
Spent: vv.Spent,
|
||||
ExpireAt: vv.ExpireAt,
|
||||
SpentBy: vv.SpentBy,
|
||||
Swept: vv.Swept,
|
||||
RedeemTx: vv.RedeemTx,
|
||||
Pending: vv.Pending,
|
||||
Pubkey: vv.Pubkey,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user