|
| 1 | +package bsvalias |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + "github.com/tokenized/pkg/bitcoin" |
| 9 | + "github.com/tokenized/pkg/wire" |
| 10 | +) |
| 11 | + |
| 12 | +// Capabilities contains the information about the endpoints supported by the bsvalias host. |
| 13 | +type Capabilities struct { |
| 14 | + Version string `json:"bsvalias"` |
| 15 | + Capabilities map[string]interface{} `json:"capabilities"` |
| 16 | +} |
| 17 | + |
| 18 | +// Site represents a bsvalias host. |
| 19 | +type Site struct { |
| 20 | + Capabilities Capabilities |
| 21 | + URL string `json:"url"` |
| 22 | +} |
| 23 | + |
| 24 | +// Identity represents a paymail/bsvalias handle and the data retrieved for the host. |
| 25 | +type Identity struct { |
| 26 | + Handle string |
| 27 | + Site Site |
| 28 | + Alias string |
| 29 | + Hostname string |
| 30 | +} |
| 31 | + |
| 32 | +// PublicKeyResponse is the raw response from a PublicKey endpoint. |
| 33 | +type PublicKeyResponse struct { |
| 34 | + Version string `json:"bsvalias"` |
| 35 | + Handle string `json:"handle"` |
| 36 | + PublicKey string `json:"pubkey"` |
| 37 | +} |
| 38 | + |
| 39 | +// PaymentDestinationRequest is the data structure sent to request a payment destination. |
| 40 | +type PaymentDestinationRequest struct { |
| 41 | + SenderName string `json:"senderName"` |
| 42 | + SenderHandle string `json:"senderHandle"` |
| 43 | + DateTime string `json:"dt"` |
| 44 | + Amount uint64 `json:"amount"` |
| 45 | + Purpose string `json:"purpose"` |
| 46 | + Signature string `json:"signature"` |
| 47 | +} |
| 48 | + |
| 49 | +// Sign adds a signature to the request. The key should correspond to the sender handle's PKI. |
| 50 | +func (r *PaymentDestinationRequest) Sign(key bitcoin.Key) error { |
| 51 | + sigHash, err := SignatureHashForMessage(r.SenderHandle + strconv.FormatUint(r.Amount, 10) + |
| 52 | + r.DateTime + r.Purpose) |
| 53 | + if err != nil { |
| 54 | + return errors.Wrap(err, "signature hash") |
| 55 | + } |
| 56 | + |
| 57 | + sig, err := key.Sign(sigHash.Bytes()) |
| 58 | + if err != nil { |
| 59 | + return errors.Wrap(err, "sign") |
| 60 | + } |
| 61 | + |
| 62 | + r.Signature = sig.ToCompact() |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (r PaymentDestinationRequest) CheckSignature(publicKey bitcoin.PublicKey) error { |
| 67 | + sigHash, err := SignatureHashForMessage(r.SenderHandle + strconv.FormatUint(r.Amount, 10) + |
| 68 | + r.DateTime + r.Purpose) |
| 69 | + if err != nil { |
| 70 | + return errors.Wrap(err, "signature hash") |
| 71 | + } |
| 72 | + |
| 73 | + sig, err := bitcoin.SignatureFromCompact(r.Signature) |
| 74 | + if err != nil { |
| 75 | + return errors.Wrap(err, fmt.Sprintf("parse signature: %s", r.Signature)) |
| 76 | + } |
| 77 | + |
| 78 | + if !sig.Verify(sigHash.Bytes(), publicKey) { |
| 79 | + return ErrInvalidSignature |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// PaymentDestinationResponse is the raw response from a PaymentDestination endpoint. |
| 86 | +type PaymentDestinationResponse struct { |
| 87 | + Output string `json:"output"` |
| 88 | +} |
| 89 | + |
| 90 | +// PaymentRequestRequest is the data structure sent to request a payment request. |
| 91 | +type PaymentRequestRequest struct { |
| 92 | + SenderName string `json:"senderName"` |
| 93 | + SenderHandle string `json:"senderHandle"` |
| 94 | + DateTime string `json:"dt"` |
| 95 | + AssetID string `json:"assetID"` |
| 96 | + Amount uint64 `json:"amount"` |
| 97 | + Purpose string `json:"purpose"` |
| 98 | + Signature string `json:"signature"` |
| 99 | +} |
| 100 | + |
| 101 | +// Sign adds a signature to the request. The key should correspond to the sender handle's PKI. |
| 102 | +func (r *PaymentRequestRequest) Sign(key bitcoin.Key) error { |
| 103 | + sigHash, err := SignatureHashForMessage(r.SenderHandle + r.AssetID + |
| 104 | + strconv.FormatUint(r.Amount, 10) + r.DateTime + r.Purpose) |
| 105 | + if err != nil { |
| 106 | + return errors.Wrap(err, "signature hash") |
| 107 | + } |
| 108 | + |
| 109 | + sig, err := key.Sign(sigHash.Bytes()) |
| 110 | + if err != nil { |
| 111 | + return errors.Wrap(err, "sign") |
| 112 | + } |
| 113 | + |
| 114 | + r.Signature = sig.ToCompact() |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func (r PaymentRequestRequest) CheckSignature(publicKey bitcoin.PublicKey) error { |
| 119 | + sigHash, err := SignatureHashForMessage(r.SenderHandle + r.AssetID + |
| 120 | + strconv.FormatUint(r.Amount, 10) + r.DateTime + r.Purpose) |
| 121 | + if err != nil { |
| 122 | + return errors.Wrap(err, "signature hash") |
| 123 | + } |
| 124 | + |
| 125 | + sig, err := bitcoin.SignatureFromCompact(r.Signature) |
| 126 | + if err != nil { |
| 127 | + return errors.Wrap(err, fmt.Sprintf("parse signature: %s", r.Signature)) |
| 128 | + } |
| 129 | + |
| 130 | + if !sig.Verify(sigHash.Bytes(), publicKey) { |
| 131 | + return ErrInvalidSignature |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// PaymentRequestResponse is the raw response from a PaymentRequest endpoint. |
| 138 | +type PaymentRequestResponse struct { |
| 139 | + PaymentRequest string `json:"paymentRequest"` |
| 140 | + Outputs []string `json:"outputs"` |
| 141 | +} |
| 142 | + |
| 143 | +// PaymentRequest is the processed response from a PaymentRequest endpoint. |
| 144 | +type PaymentRequest struct { |
| 145 | + Tx wire.MsgTx |
| 146 | + Outputs []wire.TxOut |
| 147 | +} |
0 commit comments