Documentation ¶
Overview ¶
Package tredd implements trustless escrow for digital data, as described at https://docs.google.com/document/d/1eC36V8fX9AVXJDNx1qiCksAj03C9DOp9YCWzIC2wKJE/edit?usp=sharing.
Index ¶
- Constants
- func ClaimPayment(r *Redeem) ([]byte, error)
- func ClaimRefund(r *Redeem, index int64, cipherChunk []byte, clearHash []byte, ...) ([]byte, error)
- func Crypt(key [32]byte, chunk []byte, index uint64)
- func Decrypt(w io.Writer, clearHashes, cipherChunks ChunkStore, key [32]byte) error
- func Get(r io.Reader, clearRoot [32]byte, clearHashes, cipherChunks ChunkStore) ([]byte, error)
- func ProposePayment(ctx context.Context, buyer ed25519.PublicKey, amount int64, assetID bc.Hash, ...) ([]byte, error)
- func RevealKey(ctx context.Context, paymentProposal []byte, seller ed25519.PublicKey, ...) ([]byte, error)
- func Serve(w io.Writer, r io.Reader, key [32]byte) ([]byte, error)
- type BadClearHashError
- type ChunkStore
- type ParseResult
- type Redeem
- type Reservation
- type Reserver
- type Signer
- type UTXO
Constants ¶
const ChunkSize = 8192
ChunkSize is the size of a chunk of Tredd data.
Variables ¶
This section is empty.
Functions ¶
func ClaimPayment ¶
ClaimPayment constructs a seller-claims-payment transaction, rehydrating and invoking a Tredd contract from the utxo state (identified by the information in r).
func ClaimRefund ¶
func ClaimRefund(r *Redeem, index int64, cipherChunk []byte, clearHash []byte, cipherProof, clearProof merkle.Proof) ([]byte, error)
ClaimRefund constructs a buyer-claims-refund transaction, rehydrating a Tredd contract from the utxo state (identified by the information in r) and calling it with the necessary proofs and other information.
func Decrypt ¶
func Decrypt(w io.Writer, clearHashes, cipherChunks ChunkStore, key [32]byte) error
Decrypt decrypts the chunks in cipherChunks by xoring with hashes derived from key. It writes the concatenated cleartext chunks to w. Along the way, it compares each cleartext chunk's hash to the corresponding value in clearHashes. If it finds a mismatch, it returns a BadClearHashError.
func Get ¶
Get parses a stream of interleaved <clearhash><cipherchunk> pairs, placing them in their respective ChunkStores. Along the way it compares the clear hashes' Merkle root hash to the expected value in clearRoot. If it finds a mismatch it returns errBadClearRoot. If there is no error, the Merkle root hash of the cipher chunks is returned. Both Merkle root hashes are computed from values prepended with the chunk index number.
func ProposePayment ¶
func ProposePayment( ctx context.Context, buyer ed25519.PublicKey, amount int64, assetID bc.Hash, clearRoot, cipherRoot [32]byte, now, revealDeadline, refundDeadline time.Time, reserver Reserver, signer Signer, ) ([]byte, error)
ProposePayment constructs a partial transaction in which the buyer commits funds to the Tredd contract.
func RevealKey ¶
func RevealKey( ctx context.Context, paymentProposal []byte, seller ed25519.PublicKey, key [32]byte, amount int64, assetID bc.Hash, reserver Reserver, signer Signer, wantClearRoot, wantCipherRoot [32]byte, now, wantRevealDeadline, wantRefundDeadline time.Time, ) ([]byte, error)
RevealKey completes the partial transaction in paymentProposal (which came from ProposePayment). The Tredd contract is on the contract stack. The arg stack is empty.
func Serve ¶
Serve produces a stream of interleaved <clearhash><cipherchunk> pairs from the content in r. It writes the stream to w, encrypting the chunks by xoring with hashes derived from key. The return value is the Merkle root hash of the cipher chunks, each prepended with its chunk index. TODO: Cleartext chunks and their hashes can be precomputed and supplied as ChunkStores.
Types ¶
type BadClearHashError ¶
type BadClearHashError struct { // Index is the index of the chunk and of the hash within their respective ChunkStores. Index uint64 }
BadClearHashError gives the index of a cleartext chunk whose hash doesn't have the expected value.
func (BadClearHashError) Error ¶
func (e BadClearHashError) Error() string
type ChunkStore ¶
type ChunkStore interface { // Add adds a chunk to the end of the ChunkStore. Add([]byte) error // Get gets the chunk with the given index (0-based). Get(uint64) ([]byte, error) // Len tells the number of chunks in the store. Len() (int64, error) }
ChunkStore stores and retrieves data in chunks. The chunk size need not be ChunkSize.
type ParseResult ¶
type ParseResult struct { // Amount is the amount of the buyer's payment (not including the seller's collateral). Amount int64 AssetID []byte // Anchor1 is the anchor in the Value tuple of the buyer's payment. Anchor1 []byte // Anchor2 is the anchor in the Value tuple of the buyer's payment after merging with the seller's collateral. Anchor2 []byte ClearRoot []byte CipherRoot []byte RevealDeadline time.Time RefundDeadline time.Time Buyer ed25519.PublicKey Seller ed25519.PublicKey Key []byte // OutputID is the id of the Tredd contract UTXO while awaiting redemption. OutputID []byte }
ParseResult holds the values parsed from the log of a transaction that invokes the propose-payment phase of a Tredd contract. If the transaction is complete (i.e., the seller has added the "reveal-key" call), all of the fields will be filled in. If the transaction is partial, some fields will be uninitialized.
func ParseLog ¶
func ParseLog(prog []byte) *ParseResult
ParseLog parses the log of a (possibly partial) transaction program. If the log shows a call to an instance of the Tredd contract, ParseLog returns a ParseResult containing information extracted from the log. Otherwise it returns nil.
type Redeem ¶
type Redeem struct { RefundDeadline time.Time Buyer, Seller ed25519.PublicKey // Amount is the sum of the payment and the collateral (i.e., the buyer's payment times 2). Amount int64 AssetID bc.Hash // Anchor2 is the anchor of the Value tuple holding the payment+collateral. Anchor2 [32]byte CipherRoot, ClearRoot [32]byte Key [32]byte }
Redeem holds the values needed to redeem a Tredd contract (whether by the seller claiming payment or the buyer claiming a refund).
type Reservation ¶
type Reservation interface { UTXOs(context.Context) ([]UTXO, error) Change(context.Context) (int64, error) Cancel(context.Context) error }
Reservation is the result of reserving some UTXOs with a Reserver.
type Reserver ¶
type Reserver interface {
Reserve(ctx context.Context, amount int64, assetID bc.Hash, now, exp time.Time) (Reservation, error)
}
Reserver can reserve UTXOs for spending before a given expiration time. A UTXO, once reserved, will not appear in another Reservation until/unless the first reservation expires or is canceled.