-
Notifications
You must be signed in to change notification settings - Fork 84
/
common.go
140 lines (128 loc) · 5.33 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package asset
import (
"context"
"fmt"
"time"
"decred.org/dcrdex/dex"
)
// CoinNotFoundError is to be returned from Contract, Redemption, and
// FundingCoin when the specified transaction cannot be found. Used by the
// server to handle network latency.
const (
CoinNotFoundError = dex.ErrorKind("coin not found")
ErrRequestTimeout = dex.ErrorKind("request timeout")
)
// The Backend interface is an interface for a blockchain backend.
type Backend interface {
dex.Connector
// Contract returns a Contract only for outputs that would be spendable on
// the blockchain immediately. The redeem script is required in order to
// calculate sigScript length and verify pubkeys.
Contract(coinID []byte, redeemScript []byte) (*Contract, error)
// TxData fetches the raw transaction data for the specified coin.
TxData(coinID []byte) ([]byte, error)
// ValidateSecret checks that the secret satisfies the contract.
ValidateSecret(secret, contract []byte) bool
// Redemption returns a Coin for redemptionID, a transaction input, that
// spends contract ID, an output containing the swap contract.
Redemption(redemptionID, contractID []byte) (Coin, error)
// FundingCoin returns the unspent coin at the specified location. Coins
// with non-standard pkScripts or scripts that require zero signatures to
// redeem must return an error.
FundingCoin(ctx context.Context, coinID []byte, redeemScript []byte) (FundingCoin, error)
// BlockChannel creates and returns a new channel on which to receive updates
// when new blocks are connected.
BlockChannel(size int) <-chan *BlockUpdate
// InitTxSize is the size of a serialized atomic swap initialization
// transaction with 1 input spending a P2PKH utxo, 1 swap contract output and
// 1 change output.
InitTxSize() uint32
// InitTxSizeBase is InitTxSize not including an input.
InitTxSizeBase() uint32
// CheckAddress checks that the given address is parseable.
CheckAddress(string) bool
// ValidateCoinID checks the coinID to ensure it can be decoded, returning a
// human-readable string if it is valid.
ValidateCoinID(coinID []byte) (string, error)
// ValidateContract ensures that the swap contract is constructed properly
// for the asset.
ValidateContract(contract []byte) error
// VerifyUnspentCoin attempts to verify a coin ID by decoding the coin ID
// and retrieving the corresponding Coin. If the coin is not found or no
// longer unspent, an asset.CoinNotFoundError is returned. Use FundingCoin
// for more UTXO data.
VerifyUnspentCoin(ctx context.Context, coinID []byte) error
// FeeRate returns the current optimal fee rate in atoms / byte.
FeeRate() (uint64, error)
// Synced should return true when the blockchain is synced and ready for
// fee rate estimation.
Synced() (bool, error)
}
// Coin represents a transaction input or output.
type Coin interface {
// Confirmations returns the number of confirmations for a Coin's
// transaction. Because a Coin can become invalid after once being
// considered valid, this condition should be checked for during
// confirmation counting and an error returned if this Coin is no longer
// ready to spend. An unmined transaction should have zero confirmations. A
// transaction in the current best block should have one confirmation. A
// negative number can be returned if error is not nil.
Confirmations(context.Context) (int64, error)
// ID is the coin ID.
ID() []byte
// TxID is a transaction identifier for the coin.
TxID() string
// String is a human readable representation of the Coin.
String() string
// Value is the coin value.
Value() uint64
// FeeRate returns the transaction fee rate, in atoms/byte equivalent.
FeeRate() uint64
}
// FundingCoin is some unspent value on the blockchain.
type FundingCoin interface {
Coin
// Auth checks that the owner of the provided pubkeys can spend the
// FundingCoin. The signatures (sigs) generated with the private keys
// corresponding to pubkeys must validate against the pubkeys and signing
// message (msg).
Auth(pubkeys, sigs [][]byte, msg []byte) error
// SpendSize returns the size of the serialized input that spends this
// FundingCoin.
SpendSize() uint32
}
// Contract is an atomic swap contract.
type Contract struct {
Coin
// SwapAddress is the receiving address of the swap contract.
SwapAddress string
// RedeemScript is the contract redeem script.
RedeemScript []byte
// SecretHash is the secret key hash used for this swap. This is extracted
// from the RedeemScript and is provided here for convenience.
SecretHash []byte
// LockTime is the refund locktime.
LockTime time.Time
// TxData is raw transaction data. This data is provided for some assets
// to aid in SPV compatiblity.
TxData []byte
}
// BlockUpdate is sent over the update channel when a tip change is detected.
type BlockUpdate struct {
Err error
Reorg bool
}
// ConnectionError error should be sent over the block update channel if a
// connection error is detected by the Backend.
type ConnectionError error
// NewConnectionError is a constructor for a ConnectionError.
func NewConnectionError(s string, a ...interface{}) ConnectionError {
return ConnectionError(fmt.Errorf(s, a...))
}
// BackedAsset is a dex.Asset with a Backend.
type BackedAsset struct {
dex.Asset
Backend Backend
}