-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add accept sell ord offer + add ulocker to UTXO
- Loading branch information
Showing
11 changed files
with
257 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,126 @@ | ||
package bt | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/libsv/go-bt/v2/bscript" | ||
"github.com/libsv/go-bt/v2/sighash" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func MakeOfferToSellOrdinal(ctx context.Context, sellerOutput *Output, | ||
utxo *UTXO, unlocker Unlocker) (*Tx, error) { | ||
// MakeSellOfferArgs contains the arguments | ||
// needed to make an offer to sell an | ||
// ordinal (PSBT). | ||
type MakeSellOfferArgs struct { | ||
SellerReceiveOutput *Output | ||
OrdinalUTXO *UTXO | ||
OrdinalUnlocker Unlocker | ||
} | ||
|
||
// MakeOfferToSellOrdinal creates a PBST (Partially Signed Bitcoin | ||
// Transaction) that offers a specific ordinal UTXO for sale at a | ||
// specific price. | ||
func MakeOfferToSellOrdinal(ctx context.Context, msoa *MakeSellOfferArgs) (*Tx, error) { | ||
tx := NewTx() | ||
|
||
// add dummy input 0 to balance out tx | ||
// so that the next indices are equal | ||
// for the sighash single as well as | ||
// fo the ordinal theory rules to carry | ||
// over ordinals from input to output | ||
err := tx.From("0000000000000000000000000000000000000000000000000000000000000000", 0, "", 0) | ||
err := tx.FromUTXOs(msoa.OrdinalUTXO) | ||
if err != nil { | ||
return nil, ErrDummyInput | ||
return nil, err | ||
} | ||
|
||
err = tx.FromUTXOs(utxo) | ||
tx.AddOutput(msoa.SellerReceiveOutput) | ||
|
||
err = tx.FillInput(ctx, msoa.OrdinalUnlocker, UnlockerParams{ | ||
InputIdx: 0, | ||
SigHashFlags: sighash.SingleForkID | sighash.AnyOneCanPay, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// add dummy output (to be replaced by buyer/taker) | ||
tx.AddDummyOutput() | ||
return tx, nil | ||
} | ||
|
||
tx.AddOutput(sellerOutput) | ||
// AcceptSellOfferArgs contains the arguments | ||
// needed to make an offer to sell an | ||
// ordinal (PSBT). | ||
type AcceptSellOfferArgs struct { | ||
PSTx *Tx | ||
Utxos []*UTXO | ||
BuyerReceiveOrdinalScript *bscript.Script | ||
DummyOutputScript *bscript.Script | ||
ChangeScript *bscript.Script | ||
FQ *FeeQuote | ||
} | ||
|
||
err = tx.FillInput(ctx, unlocker, UnlockerParams{ | ||
InputIdx: 1, | ||
SigHashFlags: sighash.SingleForkID | sighash.AnyOneCanPay, | ||
// AcceptOfferToSellOrdinal accepts a partially signed Bitcoin | ||
// transaction offer to sell an ordinal. When accepting the offer, | ||
// you will need to provide at least 3 UTXOs - with the first 2 | ||
// being dummy utxos that will just pass through, and the rest with | ||
// the required payment and tx fees. | ||
func AcceptOfferToSellOrdinal(ctx context.Context, asoa *AcceptSellOfferArgs) (*Tx, error) { | ||
|
||
// TODO: ValidateSellOffer() | ||
// check if input 1 sat | ||
|
||
if len(asoa.Utxos) < 3 { | ||
return nil, ErrInsufficientUTXOs | ||
} | ||
|
||
tx := NewTx() | ||
|
||
// add dummy inputs | ||
err := tx.FromUTXOs(asoa.Utxos[0], asoa.Utxos[1]) | ||
if err != nil { | ||
return nil, fmt.Errorf(`failed to add inputs: %w`, err) | ||
} | ||
|
||
sellerOrdinalInput := asoa.PSTx.Inputs[0] // TODO: get from psbt | ||
tx.addInput(sellerOrdinalInput) | ||
|
||
// add payment input(s) | ||
err = tx.FromUTXOs(asoa.Utxos[2:]...) | ||
if err != nil { | ||
return nil, fmt.Errorf(`failed to add inputs: %w`, err) | ||
} | ||
|
||
// add dummy output to passthrough dummy inputs | ||
tx.AddOutput(&Output{ | ||
LockingScript: asoa.DummyOutputScript, | ||
Satoshis: asoa.Utxos[0].Satoshis + asoa.Utxos[1].Satoshis, | ||
}) | ||
|
||
// add ordinal receive output | ||
tx.AddOutput(&Output{ | ||
LockingScript: asoa.BuyerReceiveOrdinalScript, | ||
Satoshis: 1, | ||
}) | ||
|
||
sellerOutput := asoa.PSTx.Outputs[0] // TODO: get from psbt | ||
tx.AddOutput(sellerOutput) | ||
|
||
err = tx.Change(asoa.ChangeScript, asoa.FQ) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, u := range asoa.Utxos { | ||
// skip 3rd input (ordinals input) | ||
j := i | ||
if i >= 2 { | ||
j++ | ||
} | ||
|
||
if !(bytes.Equal(u.TxID, tx.Inputs[j].previousTxID)) { | ||
return nil, errors.New("input and utxo mismatch") // TODO: move to errors.go | ||
} | ||
err = tx.FillInput(context.Background(), *u.Unlocker, UnlockerParams{InputIdx: uint32(j)}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return tx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.