-
Notifications
You must be signed in to change notification settings - Fork 13
/
asset.go
130 lines (103 loc) · 3.14 KB
/
asset.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpccalls
import (
"fmt"
"golang.org/x/crypto/ed25519"
"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/rpc/assets"
"github.com/bitmark-inc/bitmarkd/rpc/bitmarks"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
)
// AssetData - asset data for bitmark creation
type AssetData struct {
Name string
Metadata string
Quantity int
Registrant *configuration.Private
Fingerprint string
}
// AssetResult - result of an asset get request
type AssetResult struct {
AssetId *transactionrecord.AssetIdentifier
Confirmed bool
}
// MakeAsset - build a properly signed asset
func (client *Client) MakeAsset(assetConfig *AssetData) (*AssetResult, error) {
result := &AssetResult{}
getArgs := assets.GetArguments{
Fingerprints: []string{assetConfig.Fingerprint},
}
client.printJson("Asset Get Request", getArgs)
var getReply assets.GetReply
if err := client.client.Call("Assets.Get", &getArgs, &getReply); nil != err {
return nil, err
}
if 1 != len(getReply.Assets) {
return nil, fmt.Errorf("multple asset response")
}
switch getReply.Assets[0].Record {
case "AssetData":
ar, ok := getReply.Assets[0].Data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("missing asset data")
}
if ar["metadata"] != assetConfig.Metadata {
return nil, fmt.Errorf("mismatched asset metadata")
}
if ar["name"] != assetConfig.Name {
return nil, fmt.Errorf("mismatched asset name")
}
buffer, ok := getReply.Assets[0].AssetId.(string)
if !ok {
return nil, fmt.Errorf("missing asset id")
}
ai := &transactionrecord.AssetIdentifier{}
err := ai.UnmarshalText([]byte(buffer))
if nil != err {
return nil, err
}
result.AssetId = ai
result.Confirmed = getReply.Assets[0].Confirmed
default:
if nil != getReply.Assets[0].Data {
return nil, fmt.Errorf("non-asset response")
}
}
client.printJson("Asset Get Reply", getReply)
registrant := assetConfig.Registrant.PrivateKey.Account()
r := transactionrecord.AssetData{
Name: assetConfig.Name,
Fingerprint: assetConfig.Fingerprint,
Metadata: assetConfig.Metadata,
Registrant: registrant,
Signature: nil,
}
// pack without signature
packed, err := r.Pack(registrant)
if fault.InvalidSignature != err {
return nil, err
}
// manually sign the record and attach signature
signature := ed25519.Sign(assetConfig.Registrant.PrivateKey.PrivateKeyBytes(), packed)
r.Signature = signature[:]
// check that signature is correct by packing again
if _, err = r.Pack(registrant); nil != err {
return nil, err
}
client.printJson("Asset Request", r)
args := bitmarks.CreateArguments{
Assets: []*transactionrecord.AssetData{&r},
Issues: nil,
}
var reply bitmarks.CreateReply
if err := client.client.Call("Bitmarks.Create", &args, &reply); nil != err {
return nil, err
}
client.printJson("Asset Reply", reply)
result.AssetId = reply.Assets[0].AssetId
return result, nil
}