Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Proof of Possession for Node Staking Keys #457

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions contracts/FlowIDTableStaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ access(all) contract FlowIDTableStaking {
access(all) var networkingKey: String
access(all) var stakingKey: String

/// TODO: Proof of Possession (PoP) of the staking private key

/// The total tokens that only this node currently has staked, not including delegators
/// This value must always be above the minimum requirement to stay staked or accept delegators
access(mapping Identity) var tokensStaked: @FlowToken.Vault
Expand Down Expand Up @@ -160,6 +158,7 @@ access(all) contract FlowIDTableStaking {
networkingAddress: String,
networkingKey: String,
stakingKey: String,
stakingKeyPoP: String,
tokensCommitted: @{FungibleToken.Vault}
) {
pre {
Expand All @@ -180,13 +179,20 @@ access(all) contract FlowIDTableStaking {
signatureAlgorithm: SignatureAlgorithm.BLS_BLS12_381
)

// Verify the proof of possesion of the private staking key
assert(
stakeKey.verifyPoP(stakingKeyPoP.decodeHex()),
message:
"FlowIDTableStaking.NodeRecord.init: Cannot create node with ID "
.concat(id).concat(". The Proof of Possession (").concat(stakingKeyPoP)
.concat(") for the node's staking key (").concat(") is invalid")
)

let netKey = PublicKey(
publicKey: networkingKey.decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

// TODO: Verify the provided Proof of Possession of the staking private key

self.id = id
self.role = role
self.networkingAddress = networkingAddress
Expand Down Expand Up @@ -1560,6 +1566,7 @@ access(all) contract FlowIDTableStaking {
networkingAddress: String,
networkingKey: String,
stakingKey: String,
stakingKeyPoP: String,
tokensCommitted: @{FungibleToken.Vault}): @NodeStaker
{
assert (
Expand All @@ -1572,6 +1579,7 @@ access(all) contract FlowIDTableStaking {
networkingAddress: networkingAddress,
networkingKey: networkingKey,
stakingKey: stakingKey,
stakingKeyPoP: stakingKeyPoP,
tokensCommitted: <-FlowToken.createEmptyVault(vaultType: Type<@FlowToken.Vault>()))

let minimum = self.minimumStakeRequired[role]!
Expand Down
2 changes: 2 additions & 0 deletions contracts/FlowStakingCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ access(all) contract FlowStakingCollection {
networkingAddress: String,
networkingKey: String,
stakingKey: String,
stakingKeyPoP: String,
amount: UFix64,
payer: auth(BorrowValue) &Account
): auth(Storage, Capabilities, Contracts, Keys, Inbox) &Account? {
Expand All @@ -485,6 +486,7 @@ access(all) contract FlowStakingCollection {
networkingAddress: networkingAddress,
networkingKey: networkingKey,
stakingKey: stakingKey,
stakingKeyPoP: stakingKeyPoP,
tokensCommitted: <-tokens
)

Expand Down
17 changes: 13 additions & 4 deletions contracts/LockedTokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ access(all) contract LockedTokens {

/// Registers a new node operator with the Flow Staking contract
/// and commits an initial amount of locked tokens to stake
access(account) fun registerNode(nodeInfo: StakingProxy.NodeInfo, amount: UFix64) {
access(account) fun registerNode(nodeInfo: StakingProxy.NodeInfo,
stakingKeyPoP: String,
amount: UFix64
) {
if let nodeStaker <- self.nodeStaker <- nil {
let stakingInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeStaker.id)

Expand All @@ -210,7 +213,13 @@ access(all) contract LockedTokens {

let tokens <- vaultRef.withdraw(amount: amount)

let nodeStaker <- self.nodeStaker <- FlowIDTableStaking.addNodeRecord(id: nodeInfo.id, role: nodeInfo.role, networkingAddress: nodeInfo.networkingAddress, networkingKey: nodeInfo.networkingKey, stakingKey: nodeInfo.stakingKey, tokensCommitted: <-tokens)
let nodeStaker <- self.nodeStaker <- FlowIDTableStaking.addNodeRecord(id: nodeInfo.id,
role: nodeInfo.role,
networkingAddress: nodeInfo.networkingAddress,
networkingKey: nodeInfo.networkingKey,
stakingKey: nodeInfo.stakingKey,
stakingKeyPoP: stakingKeyPoP,
tokensCommitted: <-tokens)

destroy nodeStaker

Expand Down Expand Up @@ -378,9 +387,9 @@ access(all) contract LockedTokens {

/// The user calls this function if they want to register as a node operator
/// They have to provide all the info for their node
access(TokenOperations) fun createNodeStaker(nodeInfo: StakingProxy.NodeInfo, amount: UFix64) {
access(TokenOperations) fun createNodeStaker(nodeInfo: StakingProxy.NodeInfo, stakingKeyPoP: String, amount: UFix64) {

self.borrowTokenManager().registerNode(nodeInfo: nodeInfo, amount: amount)
self.borrowTokenManager().registerNode(nodeInfo: nodeInfo, stakingKeyPoP: stakingKeyPoP, amount: amount)

// Create a new staker proxy that can be accessed in transactions
self.nodeStakerProxy = LockedNodeStakerProxy(tokenManager: self.tokenManager)
Expand Down
1 change: 1 addition & 0 deletions contracts/testContracts/TestFlowIDTableStaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ access(all) contract FlowIDTableStaking {
networkingAddress: String,
networkingKey: String,
stakingKey: String,
stakingKeyPoP: String,
tokensCommitted: @{FungibleToken.Vault}
): @NodeStaker {
destroy tokensCommitted
Expand Down
24 changes: 12 additions & 12 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

76 changes: 74 additions & 2 deletions lib/go/templates/cmd/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func generateManifest(env templates.Environment) *manifest {
}

sampleStakingKey := cadenceValue{
cadence.String("9e9ae0d645fd5fd9050792e0b0daa82cc1686d9133afa0f81a784b375c42ae48567d1545e7a9e1965f2c1a32f73cf8575ebb7a967f6e4d104d2df78eb8be409135d12da0499b8a00771f642c1b9c49397f22b440439f036c3bdee82f5309dab3"),
cadence.String("8dec36ed8a91e3e5d737b06434d94a8a561c7889495d6c7081cd5e123a42124415b9391c9b9aa165c2f71994bf9607cb0ea262ad162fec74146d1ebc482a33b9dad203d16a83bbfda89b3f6e1cd1d8fb2e704a162d259a0ac9f26bc8635d74f6"),
}

sampleStakingKeyPoP := cadenceValue{
cadence.String("828a68a2be392804044d85888100462702a422901da3269fb6512defabad07250aad24f232671e4ac8ae531f54e062fc"),
}

sampleNullOptional := cadenceValue{
Expand Down Expand Up @@ -442,7 +446,7 @@ func generateManifest(env templates.Environment) *manifest {
m.addTemplate(generateTemplate(
"SCO.03", "Register Node",
env,
templates.GenerateCollectionRegisterNode,
templates.GenerateCollectionRegisterNodeOld,
[]argument{
{
Type: "String",
Expand Down Expand Up @@ -830,6 +834,74 @@ func generateManifest(env templates.Environment) *manifest {
},
))

m.addTemplate(generateTemplate(
"SCO.17", "Register Node with PoP",
env,
templates.GenerateCollectionRegisterNode,
[]argument{
{
Type: "String",
Name: "id",
Label: "Node ID",
SampleValues: []cadenceValue{sampleNodeID},
},
{
Type: "UInt8",
Name: "role",
Label: "Node Role",
SampleValues: []cadenceValue{sampleNodeRole},
},
{
Type: "String",
Name: "networkingAddress",
Label: "Networking Address",
SampleValues: []cadenceValue{sampleNetworkingAddress},
},
{
Type: "String",
Name: "networkingKey",
Label: "Networking Key",
SampleValues: []cadenceValue{sampleNetworkingKey},
},
{
Type: "String",
Name: "stakingKey",
Label: "Staking Key",
SampleValues: []cadenceValue{sampleStakingKey},
},
{
Type: "String",
Name: "stakingKeyPoP",
Label: "Staking Key PoP",
SampleValues: []cadenceValue{sampleStakingKeyPoP},
},
{
Type: "UFix64",
Name: "amount",
Label: "Amount",
SampleValues: []cadenceValue{sampleAmount},
},
{
Type: "String",
Name: "machineAccountKey",
Label: "Machine Account Public Key",
SampleValues: []cadenceValue{sampleKey},
},
{
Type: "UInt8",
Name: "machineAccountKeySignatureAlgorithm",
Label: "Raw Value for Machine Account Signature Algorithm Enum",
SampleValues: []cadenceValue{sampleSigAlgoEnumRawValue},
},
{
Type: "UInt8",
Name: "machineAccountKeyHashAlgorithm",
Label: "Raw Value for Machine Account Hash Algorithm Enum",
SampleValues: []cadenceValue{sampleHashAlgoEnumRawValue},
},
},
))

return m
}

Expand Down
Loading
Loading