Skip to content

Commit

Permalink
Merge pull request #7 from flow-hydraulics/feat/configurable-key-algo
Browse files Browse the repository at this point in the history
wip: add sigAlgo to configureKeys argument
  • Loading branch information
aphelionz authored Sep 15, 2021
2 parents 0d70a13 + ba51f44 commit 010fe72
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 14 deletions.
7 changes: 4 additions & 3 deletions contracts/MultiSigFlowToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ pub contract MultiSigFlowToken: FungibleToken {
case "configureKey":
let pubKey = p.getArg(i: 0)! as? String ?? panic ("cannot downcast public key");
let weight = p.getArg(i: 1)! as? UFix64 ?? panic ("cannot downcast weight");
let sigAlgo = p.getArg(i: 2)! as? UInt8 ?? panic ("cannot downcast sigAlgo");
destroy(p)
self.multiSigManager.configureKeys(pks: [pubKey], kws: [weight])
self.multiSigManager.configureKeys(pks: [pubKey], kws: [weight], sa: [sigAlgo])
case "removeKey":
let pubKey = p.getArg(i: 0)! as? String ?? panic ("cannot downcast public key");
destroy(p)
Expand Down Expand Up @@ -139,8 +140,8 @@ pub contract MultiSigFlowToken: FungibleToken {
//
// These follows the usual account authorization logic
// i.e. if it is an account with multiple keys, then the total weight of the signatures must be > 1000
pub fun addKeys( multiSigPubKeys: [String], multiSigKeyWeights: [UFix64]) {
self.multiSigManager.configureKeys(pks: multiSigPubKeys, kws: multiSigKeyWeights)
pub fun addKeys( multiSigPubKeys: [String], multiSigKeyWeights: [UFix64], multiSigAlgos: [UInt8]) {
self.multiSigManager.configureKeys(pks: multiSigPubKeys, kws: multiSigKeyWeights, sa: multiSigAlgos)
}

pub fun removeKeys( multiSigPubKeys: [String]) {
Expand Down
13 changes: 8 additions & 5 deletions contracts/OnChainMultiSig.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub contract OnChainMultiSig {
///
/// 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 addKeys( multiSigPubKeys: [String], multiSigKeyWeights: [UFix64], multiSigAlgos: [UInt8]);
pub fun removeKeys( multiSigPubKeys: [String]);
}

Expand All @@ -58,7 +58,7 @@ pub contract OnChainMultiSig {
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 configureKeys (pks: [String], kws: [UFix64], sa: [UInt8]);
pub fun removeKeys (pks: [String]);
}

Expand Down Expand Up @@ -126,6 +126,9 @@ pub contract OnChainMultiSig {
case Type<UFix64>():
let temp = a as? UFix64;
b = temp!.toBigEndianBytes();
case Type<UInt8>():
let temp = a as? UInt8;
b = temp!.toBigEndianBytes();
case Type<Address>():
let temp = a as? Address;
b = temp!.toBytes();
Expand Down Expand Up @@ -270,10 +273,10 @@ pub contract OnChainMultiSig {

/// Add / replace stored public keys and respected attributes
/// from `keyList`
pub fun configureKeys (pks: [String], kws: [UFix64]) {
pub fun configureKeys (pks: [String], kws: [UFix64], sa: [UInt8]) {
var i: Int = 0;
while (i < pks.length) {
let a = PubKeyAttr(sa: 1, w: kws[i])
let a = PubKeyAttr(sa: sa[i], w: kws[i])
self.keyList.insert(key: pks[i], a)
i = i + 1;
}
Expand Down Expand Up @@ -411,4 +414,4 @@ pub contract OnChainMultiSig {
pub fun createPayload(txIndex: UInt64, method: String, args: [AnyStruct], rsc: @AnyResource?): @PayloadDetails{
return <- create PayloadDetails(txIndex: txIndex, method: method, args: args, rsc: <-rsc)
}
}
}
6 changes: 4 additions & 2 deletions lib/go/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ func MultiSig_ConfigKey(
pkToConfig := cadence.NewString(g.Accounts[acctToConfig].PrivateKey.PublicKey().String()[2:])

weightToConfig, err := cadence.NewUFix64(acctToConfigWeight)
sigAlgoToConfig := cadence.NewUInt8(1)

if err != nil {
return
}
signable, err := util.GetSignableDataFromScript(g, txIndex, method, pkToConfig, weightToConfig)
signable, err := util.GetSignableDataFromScript(g, txIndex, method, pkToConfig, weightToConfig, sigAlgoToConfig)
if err != nil {
return
}
Expand All @@ -74,7 +76,7 @@ func MultiSig_ConfigKey(
}

if newPayload {
args := []cadence.Value{pkToConfig, weightToConfig}
args := []cadence.Value{pkToConfig, weightToConfig, sigAlgoToConfig}
return util.MultiSig_VaultNewPayload(g, sig, txIndex, method, args, signerAcct, vaultAcct, "0.0")
} else {
return util.MultiSig_VaultAddPayloadSignature(g, sig, txIndex, signerAcct, vaultAcct)
Expand Down
3 changes: 3 additions & 0 deletions lib/go/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ func AddVaultToAccount(
cadence.String(pk250_2[2:]),
}
multiSigKeyWeights := []cadence.Value{w1000, w500, w500, w250, w250}
sa := cadence.NewUInt8(1)
multiSigAlgos := []cadence.Value{sa, sa, sa, sa, sa}

e, err := g.TransactionFromFile(txFilename, txScript).
SignProposeAndPayAs(vaultAcct).
Argument(cadence.NewArray(multiSigPubKeys)).
Argument(cadence.NewArray(multiSigKeyWeights)).
Argument(cadence.NewArray(multiSigAlgos)).
Run()
events = util.ParseTestEvents(e)
return
Expand Down
3 changes: 3 additions & 0 deletions scripts/calc_signable_data.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub fun main(v: AnyStruct?): [UInt8] {
case Type<UInt64>():
let temp = value as? UInt64;
return temp!.toBigEndianBytes();
case Type<UInt8>():
let temp = value as? UInt8;
return temp!.toBigEndianBytes();
case Type<UFix64>():
let temp = value as? UFix64;
return temp!.toBigEndianBytes();
Expand Down
4 changes: 2 additions & 2 deletions transactions/create_vault.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FungibleToken from 0x{{.FungibleToken}}
import MultiSigFlowToken from 0x{{.MultiSigFlowToken}}
import OnChainMultiSig from 0x{{.OnChainMultiSig}}

transaction(multiSigPubKeys: [String], multiSigKeyWeights: [UFix64]) {
transaction(multiSigPubKeys: [String], multiSigKeyWeights: [UFix64], multiSigAlgos: [UInt8]) {

prepare(signer: AuthAccount) {

Expand Down Expand Up @@ -48,7 +48,7 @@ transaction(multiSigPubKeys: [String], multiSigKeyWeights: [UFix64]) {

// The transaction that creates the vault can also add required multiSig public keys to the multiSigManager
let s = signer.borrow<&MultiSigFlowToken.Vault>(from: MultiSigFlowToken.VaultStoragePath) ?? panic ("cannot borrow own resource")
s.addKeys(multiSigPubKeys: multiSigPubKeys, multiSigKeyWeights: multiSigKeyWeights)
s.addKeys(multiSigPubKeys: multiSigPubKeys, multiSigKeyWeights: multiSigKeyWeights, multiSigAlgos: multiSigAlgos)
}

}
2 changes: 1 addition & 1 deletion transactions/ownerUpdateKeyList.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ transaction (multiSigVaultAddr: Address) {
prepare(owner: AuthAccount) {
let s = owner.borrow<&MultiSigFlowToken.Vault>(from: MultiSigFlowToken.VaultStoragePath) ?? panic ("cannot borrow own resource")
let pka = OnChainMultiSig.PubKeyAttr(sa: 1, w: 0.2)
s.multiSigManager.configureKeys(pks: ["1234"], kws: [0.2])
s.multiSigManager.configureKeys(pks: ["1234"], kws: [0.2], sa: [1])
}
}
2 changes: 1 addition & 1 deletion transactions/pubUpdateKeyList.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ transaction (multiSigVaultAddr: Address) {
.borrow<&MultiSigFlowToken.Vault{OnChainMultiSig.PublicSigner}>()
?? panic("Could not borrow vault pub sig reference")

vaultRef.multiSigManager.configureKeys(pks: ["1234"], kws: [0.2])
vaultRef.multiSigManager.configureKeys(pks: ["1234"], kws: [0.2], sa: [1])
}
}

0 comments on commit 010fe72

Please sign in to comment.