/
builder.go
43 lines (35 loc) · 1.17 KB
/
builder.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
package bip70
import (
"github.com/bip70/bip70-go/pkg/protobuf/payments"
"github.com/golang/protobuf/proto"
)
// Builder is an interface designed for completing
// a payment request. It receives PaymentDetails, and
// renders it into a final PaymentRequest object.
type Builder interface {
Build(details *payments.PaymentDetails) (*payments.PaymentRequest, error)
}
// NewUnsignedBuilder returns an UnsignedBuilder.
func NewUnsignedBuilder() (*UnsignedBuilder, error) {
return &UnsignedBuilder{}, nil
}
// UnsignedBuilder implements the Builder interface,
// and works by embedding the payment details into
// a request, without a signature (PkiTypeNone)
type UnsignedBuilder struct {
}
// Build takes the details struct and converts it to
// a payment request, or returns an error on failure.
func (c *UnsignedBuilder) Build(details *payments.PaymentDetails) (*payments.PaymentRequest, error) {
detailsBin, err := proto.Marshal(details)
if err != nil {
return nil, err
}
version := uint32(1)
pkiType := PkiTypeNone
req := &payments.PaymentRequest{}
req.PaymentDetailsVersion = &version
req.SerializedPaymentDetails = detailsBin
req.PkiType = &pkiType
return req, nil
}