forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder_manage_data.go
52 lines (40 loc) · 1.32 KB
/
builder_manage_data.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
package bridge
import (
"encoding/base64"
shared "github.com/stellar/go/services/internal/bridge-compliance-shared"
"github.com/stellar/go/services/internal/bridge-compliance-shared/http/helpers"
"github.com/stellar/go/txnbuild"
)
// ManageDataOperationBody represents manage_data operation
type ManageDataOperationBody struct {
Source *string
Name string
Data string
}
// Build returns a txnbuild.Operation
func (op ManageDataOperationBody) Build() txnbuild.Operation {
// This is validated in Validate()
data, _ := base64.StdEncoding.DecodeString(op.Data)
txnOp := txnbuild.ManageData{
Name: op.Name,
Value: data,
}
if op.Source != nil {
txnOp.SourceAccount = &txnbuild.SimpleAccount{AccountID: *op.Source}
}
return &txnOp
}
// Validate validates if operation body is valid.
func (op ManageDataOperationBody) Validate() error {
if len(op.Name) > 64 {
return helpers.NewInvalidParameterError("name", "Name must be less than or equal 64 characters.")
}
data, err := base64.StdEncoding.DecodeString(op.Data)
if err != nil || len(data) > 64 {
return helpers.NewInvalidParameterError("data", "Not a valid base64 string.")
}
if op.Source != nil && !shared.IsValidAccountID(*op.Source) {
return helpers.NewInvalidParameterError("source", "Source must be a public key (starting with `G`).")
}
return nil
}