-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFlowColdStorageProxy.cdc
More file actions
318 lines (268 loc) · 13.7 KB
/
Copy pathFlowColdStorageProxy.cdc
File metadata and controls
318 lines (268 loc) · 13.7 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import FlowToken from 0x0ae53cb6e3f42a79
import FungibleToken from 0xee82856bf20e2aa6
import FlowIDTableStaking from 0x8624b52f9ddcd04a
// FlowColdStorageProxy provides support for the transfer of FLOW tokens within
// cold storage transactions.
//
// Since cold storage transactions are signed offline, the time it takes for the
// transaction construction could potentially exceed Flow's expiry window. We
// work around this constraint by signing a meta transaction for transfers,
// which can then be submitted by the owner account.
//
// Combined with an immutable owner account, i.e. an account without any
// registered keys, this can also be used to enable a more "locked down" account
// for doing sends/receives of FLOW tokens.
pub contract FlowColdStorageProxy {
pub let VaultCapabilityPublicPath: PublicPath
pub let VaultCapabilityStoragePath: StoragePath
// Created is emitted when a new FlowColdStorageProxy.Vault is set up on an
// account.
pub event Created(account: Address, publicKey: String)
// Deposited is emitted when FLOW tokens are deposited to the
// FlowColdStorageProxy.Vault.
pub event Deposited(account: Address, amount: UFix64)
// Transferred is emitted when a transfer takes place from a
// FlowColdStorageProxy.Vault.
pub event Transferred(from: Address?, to: Address, amount: UFix64)
// Delegated is emitted when a delegation takes place from a
// FlowColdStorageProxy.Vault.
pub event Delegated(from: Address?, to: String, amount: UFix64)
// RewardsDelegated is emitted when delegation rewards are re-delegated.
pub event RewardsDelegated(from: Address?, to: String, amount: UFix64)
// RequestedUndelegate is emitted when an undelgation request takes place.
pub event RequestedUndelegate(from: Address?, to: String, amount: UFix64)
// WithdrewUndelegated is emitted when previously delegated funds are
// transfered back to the Vault.
pub event WithdrewUndelegated(from: Address?, to: String, amount: UFix64)
// Vault implements FungibleToken.Receiver and some additional methods for
// making transfers and getting the balance.
//
// It wraps a FlowToken.Vault resource so that its funds cannot be
// transferred elsewhere without having a meta transaction signed by the
// associated public key.
//
// To ensure that an owner account can't move the resource, and thus make
// funds inaccessible, the account should be made immutable after the
// FlowColdStorageProxy.setup call by ensuring that no keys are registered
// on the account itself.
pub resource Vault: FungibleToken.Receiver {
pub var lastNonce: Int64
access(self) let flowVault: @FungibleToken.Vault
access(self) let publicKey: [UInt8]
access(self) let nodeDelegator: @FlowIDTableStaking.NodeDelegator
init(publicKey: [UInt8], stakingNodeID: String) {
self.flowVault <- FlowToken.createEmptyVault()
self.nodeDelegator <- FlowIDTableStaking.registerNewDelegator(nodeID: stakingNodeID)
self.lastNonce = -1
self.publicKey = publicKey
// NOTE(tav): We instantiate a PublicKey to make sure it is valid.
let key = PublicKey(
publicKey: self.publicKey,
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
)
}
// deposit proxies the call to the underlying FlowToken.Vault resource.
pub fun deposit(from: @FungibleToken.Vault) {
emit Deposited(account: self.owner!.address, amount: from.balance)
self.flowVault.deposit(from: <- from)
}
// getBalance returns the balance of the underlying FlowToken.Vault
// resource.
pub fun getBalance(): UFix64 {
return self.flowVault.balance
}
// getPublicKey returns the raw public key for the Vault.
pub fun getPublicKey(): [UInt8] {
return self.publicKey
}
// getDelegatorInfo return a struct with info about the
// delegated balances for the Vault.
pub fun getDelegatorInfo(): FlowIDTableStaking.DelegatorInfo {
return FlowIDTableStaking.DelegatorInfo(
nodeID: self.nodeDelegator.id,
delegatorID: self.nodeDelegator.nodeID
)
}
// LEAVE THIS COMMENT AS IS. IT IS USED BY THE AUTOMATED TESTS.
// transfer acts as a meta transaction to transfer FLOW from the
// underlying FlowToken.Vault to a receiver.
//
// While during execution, the transaction parameters are passed in as
// Cadence values, for signing purposes, the raw data is first encoded
// into the following binary layout:
//
// <domain-tag><receiver-address><uint64-amount><uint64-nonce>
//
// Where all uint64 values are encoded using big endian format, and the
// domain separation tag is 32 bytes long and made up of the string
// "FLOW-V0.0-user" followed by a padding of null bytes.
//
// The resulting data is then hashed with SHA3-256 before being signed.
pub fun transfer(receiver: Address, amount: UFix64, nonce: Int64, sig: [UInt8]) {
// Ensure that the nonce follows on from the previous meta
// transaction.
if nonce != (self.lastNonce + 1) {
panic("Invalid meta transaction nonce")
}
// Construct the message to sign.
var data: [UInt8] = receiver.toBytes()
data = data.concat(amount.toBigEndianBytes())
data = data.concat(nonce.toBigEndianBytes())
// Verify the signature.
let key = PublicKey(
publicKey: self.publicKey,
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
)
if !key.verify(signature: sig, signedData: data, domainSeparationTag: "FLOW-V0.0-user", hashAlgorithm: HashAlgorithm.SHA3_256) {
panic("Invalid meta transaction signature")
}
// Do the actual transfer.
let acct = getAccount(receiver)
.getCapability(/public/flowTokenReceiver)
.borrow<&{FungibleToken.Receiver}>()
?? panic("Unable to borrow a reference to the receiver's default token vault")
let xfer <- self.flowVault.withdraw(amount: amount)
acct.deposit(from: <- xfer)
// Increment the nonce.
self.lastNonce = self.lastNonce + 1
emit Transferred(from: self.owner!.address, to: receiver, amount: amount)
}
pub fun delegateVaultTokens(amount: UFix64, nonce: Int64, sig: [UInt8]) {
// Ensure that the nonce follows on from the previous meta
// transaction.
if nonce != (self.lastNonce + 1) {
panic("Invalid meta transaction nonce")
}
// Construct the message to sign.
var data: [UInt8] = "delegate".toBytes()
data = data.concat(amount.toBigEndianBytes())
data = data.concat(nonce.toBigEndianBytes())
// Verify the signature.
let key = PublicKey(
publicKey: self.publicKey,
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
)
if !key.verify(signature: sig, signedData: data, domainSeparationTag: "FLOW-V0.0-user", hashAlgorithm: HashAlgorithm.SHA3_256) {
panic("Invalid meta transaction signature")
}
let xfer <- self.flowVault.withdraw(amount: amount)
self.nodeDelegator.delegateNewTokens(from: <- xfer)
// Increment the nonce.
self.lastNonce = self.lastNonce + 1
emit Delegated(from: self.owner!.address, to: self.nodeDelegator.nodeID, amount: amount)
}
pub fun delegateRewardsTokens(nonce: Int64, sig: [UInt8]) {
// Ensure that the nonce follows on from the previous meta
// transaction.
if nonce != (self.lastNonce + 1) {
panic("Invalid meta transaction nonce")
}
// Construct the message to sign.
var data: [UInt8] = "delegate-rewards".toBytes()
data = data.concat(nonce.toBigEndianBytes())
// Verify the signature.
let key = PublicKey(
publicKey: self.publicKey,
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
)
if !key.verify(signature: sig, signedData: data, domainSeparationTag: "FLOW-V0.0-user", hashAlgorithm: HashAlgorithm.SHA3_256) {
panic("Invalid meta transaction signature")
}
let delegatorInfo = self.getDelegatorInfo()
let amount = delegatorInfo.tokensRewarded
self.nodeDelegator.delegateRewardedTokens(amount: amount)
// Increment the nonce.
self.lastNonce = self.lastNonce + 1
emit RewardsDelegated(from: self.owner!.address, to: self.nodeDelegator.nodeID, amount: amount)
}
pub fun requestUndelegateTokens(amount: UFix64, nonce: Int64, sig: [UInt8]) {
// Ensure that the nonce follows on from the previous meta
// transaction.
if nonce != (self.lastNonce + 1) {
panic("Invalid meta transaction nonce")
}
// Construct the message to sign.
var data: [UInt8] = "undelegate".toBytes()
data = data.concat(amount.toBigEndianBytes())
data = data.concat(nonce.toBigEndianBytes())
// Verify the signature.
let key = PublicKey(
publicKey: self.publicKey,
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
)
if !key.verify(signature: sig, signedData: data, domainSeparationTag: "FLOW-V0.0-user", hashAlgorithm: HashAlgorithm.SHA3_256) {
panic("Invalid meta transaction signature")
}
self.nodeDelegator.requestUnstaking(amount: amount)
// Increment the nonce.
self.lastNonce = self.lastNonce + 1
emit RequestedUndelegate(from: self.owner!.address, to: self.nodeDelegator.nodeID, amount: amount)
}
pub fun withdrawUndelegatedTokensToVault(nonce: Int64, sig: [UInt8]) {
// Ensure that the nonce follows on from the previous meta
// transaction.
if nonce != (self.lastNonce + 1) {
panic("Invalid meta transaction nonce")
}
// Construct the message to sign.
var data: [UInt8] = "delegate-rewards".toBytes()
data = data.concat(nonce.toBigEndianBytes())
// Verify the signature.
let key = PublicKey(
publicKey: self.publicKey,
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
)
if !key.verify(signature: sig, signedData: data, domainSeparationTag: "FLOW-V0.0-user", hashAlgorithm: HashAlgorithm.SHA3_256) {
panic("Invalid meta transaction signature")
}
let delegatorInfo = self.getDelegatorInfo()
let totalAmount = delegatorInfo.tokensRewarded + delegatorInfo.tokensUnstaked
if totalAmount == 0.0 {
panic("No funds available for withdrawal")
}
if delegatorInfo.tokensRewarded > 0.0 {
let xfer = self.nodeDelegator.withdrawRewardedTokens(amount: delegatorInfo.tokensRewarded)
self.flowVault.deposit(from: <- xfer)
}
if delegatorInfo.tokensUnstaked > 0.0 {
let xfer = self.nodeDelegator.withdrawUnstakedTokens(amount: delegatorInfo.tokensUnstaked)
self.flowVault.deposit(from: <- xfer)
}
// Increment the nonce.
self.lastNonce = self.lastNonce + 1
emit WithdrewUndelegated(from: self.owner!.address, to: self.nodeDelegator.nodeID, amount: totalAmount)
}
destroy() {
if self.flowVault.balance > 0.0 {
panic("Cannot destroy a Vault without transferring the remaining balance")
}
if self.getDelegatorInfo().totalTokensInRecord() > 0.0 {
panic("Cannot destroy a Vault with delegated funds remaining")
}
destroy self.flowVault
}
}
// setup creates a new FlowColdStorageProxy.Vault with the given public key,
// and stores it within /storage/proxyVault on the given account.
//
// This function also moves the default /public/flowTokenReceiver to
// /public/defaultFlowTokenReceiver, and replaces /public/flowTokenReceiver
// with the FlowColdStorageProxy.Vault.
//
// And, finally, the FlowColdStorageProxy.Vault itself is made directly
// accessible via /public/flowColdStorageProxyVault.
pub fun setup(payer: AuthAccount, publicKey: [UInt8], stakingNodeID: String): Address {
let acct = AuthAccount(payer: payer)
acct.save(<- create Vault(publicKey: publicKey, stakingNodeID: stakingNodeID), to: self.VaultCapabilityStoragePath)
acct.unlink(/public/flowTokenReceiver)
acct.link<&{FungibleToken.Receiver}>(/public/defaultFlowTokenReceiver, target: /storage/flowTokenVault)!
acct.link<&{FungibleToken.Receiver}>(/public/flowTokenReceiver, target: self.VaultCapabilityStoragePath)!
acct.link<&Vault>(self.VaultCapabilityPublicPath, target: self.VaultCapabilityStoragePath)!
emit Created(account: acct.address, publicKey: String.encodeHex(publicKey))
return acct.address
}
init() {
self.VaultCapabilityPublicPath = /public/flowColdStorageProxyVault
self.VaultCapabilityStoragePath = /storage/flowColdStorageProxyVault
}
}