fork.go raw

   1  package domain
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  
   7  	"git.smesh.lol/tinywallet/pkg/bech32"
   8  	"git.smesh.lol/tinywallet/pkg/script"
   9  	"git.smesh.lol/tinywallet/pkg/utxo"
  10  )
  11  
  12  // Errors a fork refuses.
  13  var (
  14  	// ErrNoFork is a chain whose profile schedules no fork, so there is no
  15  	// shared pre-fork history to split.
  16  	ErrNoFork = errors.New("domain: this chain has no scheduled fork to split")
  17  	// ErrNotPreFork is a coin that is not shared history: only a coin confirmed
  18  	// at or below the fork height exists on both chains and can be split.
  19  	ErrNotPreFork = errors.New("domain: a coin is not shared pre-fork history")
  20  	// ErrNoSuchCoin is an outpoint the wallet does not hold on this chain.
  21  	ErrNoSuchCoin = errors.New("domain: no such coin")
  22  )
  23  
  24  // PreForkCoins lists the coins on one chain that predate the fork.
  25  //
  26  // Those are the split candidates: a coin confirmed at or below ForkHeight exists
  27  // on both chains and can be spent here to turn shared Bitcoin into this chain's
  28  // own money. The list is the wallet's whole unspent set, not just what a spend
  29  // would select, so the operator can see and choose one coin to split.
  30  func (d *Domain) PreForkCoins(name string) ([]utxo.Coin, error) {
  31  	link, err := d.link(name)
  32  
  33  	if err != nil {
  34  		return nil, err
  35  	}
  36  
  37  	if link.Profile.ForkHeight == 0 {
  38  		return nil, ErrNoFork
  39  	}
  40  
  41  	coins, err := d.UnspentOn(name)
  42  
  43  	if err != nil {
  44  		return nil, err
  45  	}
  46  
  47  	shared := make([]utxo.Coin, 0, len(coins))
  48  
  49  	for _, coin := range coins {
  50  		if coin.Height > 0 && coin.Height <= link.Profile.ForkHeight {
  51  			shared = append(shared, coin)
  52  		}
  53  	}
  54  
  55  	return shared, nil
  56  }
  57  
  58  // Fork splits one shared pre-fork coin onto the forked chain.
  59  //
  60  // `destination` is the chain that receives the split, which is always "blake":
  61  // the fork only ever turns shared Bitcoin into the forked chain's own money, so
  62  // a BTC-side split is meaningless. `outpoint` names the exact coin to split,
  63  // `to` is the receiving address (empty derives the wallet's next receive
  64  // address), and `fee` is explicit, as in Send.
  65  //
  66  // The coin's whole value minus the fee is spent: a split moves a coin, it does
  67  // not partially change it. The spend is signed SIGHASH_UNIFIED, so it verifies
  68  // only on the forked chain, which is what keeps the two halves independent.
  69  func (d *Domain) Fork(destination, outpoint, to string, fee uint64, policy SendPolicy) ([32]byte, error) {
  70  	var none [32]byte
  71  
  72  	if destination != "blake" {
  73  		return none, errors.New("domain: a split can only go to the forked chain, not " + destination)
  74  	}
  75  
  76  	link, err := d.link(destination)
  77  
  78  	if err != nil {
  79  		return none, err
  80  	}
  81  
  82  	if link.Profile.ForkHeight == 0 {
  83  		return none, ErrNoFork
  84  	}
  85  
  86  	derived, err := d.walletOf(destination)
  87  
  88  	if err != nil {
  89  		return none, err
  90  	}
  91  
  92  	meta, err := d.meta(destination)
  93  
  94  	if err != nil {
  95  		return none, err
  96  	}
  97  
  98  	if to == "" {
  99  		address, err := derived.Address(false, meta.Receive)
 100  
 101  		if err != nil {
 102  			return none, err
 103  		}
 104  
 105  		to = address.Text
 106  	}
 107  
 108  	hrp, version, program, err := bech32.DecodeSegwit(to)
 109  
 110  	if err != nil {
 111  		return none, fmt.Errorf("domain: %q: %w", to, err)
 112  	}
 113  
 114  	if hrp != link.Profile.HRP || version != 0 {
 115  		return none, fmt.Errorf("domain: %q is not a %s segwit address", to, destination)
 116  	}
 117  
 118  	var recipient []byte
 119  
 120  	switch len(program) {
 121  	case 20:
 122  		recipient = script.P2WPKH([20]byte(program))
 123  	case 32:
 124  		recipient = append([]byte{0x00, 0x20}, program...)
 125  	default:
 126  		return none, fmt.Errorf("domain: %q has a %d-byte witness program", to, len(program))
 127  	}
 128  
 129  	coins, err := d.UnspentOn(destination)
 130  
 131  	if err != nil {
 132  		return none, err
 133  	}
 134  
 135  	var coin *utxo.Coin
 136  
 137  	for index := range coins {
 138  		if coins[index].Outpoint() == outpoint {
 139  			coin = &coins[index]
 140  
 141  			break
 142  		}
 143  	}
 144  
 145  	if coin == nil {
 146  		return none, fmt.Errorf("%w: %s", ErrNoSuchCoin, outpoint)
 147  	}
 148  
 149  	if coin.Height == 0 || coin.Height > link.Profile.ForkHeight {
 150  		return none, fmt.Errorf("%w: %s at height %d, the fork is at %d",
 151  			ErrNotPreFork, coin.Outpoint(), coin.Height, link.Profile.ForkHeight)
 152  	}
 153  
 154  	if coin.Value <= fee {
 155  		return none, fmt.Errorf("%w: %d sat need %d", ErrNoFunds, coin.Value, fee)
 156  	}
 157  
 158  	keys, err := d.spendKeys(derived, meta)
 159  
 160  	if err != nil {
 161  		return none, err
 162  	}
 163  
 164  	key, ours := keys[string(coin.Script)]
 165  
 166  	if !ours {
 167  		return none, fmt.Errorf("%w: %s", ErrSendChain, coin.Chain)
 168  	}
 169  
 170  	amount := coin.Value - fee
 171  
 172  	return d.sendSigned(destination, to, recipient, amount, fee, policy,
 173  		[]signedCoin{{coin: *coin, key: key}})
 174  }
 175