package domain import ( "errors" "fmt" "git.smesh.lol/tinywallet/pkg/bech32" "git.smesh.lol/tinywallet/pkg/script" "git.smesh.lol/tinywallet/pkg/utxo" ) // Errors a fork refuses. var ( // ErrNoFork is a chain whose profile schedules no fork, so there is no // shared pre-fork history to split. ErrNoFork = errors.New("domain: this chain has no scheduled fork to split") // ErrNotPreFork is a coin that is not shared history: only a coin confirmed // at or below the fork height exists on both chains and can be split. ErrNotPreFork = errors.New("domain: a coin is not shared pre-fork history") // ErrNoSuchCoin is an outpoint the wallet does not hold on this chain. ErrNoSuchCoin = errors.New("domain: no such coin") ) // PreForkCoins lists the coins on one chain that predate the fork. // // Those are the split candidates: a coin confirmed at or below ForkHeight exists // on both chains and can be spent here to turn shared Bitcoin into this chain's // own money. The list is the wallet's whole unspent set, not just what a spend // would select, so the operator can see and choose one coin to split. func (d *Domain) PreForkCoins(name string) ([]utxo.Coin, error) { link, err := d.link(name) if err != nil { return nil, err } if link.Profile.ForkHeight == 0 { return nil, ErrNoFork } coins, err := d.UnspentOn(name) if err != nil { return nil, err } shared := make([]utxo.Coin, 0, len(coins)) for _, coin := range coins { if coin.Height > 0 && coin.Height <= link.Profile.ForkHeight { shared = append(shared, coin) } } return shared, nil } // Fork splits one shared pre-fork coin onto the forked chain. // // `destination` is the chain that receives the split, which is always "blake": // the fork only ever turns shared Bitcoin into the forked chain's own money, so // a BTC-side split is meaningless. `outpoint` names the exact coin to split, // `to` is the receiving address (empty derives the wallet's next receive // address), and `fee` is explicit, as in Send. // // The coin's whole value minus the fee is spent: a split moves a coin, it does // not partially change it. The spend is signed SIGHASH_UNIFIED, so it verifies // only on the forked chain, which is what keeps the two halves independent. func (d *Domain) Fork(destination, outpoint, to string, fee uint64, policy SendPolicy) ([32]byte, error) { var none [32]byte if destination != "blake" { return none, errors.New("domain: a split can only go to the forked chain, not " + destination) } link, err := d.link(destination) if err != nil { return none, err } if link.Profile.ForkHeight == 0 { return none, ErrNoFork } derived, err := d.walletOf(destination) if err != nil { return none, err } meta, err := d.meta(destination) if err != nil { return none, err } if to == "" { address, err := derived.Address(false, meta.Receive) if err != nil { return none, err } to = address.Text } hrp, version, program, err := bech32.DecodeSegwit(to) if err != nil { return none, fmt.Errorf("domain: %q: %w", to, err) } if hrp != link.Profile.HRP || version != 0 { return none, fmt.Errorf("domain: %q is not a %s segwit address", to, destination) } var recipient []byte switch len(program) { case 20: recipient = script.P2WPKH([20]byte(program)) case 32: recipient = append([]byte{0x00, 0x20}, program...) default: return none, fmt.Errorf("domain: %q has a %d-byte witness program", to, len(program)) } coins, err := d.UnspentOn(destination) if err != nil { return none, err } var coin *utxo.Coin for index := range coins { if coins[index].Outpoint() == outpoint { coin = &coins[index] break } } if coin == nil { return none, fmt.Errorf("%w: %s", ErrNoSuchCoin, outpoint) } if coin.Height == 0 || coin.Height > link.Profile.ForkHeight { return none, fmt.Errorf("%w: %s at height %d, the fork is at %d", ErrNotPreFork, coin.Outpoint(), coin.Height, link.Profile.ForkHeight) } if coin.Value <= fee { return none, fmt.Errorf("%w: %d sat need %d", ErrNoFunds, coin.Value, fee) } keys, err := d.spendKeys(derived, meta) if err != nil { return none, err } key, ours := keys[string(coin.Script)] if !ours { return none, fmt.Errorf("%w: %s", ErrSendChain, coin.Chain) } amount := coin.Value - fee return d.sendSigned(destination, to, recipient, amount, fee, policy, []signedCoin{{coin: *coin, key: key}}) }