-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbuilder.go
78 lines (66 loc) · 1.7 KB
/
callbuilder.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
package mazzaroth
import (
"crypto/ed25519"
"github.com/kochavalabs/mazzaroth-xdr/go-xdr/xdr"
"github.com/pkg/errors"
)
type CallBuilder struct {
sender *xdr.ID
channel *xdr.ID
nonce uint64
blockExpirationNumber uint64
functionName string
arguments []xdr.Argument
}
// Call
func (cb *CallBuilder) Call(sender, channel *xdr.ID, nonce, blockExpirationNumber uint64) *CallBuilder {
cb.sender = sender
cb.channel = channel
cb.nonce = nonce
cb.blockExpirationNumber = blockExpirationNumber
return cb
}
// Function
func (cb *CallBuilder) Function(name string) *CallBuilder {
cb.functionName = name
return cb
}
// Arguments
func (cb *CallBuilder) Arguments(arguments ...xdr.Argument) *CallBuilder {
cb.arguments = arguments
return cb
}
// Sign
func (cb *CallBuilder) Sign(pk ed25519.PrivateKey) (*xdr.Transaction, error) {
// check required values
if len(cb.functionName) <= 0 {
return nil, ErrEmptyFunctionName
}
data := xdr.Data{
ChannelID: *cb.channel,
Nonce: cb.nonce,
BlockExpirationNumber: cb.blockExpirationNumber,
Category: xdr.Category{
Type: xdr.CategoryTypeCALL,
Call: &xdr.Call{
Function: cb.functionName,
Arguments: cb.arguments,
},
},
}
dataBytes, err := data.MarshalBinary()
if err != nil {
return nil, errors.Wrap(err, "in data.MarshalBinary")
}
signatureSlice := ed25519.Sign(pk, dataBytes)
signature, err := xdr.SignatureFromSlice(signatureSlice)
if err != nil {
return nil, errors.Wrap(err, "in signing the transaction")
}
transaction := &xdr.Transaction{
Sender: *cb.sender,
Signature: signature,
Data: data,
}
return transaction, nil
}