-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from libsv/feature/spv
- Loading branch information
Showing
19 changed files
with
2,299 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package bc | ||
|
||
// MapiCallback is the body contents posted to the provided callback url from Merchant API | ||
type MapiCallback struct { | ||
CallbackPayload string `json:"callbackPayload"` | ||
APIVersion string `json:"apiVersion"` | ||
Timestamp string `json:"timestamp"` | ||
MinerID string `json:"minerId"` | ||
BlockHash string `json:"blockHash"` | ||
BlockHeight uint64 `json:"blockHeight"` | ||
CallbackTxID string `json:"callbackTxId"` | ||
CallbackReason string `json:"callbackReason"` | ||
} |
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,68 @@ | ||
package spv | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/libsv/go-bt/v2" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// CreateEnvelope builds and returns an spv.Envelope for the provided tx. | ||
func (c *creator) CreateEnvelope(ctx context.Context, tx *bt.Tx) (*Envelope, error) { | ||
if len(tx.Inputs) == 0 { | ||
return nil, ErrNoTxInputs | ||
} | ||
|
||
envelope := &Envelope{ | ||
TxID: tx.TxID(), | ||
RawTx: tx.String(), | ||
Parents: make(map[string]*Envelope), | ||
} | ||
|
||
for _, input := range tx.Inputs { | ||
pTxID := input.PreviousTxIDStr() | ||
|
||
// If we already have added the tx to the parent envelope, there's no point in | ||
// redoing the same work | ||
if _, ok := envelope.Parents[pTxID]; ok { | ||
continue | ||
} | ||
|
||
// Check the store for a Merkle Proof for the current input. | ||
mp, err := c.mpc.MerkleProof(ctx, pTxID) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to get merkle proof for tx %s", pTxID) | ||
} | ||
if mp != nil { | ||
// If a Merkle Proof exists, build and return an spv.Envelope | ||
envelope.Parents[pTxID] = &Envelope{ | ||
TxID: pTxID, | ||
Proof: mp, | ||
} | ||
|
||
// Skip getting the tx data as we have everything we need for verifying the current tx. | ||
continue | ||
} | ||
|
||
// If no merkle proof was found for the input, build a *bt.Tx from its TxID and recursively | ||
// call this function building envelopes for inputs without proofs, until a parent with a | ||
// Merkle Proof is found. | ||
pTx, err := c.txc.Tx(ctx, pTxID) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to get tx %s", pTxID) | ||
} | ||
if pTx == nil { | ||
return nil, fmt.Errorf("could not find tx %s", pTxID) | ||
} | ||
|
||
pEnvelope, err := c.CreateEnvelope(ctx, pTx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
envelope.Parents[pTxID] = pEnvelope | ||
} | ||
|
||
return envelope, nil | ||
} |
Oops, something went wrong.