mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 04:04:21 +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>
124 lines
2.3 KiB
Go
124 lines
2.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"hash"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Payment struct {
|
|
Id string
|
|
Inputs []Vtxo
|
|
Receivers []Receiver
|
|
}
|
|
|
|
func NewPayment(inputs []Vtxo) (*Payment, error) {
|
|
p := &Payment{
|
|
Id: uuid.New().String(),
|
|
Inputs: inputs,
|
|
}
|
|
if err := p.validate(true); err != nil {
|
|
return nil, err
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (p *Payment) AddReceivers(receivers []Receiver) (err error) {
|
|
if p.Receivers == nil {
|
|
p.Receivers = make([]Receiver, 0)
|
|
}
|
|
p.Receivers = append(p.Receivers, receivers...)
|
|
defer func() {
|
|
if err != nil {
|
|
p.Receivers = p.Receivers[:len(p.Receivers)-len(receivers)]
|
|
}
|
|
}()
|
|
err = p.validate(false)
|
|
return
|
|
}
|
|
|
|
func (p Payment) TotalInputAmount() uint64 {
|
|
tot := uint64(0)
|
|
for _, in := range p.Inputs {
|
|
tot += in.Amount
|
|
}
|
|
return tot
|
|
}
|
|
|
|
func (p Payment) TotalOutputAmount() uint64 {
|
|
tot := uint64(0)
|
|
for _, r := range p.Receivers {
|
|
tot += r.Amount
|
|
}
|
|
return tot
|
|
}
|
|
|
|
func (p Payment) validate(ignoreOuts bool) error {
|
|
if len(p.Id) <= 0 {
|
|
return fmt.Errorf("missing id")
|
|
}
|
|
if ignoreOuts {
|
|
return nil
|
|
}
|
|
|
|
if len(p.Receivers) <= 0 {
|
|
return fmt.Errorf("missing outputs")
|
|
}
|
|
for _, r := range p.Receivers {
|
|
if len(r.OnchainAddress) <= 0 && len(r.Descriptor) <= 0 {
|
|
return fmt.Errorf("missing receiver destination")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type VtxoKey struct {
|
|
Txid string
|
|
VOut uint32
|
|
}
|
|
|
|
func (k VtxoKey) Hash() string {
|
|
calcHash := func(buf []byte, hasher hash.Hash) []byte {
|
|
_, _ = hasher.Write(buf)
|
|
return hasher.Sum(nil)
|
|
}
|
|
|
|
hash160 := func(buf []byte) []byte {
|
|
return calcHash(calcHash(buf, sha256.New()), sha256.New())
|
|
}
|
|
|
|
buf, _ := hex.DecodeString(k.Txid)
|
|
buf = append(buf, byte(k.VOut))
|
|
return hex.EncodeToString(hash160(buf))
|
|
}
|
|
|
|
type Receiver struct {
|
|
Descriptor string
|
|
Amount uint64
|
|
OnchainAddress string
|
|
}
|
|
|
|
func (r Receiver) IsOnchain() bool {
|
|
return len(r.OnchainAddress) > 0
|
|
}
|
|
|
|
type Vtxo struct {
|
|
VtxoKey
|
|
Receiver
|
|
PoolTx string
|
|
SpentBy string // round txid or async redeem txid
|
|
Spent bool
|
|
Redeemed bool
|
|
Swept bool
|
|
ExpireAt int64
|
|
AsyncPayment *AsyncPaymentTxs // nil if not async vtxo
|
|
}
|
|
|
|
type AsyncPaymentTxs struct {
|
|
RedeemTx string // always signed by the ASP when created
|
|
UnconditionalForfeitTxs []string
|
|
}
|