-
Notifications
You must be signed in to change notification settings - Fork 13
/
asset.go
124 lines (98 loc) · 2.85 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
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpccalls
import (
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/keypair"
"github.com/bitmark-inc/bitmarkd/rpc"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"golang.org/x/crypto/ed25519"
)
var (
ErrAssetRequestFail = fault.ProcessError("send asset request failed")
)
type AssetData struct {
Name string
Metadata string
Quantity int
Registrant *keypair.KeyPair
Fingerprint string
}
// build a properly signed asset
func (client *Client) MakeAsset(assetConfig *AssetData) (*transactionrecord.AssetIndex, error) {
assetIndex := (*transactionrecord.AssetIndex)(nil)
getArgs := rpc.AssetGetArguments{
Fingerprints: []string{assetConfig.Fingerprint},
}
client.printJson("Asset Get Request", getArgs)
var getReply rpc.AssetGetReply
if err := client.client.Call("Assets.Get", &getArgs, &getReply); nil != err {
return nil, err
}
if 1 != len(getReply.Assets) {
return nil, ErrAssetRequestFail
}
switch getReply.Assets[0].Record {
case "AssetData":
ar, ok := getReply.Assets[0].Data.(map[string]interface{})
if !ok {
return nil, ErrAssetRequestFail
}
if ar["metadata"] != assetConfig.Metadata {
return nil, ErrAssetRequestFail
}
if ar["name"] != assetConfig.Name {
return nil, ErrAssetRequestFail
}
buffer, ok := getReply.Assets[0].AssetIndex.(string)
if !ok {
return nil, ErrAssetRequestFail
}
var ai transactionrecord.AssetIndex
err := ai.UnmarshalText([]byte(buffer))
if nil != err {
return nil, err
}
assetIndex = &ai
default:
if nil != getReply.Assets[0].Data {
return nil, ErrAssetRequestFail
}
}
client.printJson("Asset Get Reply", getReply)
if nil != assetIndex {
return assetIndex, nil
}
registrantAddress := makeAddress(assetConfig.Registrant, client.testnet)
r := transactionrecord.AssetData{
Name: assetConfig.Name,
Fingerprint: assetConfig.Fingerprint,
Metadata: assetConfig.Metadata,
Registrant: registrantAddress,
Signature: nil,
}
// pack without signature
packed, err := r.Pack(registrantAddress)
if fault.ErrInvalidSignature != err {
return nil, err
}
// manually sign the record and attach signature
signature := ed25519.Sign(assetConfig.Registrant.PrivateKey, packed)
r.Signature = signature[:]
// check that signature is correct by packing again
if _, err = r.Pack(registrantAddress); nil != err {
return nil, err
}
client.printJson("Asset Request", r)
args := rpc.CreateArguments{
Assets: []*transactionrecord.AssetData{&r},
Issues: nil,
}
var reply rpc.CreateReply
if err := client.client.Call("Bitmarks.Create", &args, &reply); nil != err {
return nil, err
}
client.printJson("Asset Reply", reply)
return reply.Assets[0].AssetIndex, nil
}