generated from bitcoin-sv/template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package chaintracker | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bitcoin-sv/go-sdk/chainhash" | ||
) | ||
|
||
type Network string | ||
|
||
type BlockHeader struct { | ||
Hash *chainhash.Hash `json:"hash"` | ||
Height uint32 `json:"height"` | ||
Version uint32 `json:"version"` | ||
MerkleRoot *chainhash.Hash `json:"merkleroot"` | ||
Time uint32 `json:"time"` | ||
Nonce uint32 `json:"nonce"` | ||
Bits string `json:"bits"` | ||
PrevHash *chainhash.Hash `json:"previousblockhash"` | ||
} | ||
|
||
var ( | ||
MainNet Network = "main" | ||
TestNet Network = "test" | ||
) | ||
|
||
type WhatsOnChain struct { | ||
Network Network | ||
ApiKey string | ||
} | ||
|
||
func NewWhatsOnChain(network Network, apiKey string) *WhatsOnChain { | ||
return &WhatsOnChain{ | ||
Network: network, | ||
ApiKey: apiKey, | ||
} | ||
} | ||
|
||
func (w *WhatsOnChain) GetBlockHeader(height uint32) (*BlockHeader, error) { | ||
if req, err := http.NewRequest("GET", fmt.Sprintf("https://api.whatsonchain.com/v1/bsv/%s/block/%d/header", w.Network, height), bytes.NewBuffer([]byte{})); err != nil { | ||
return nil, err | ||
} else { | ||
req.Header.Set("Authorization", w.ApiKey) | ||
if resp, err := http.DefaultClient.Do(req); err != nil { | ||
return nil, err | ||
} else { | ||
defer resp.Body.Close() | ||
if resp.StatusCode == 404 { | ||
return nil, nil | ||
} | ||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("failed to verify merkleroot for height %d because of an error: %v", height, resp.Status) | ||
} | ||
header := &BlockHeader{} | ||
if err := json.NewDecoder(resp.Body).Decode(header); err != nil { | ||
return nil, err | ||
} | ||
|
||
return header, nil | ||
} | ||
} | ||
} | ||
|
||
func (w *WhatsOnChain) IsValidRootForHeight(root *chainhash.Hash, height uint32) (bool, error) { | ||
if header, err := w.GetBlockHeader(height); err != nil { | ||
return false, err | ||
} else { | ||
return header.MerkleRoot.IsEqual(root), 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
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package verify | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bitcoin-sv/go-sdk/script/interpreter" | ||
"github.com/bitcoin-sv/go-sdk/transaction" | ||
"github.com/bitcoin-sv/go-sdk/transaction/chaintracker" | ||
) | ||
|
||
func Verify(t *transaction.Transaction, chainTracker chaintracker.ChainTracker, feeModel transaction.FeeModel) (bool, error) { | ||
verifiedTxids := make(map[string]struct{}) | ||
txQueue := []*transaction.Transaction{t} | ||
if chainTracker == nil { | ||
chainTracker = chaintracker.NewWhatsOnChain(chaintracker.MainNet, "") | ||
} | ||
|
||
for len(txQueue) > 0 { | ||
tx := txQueue[0] | ||
txQueue = txQueue[1:] | ||
txid := tx.TxID() | ||
txidStr := txid.String() | ||
|
||
if _, ok := verifiedTxids[txidStr]; ok { | ||
continue | ||
} | ||
|
||
if tx.MerklePath != nil { | ||
if isValid, err := tx.MerklePath.Verify(txid, chainTracker); err != nil { | ||
return false, err | ||
} else if isValid { | ||
verifiedTxids[txidStr] = struct{}{} | ||
} | ||
} | ||
|
||
if feeModel != nil { | ||
clone := tx.ShallowClone() | ||
clone.Outputs[0].Change = true | ||
if err := clone.Fee(feeModel, transaction.ChangeDistributionEqual); err != nil { | ||
return false, err | ||
} | ||
tx.TotalOutputSatoshis() | ||
if txFee, err := tx.GetFee(); err != nil { | ||
return false, err | ||
} else if cloneFee, err := clone.GetFee(); err != nil { | ||
return false, err | ||
} else if txFee < cloneFee { | ||
return false, fmt.Errorf("fee is too low") | ||
} | ||
} | ||
|
||
inputTotal := uint64(0) | ||
for vin, input := range tx.Inputs { | ||
if input.SourceTransaction == nil { | ||
return false, fmt.Errorf("input %d has no source transaction", vin) | ||
} | ||
if input.UnlockingScript == nil || len(*input.UnlockingScript) == 0 { | ||
return false, fmt.Errorf("input %d has no unlocking script", vin) | ||
} | ||
sourceOutput := input.SourceTransaction.Outputs[input.SourceTxOutIndex] | ||
inputTotal += sourceOutput.Satoshis | ||
sourceTxid := input.SourceTransaction.TxID().String() | ||
if _, ok := verifiedTxids[sourceTxid]; !ok { | ||
txQueue = append(txQueue, input.SourceTransaction) | ||
} | ||
|
||
otherInputs := make([]*transaction.TransactionInput, 0, len(tx.Inputs)-1) | ||
for i, otherInput := range tx.Inputs { | ||
if i != vin { | ||
otherInputs = append(otherInputs, otherInput) | ||
} | ||
} | ||
|
||
if input.SourceTXID == nil { | ||
input.SourceTXID = input.SourceTransaction.TxID() | ||
} | ||
|
||
if err := interpreter.NewEngine().Execute( | ||
interpreter.WithTx(tx, vin, sourceOutput), | ||
interpreter.WithForkID(), | ||
interpreter.WithAfterGenesis(), | ||
); err != nil { | ||
fmt.Println(err) | ||
return false, err | ||
} | ||
|
||
} | ||
} | ||
|
||
return true, nil | ||
} |