Skip to content

Commit

Permalink
fix: contract components reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
whalelephant committed Aug 23, 2021
1 parent 8c38c0f commit 63ea280
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 77 deletions.
34 changes: 17 additions & 17 deletions contracts/MultiSigFlowToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import OnChainMultiSig from 0x{{.OnChainMultiSig}}

pub contract MultiSigFlowToken: FungibleToken {

// Total supply of Flow tokens in existence
pub var totalSupply: UFix64

// Event that is emitted when the contract is created
pub event TokensInitialized(initialSupply: UFix64)

Expand All @@ -21,6 +18,9 @@ pub contract MultiSigFlowToken: FungibleToken {
pub let VaultReceiverPubPath: PublicPath;
pub let VaultPubSigner: PublicPath;

// Total supply of Flow tokens in existence
pub var totalSupply: UFix64

// Vault
//
pub resource Vault:
Expand All @@ -33,12 +33,10 @@ pub contract MultiSigFlowToken: FungibleToken {
// holds the balance of a users tokens
pub var balance: UFix64

// initialize the balance at resource creation time
init(balance: UFix64) {
self.balance = balance;
self.multiSigManager <- OnChainMultiSig.createMultiSigManager(publicKeys: [], pubKeyAttrs: [])
}

// Resource to keep track of partial sigatures and payloads, required for onchain multisig features.
// Limited to `access(self)` to avoid exposing all functions in `SignatureManager` interface to account owner(s)
access(self) let multiSigManager: @OnChainMultiSig.Manager;


pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
self.balance = self.balance - amount
Expand All @@ -55,13 +53,9 @@ pub contract MultiSigFlowToken: FungibleToken {
}

//
// Below resource and interfaces are required for any resources wanting to use OnChainMultiSig
// Below are the interfaces are required for any resources wanting to use OnChainMultiSig
//
// Resource to keep track of partial sigatures and payloads, required for onchain multisig features.
// Limited to `access(self)` to avoid exposing all functions in `SignatureManager` interface to account owner(s)
access(self) let multiSigManager: @OnChainMultiSig.Manager;

/// To submit a new paylaod, i.e. starting a new tx requiring, potentially requiring more signatures
pub fun addNewPayload(payload: @OnChainMultiSig.PayloadDetails, publicKey: String, sig: [UInt8]) {
self.multiSigManager.addNewPayload(resourceId: self.uuid, payload: <-payload, publicKey: publicKey, sig: sig);
Expand Down Expand Up @@ -157,15 +151,21 @@ pub contract MultiSigFlowToken: FungibleToken {
MultiSigFlowToken.totalSupply = MultiSigFlowToken.totalSupply - self.balance
destroy self.multiSigManager
}
}

pub fun createEmptyVault(): @Vault {
return <-create Vault(balance: 0.0)
// initialize the balance at resource creation time
init(balance: UFix64) {
self.balance = balance;
self.multiSigManager <- OnChainMultiSig.createMultiSigManager(publicKeys: [], pubKeyAttrs: [])
}

}

pub resource Administrator {
}

pub fun createEmptyVault(): @Vault {
return <-create Vault(balance: 0.0)
}

init(adminAccount: AuthAccount) {
self.totalSupply = 100000.0
Expand Down
138 changes: 78 additions & 60 deletions contracts/OnChainMultiSig.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,82 @@ import FungibleToken from "./FungibleToken.cdc"

pub contract OnChainMultiSig {

//
// ------- Events -------
//
pub event NewPayloadAdded(resourceId: UInt64, txIndex: UInt64);
pub event NewPayloadSigAdded(resourceId: UInt64, txIndex: UInt64);

//
// ------- Interfaces -------
//
/// Public Signer
///
/// These interfaces is intended for public usage, a resource that stores the @Manager should implement
///
/// 1. addNewPayload: add new transaction payload to the signature store waiting for others to sign
/// 2. addPayloadSignature: add signature to store for existing paylaods by payload index
/// 3. executeTx: attempt to execute the transaction at a given index after required signatures have been added
/// 4. UUID: gets the uuid of this resource
/// 5. getTxIndex: gets the sequentially assigned current txIndex of multisig pending tx of this resource
/// 6. getSignerKeys: gets the list of public keys for the resource's multisig signers
/// 7. getSignerKeyAttr: gets the stored key attributes
/// Interfaces 1&2 use `OnChainMultiSig.Manager` resource for code implementation
/// Interface 3 needs to be implemented specifically for each resource
/// Interfaces 4-7 are useful information to interact with the multiSigManager
///
/// For example, a `Vault` resource with onchain multisig capabilities should implement these interfaces,
/// see example in "./MultiSigFlowToken"
pub resource interface PublicSigner {
pub fun addNewPayload(payload: @PayloadDetails, publicKey: String, sig: [UInt8]);
pub fun addPayloadSignature (txIndex: UInt64, publicKey: String, sig: [UInt8]);
pub fun executeTx(txIndex: UInt64): @AnyResource?;
pub fun UUID(): UInt64;
pub fun getTxIndex(): UInt64;
pub fun getSignerKeys(): [String];
pub fun getSignerKeyAttr(publicKey: String): PubKeyAttr?;
}

/// Key Manager
///
/// Optional interfaces for owner of the vault to add / remove keys in @Manager.
pub resource interface KeyManager {
pub fun addKeys( multiSigPubKeys: [String], multiSigKeyWeights: [UFix64]);
pub fun removeKeys( multiSigPubKeys: [String]);
}

/// Signature Manager
///
/// These interfaces are minimum required for implementors of `PublicSigner` to work
/// with the @Manager resource
pub resource interface SignatureManager {
pub fun getSignerKeys(): [String];
pub fun getSignerKeyAttr(publicKey: String): PubKeyAttr?;
pub fun addNewPayload (resourceId: UInt64, payload: @PayloadDetails, publicKey: String, sig: [UInt8]);
pub fun addPayloadSignature (resourceId: UInt64, txIndex: UInt64, publicKey: String, sig: [UInt8]);
pub fun readyForExecution(txIndex: UInt64): @PayloadDetails?;
pub fun configureKeys (pks: [String], kws: [UFix64]);
pub fun removeKeys (pks: [String]);
}

//
// ------- Struct -------
//
pub struct PubKeyAttr{
pub let sigAlgo: UInt8;
pub let weight: UFix64

init(sa: UInt8, w: UFix64) {
self.sigAlgo = sa;
self.weight = w;
}
}

//
// ------- Resources -------
//
/// PayloadDetails
///
Expand Down Expand Up @@ -141,65 +214,6 @@ pub contract OnChainMultiSig {
}
}

pub struct PubKeyAttr{
pub let sigAlgo: UInt8;
pub let weight: UFix64

init(sa: UInt8, w: UFix64) {
self.sigAlgo = sa;
self.weight = w;
}
}

/// Public Signer
///
/// These interfaces is intended for public usage, a resource that stores the @Manager should implement
///
/// 1. addNewPayload: add new transaction payload to the signature store waiting for others to sign
/// 2. addPayloadSignature: add signature to store for existing paylaods by payload index
/// 3. executeTx: attempt to execute the transaction at a given index after required signatures have been added
/// 4. UUID: gets the uuid of this resource
/// 5. getTxIndex: gets the sequentially assigned current txIndex of multisig pending tx of this resource
/// 6. getSignerKeys: gets the list of public keys for the resource's multisig signers
/// 7. getSignerKeyAttr: gets the stored key attributes
/// Interfaces 1&2 use `OnChainMultiSig.Manager` resource for code implementation
/// Interface 3 needs to be implemented specifically for each resource
/// Interfaces 4-7 are useful information to interact with the multiSigManager
///
/// For example, a `Vault` resource with onchain multisig capabilities should implement these interfaces,
/// see example in "./MultiSigFlowToken"
pub resource interface PublicSigner {
pub fun addNewPayload(payload: @PayloadDetails, publicKey: String, sig: [UInt8]);
pub fun addPayloadSignature (txIndex: UInt64, publicKey: String, sig: [UInt8]);
pub fun executeTx(txIndex: UInt64): @AnyResource?;
pub fun UUID(): UInt64;
pub fun getTxIndex(): UInt64;
pub fun getSignerKeys(): [String];
pub fun getSignerKeyAttr(publicKey: String): PubKeyAttr?;
}

/// Key Manager
///
/// Optional interfaces for owner of the vault to add / remove keys in @Manager.
pub resource interface KeyManager {
pub fun addKeys( multiSigPubKeys: [String], multiSigKeyWeights: [UFix64]);
pub fun removeKeys( multiSigPubKeys: [String]);
}

/// Signature Manager
///
/// These interfaces are minimum required for implementors of `PublicSigner` to work
/// with the @Manager resource
pub resource interface SignatureManager {
pub fun getSignerKeys(): [String];
pub fun getSignerKeyAttr(publicKey: String): PubKeyAttr?;
pub fun addNewPayload (resourceId: UInt64, payload: @PayloadDetails, publicKey: String, sig: [UInt8]);
pub fun addPayloadSignature (resourceId: UInt64, txIndex: UInt64, publicKey: String, sig: [UInt8]);
pub fun readyForExecution(txIndex: UInt64): @PayloadDetails?;
pub fun configureKeys (pks: [String], kws: [UFix64]);
pub fun removeKeys (pks: [String]);
}

/// Manager
///
/// The main resource that stores, keys, payloads and signature before all signatures are collected / executed
Expand Down Expand Up @@ -377,7 +391,11 @@ pub contract OnChainMultiSig {
}
}
}


//
// ------- Functions --------
//
pub fun createMultiSigManager(publicKeys: [String], pubKeyAttrs: [PubKeyAttr]): @Manager {
return <- create Manager(publicKeys: publicKeys, pubKeyAttrs: pubKeyAttrs)
}
Expand Down

0 comments on commit 63ea280

Please sign in to comment.