-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
158 lines (139 loc) · 5.78 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"crypto/ed25519"
"github.com/diem/client-sdk-go/diemkeys"
"github.com/diem/client-sdk-go/diemtypes"
"github.com/diem/client-sdk-go/examples/exampleutils"
"github.com/diem/client-sdk-go/stdlib"
"github.com/diem/client-sdk-go/testnet"
"github.com/diem/client-sdk-go/txnmetadata"
)
const currency = "XUS"
func main() {
nonCustodialAccount := testnet.GenAccount()
nonCustodialAccount2 := testnet.GenAccount()
custodialAccountParentVasp, custodialAccountChildVasp, custodialAccountSubAddress := createCustodialAccount()
amount := uint64(10000)
// Non custodial to non custodial has no requirement on metadata
exampleutils.PrintAccountsBalances("before transfer", nonCustodialAccount, nonCustodialAccount2)
exampleutils.SubmitAndWait(
"non custodial account to non custodial account transaction",
nonCustodialAccount,
stdlib.EncodePeerToPeerWithMetadataScript(
diemtypes.Currency(currency),
nonCustodialAccount2.AccountAddress(),
amount,
nil,
nil,
),
)
exampleutils.PrintAccountsBalances("after transfer", nonCustodialAccount, nonCustodialAccount2)
// Non custodial account to custodial account requires target custodial account subaddress,
// hence we need construct a general metadata includes to_subaddress
exampleutils.PrintAccountsBalances("before transfer", nonCustodialAccount, custodialAccountChildVasp)
exampleutils.SubmitAndWait(
"non custodial account to custodial account transaction",
nonCustodialAccount,
stdlib.EncodePeerToPeerWithMetadataScript(
diemtypes.Currency(currency),
custodialAccountChildVasp.AccountAddress(),
amount,
txnmetadata.NewGeneralMetadataToSubAddress(custodialAccountSubAddress),
nil, // no metadata signature for GeneralMetadata
),
)
exampleutils.PrintAccountsBalances("after transfer", nonCustodialAccount, custodialAccountChildVasp)
// Custodial account to non-custodial account requires sender's custodial account subaddress,
// hence we need construct a general metadata includes from_subaddress
exampleutils.PrintAccountsBalances("before transfer", custodialAccountChildVasp, nonCustodialAccount)
exampleutils.SubmitAndWait(
"custodial account to non custodial account transaction",
custodialAccountChildVasp,
stdlib.EncodePeerToPeerWithMetadataScript(
diemtypes.Currency(currency),
nonCustodialAccount.AccountAddress(),
amount,
txnmetadata.NewGeneralMetadataFromSubAddress(custodialAccountSubAddress),
nil, // no metadata signature for GeneralMetadata
),
)
exampleutils.PrintAccountsBalances("after transfer", custodialAccountChildVasp, nonCustodialAccount)
// Custodial account to custodial account transaction has 2 cases
// setup sender custodial account
_, senderCustodialAccountChildVasp, senderCustodialAccountSubAddress := createCustodialAccount()
// Case 1: For transactions under the travel rule threshold, transaction metadata inclusive of both to_subaddress and from_subaddress should be composed.
exampleutils.PrintAccountsBalances("before transfer",
senderCustodialAccountChildVasp, custodialAccountChildVasp)
exampleutils.SubmitAndWait(
"custodial account to custodial account transaction under travel rule threshold",
senderCustodialAccountChildVasp,
stdlib.EncodePeerToPeerWithMetadataScript(
diemtypes.Currency(currency),
custodialAccountChildVasp.AccountAddress(),
amount,
txnmetadata.NewGeneralMetadataWithFromToSubAddresses(
senderCustodialAccountSubAddress,
custodialAccountSubAddress,
),
nil, // no metadata signature for GeneralMetadata
),
)
exampleutils.PrintAccountsBalances("after transfer",
senderCustodialAccountChildVasp, custodialAccountChildVasp)
// Case 2: For transactions over the travel rule limit, custodial to custodial transactions must exchange travel rule compliance data off-chain
// setup receiver compliance public & private keys
compliancePublicKey, compliancePrivateKey, _ := ed25519.GenerateKey(nil)
exampleutils.SubmitAndWait(
"setup parent vasp compliance key, testnet defaults it to fake key.",
custodialAccountParentVasp,
stdlib.EncodeRotateDualAttestationInfoScript(
[]byte("http://helloworld.com"),
[]byte(compliancePublicKey),
),
)
// sender & receiver communicate by off chain APIs
offChainReferenceId := "32323abc"
// metadata and signature message
metadata, sigMsg := txnmetadata.NewTravelRuleMetadata(
offChainReferenceId,
senderCustodialAccountChildVasp.AccountAddress(),
amount,
)
// receiver_signature is passed to the sender via the off-chain APIs as per
// https://github.com/diem/lip/blob/master/lips/lip-1.mdx#recipient-signature
recipientSignature := ed25519.Sign(compliancePrivateKey, sigMsg)
exampleutils.PrintAccountsBalances("before transfer",
senderCustodialAccountChildVasp, custodialAccountChildVasp)
exampleutils.SubmitAndWait(
"custodial account to custodial account transaction",
senderCustodialAccountChildVasp,
stdlib.EncodePeerToPeerWithMetadataScript(
diemtypes.Currency(currency),
custodialAccountChildVasp.AccountAddress(), //receiverAccountAddress,
amount,
metadata,
recipientSignature,
),
)
exampleutils.PrintAccountsBalances("after transfer",
senderCustodialAccountChildVasp, custodialAccountChildVasp)
}
func createCustodialAccount() (*diemkeys.Keys, *diemkeys.Keys, diemtypes.SubAddress) {
parentVASP := testnet.GenAccount()
childVASPAccount := diemkeys.MustGenKeys()
exampleutils.SubmitAndWait(
"create child vasp for custodial account",
parentVASP,
stdlib.EncodeCreateChildVaspAccountScript(
testnet.XUS,
childVASPAccount.AccountAddress(),
childVASPAccount.AuthKey().Prefix(),
false,
uint64(100000),
),
)
custodialAccountSubAddress := diemtypes.MustGenSubAddress()
return parentVASP, childVASPAccount, custodialAccountSubAddress
}