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

feat: update sdk scripts to Cadence 1.0 #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
109 changes: 58 additions & 51 deletions Sources/Cadence/CommonCadence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ extension Flow {
/// The cadence code for adding key to account
static let addKeyToAccount = """
import Crypto

transaction(publicKey: String, signatureAlgorithm: UInt8, hashAlgorithm: UInt8, weight: UFix64) {
prepare(signer: AuthAccount) {
prepare(signer: auth(BorrowValue | Storage) &Account) {
let key = PublicKey(
publicKey: publicKey.decodeHex(),
signatureAlgorithm: SignatureAlgorithm(rawValue: signatureAlgorithm)!
)
signer.keys.add(

let account = Account(payer: signer)
account.keys.add(
publicKey: key,
hashAlgorithm: HashAlgorithm(rawValue: hashAlgorithm)!,
weight: weight
Expand All @@ -43,30 +46,33 @@ extension Flow {
/// The cadence code for adding contract to account
static let addContractToAccount = """
transaction(name: String, code: String) {
prepare(signer: AuthAccount) {
signer.contracts.add(name: name, code: code.decodeHex())
prepare(signer: auth(Contracts, AddContract) &Account) {
signer.contracts.add(name: name, code: code.utf8)
}
}
"""

/// The cadence code for creating account
static let createAccount = """
import Crypto

transaction(publicKey: String, signatureAlgorithm: UInt8, hashAlgorithm: UInt8, weight: UFix64, contracts: {String: String}) {
prepare(signer: AuthAccount) {
prepare(signer: auth(BorrowValue | Storage) &Account) {
let account = Account(payer: signer)

let key = PublicKey(
publicKey: publicKey.decodeHex(),
signatureAlgorithm: SignatureAlgorithm(rawValue: signatureAlgorithm)!
)
let account = AuthAccount(payer: signer)

account.keys.add(
publicKey: key,
hashAlgorithm: HashAlgorithm(rawValue: hashAlgorithm)!,
weight: weight
)

for contract in contracts.keys {
acct.contracts.add(name: contract, code: contracts[contract]!.decodeHex())
account.contracts.add(name: contract, code: contracts[contract]!.decodeHex())
}
}
}
Expand All @@ -75,16 +81,16 @@ extension Flow {
/// The cadence code for removing account key by index
static let removeAccountKeyByIndex = """
transaction(keyIndex: Int) {
prepare(signer: AuthAccount) {
signer.removePublicKey(keyIndex)
prepare(signer: auth(Keys) &Account) {
signer.keys.revoke(keyIndex: keyIndex)
}
}
"""

/// The cadence code for removing contract from account
static let removeContractFromAccount = """
transaction(name: String) {
prepare(signer: AuthAccount) {
transaction(name: String) {
prepare(signer: auth(RemoveContract) &Account) {
signer.contracts.remove(name: name)
}
}
Expand All @@ -93,17 +99,17 @@ extension Flow {
/// The cadence code for updating contract from account
static let updateContractOfAccount = """
transaction(name: String, code: String) {
prepare(signer: AuthAccount) {
signer.contracts.update__experimental(name: name, code: code.decodeHex())
prepare(signer: auth(UpdateContract) &Account) {
signer.contracts.update(name: name, code: code.utf8)
}
}
"""

static let accountStorage = """
pub struct StorageInfo {
pub let capacity: UInt64
pub let used: UInt64
pub let available: UInt64
access(all) struct StorageInfo {
access(all) let capacity: UInt64
access(all) let used: UInt64
access(all) let available: UInt64

init(capacity: UInt64, used: UInt64, available: UInt64) {
self.capacity = capacity
Expand All @@ -112,61 +118,62 @@ extension Flow {
}
}

pub fun main(addr: Address): StorageInfo {
let acct = getAccount(addr)
return StorageInfo(capacity: acct.storageCapacity,
used: acct.storageUsed,
available: acct.storageCapacity - acct.storageUsed)
access(all) fun main(addr: Address): StorageInfo {
let acct = getAccount(addr)
return StorageInfo(capacity: acct.storage.capacity,
used: acct.storage.used,
available: acct.storage.capacity - acct.storage.used)
}
"""

/// The cadence code to verify user signature
static let verifyUserSignature = """
import Crypto

pub fun main(
message: String,
rawPublicKeys: [String],
weights: [UFix64],
signAlgos: [UInt8],
hashAlgos: [UInt8],
signatures: [String],
access(all) fun main(
message: String,
rawPublicKeys: [String],
weights: [UFix64],
signAlgos: [UInt8],
hashAlgos: [UInt8],
signatures: [String],
): Bool {

let keyList = Crypto.KeyList()
let keyList = Crypto.KeyList()

var i = 0
for rawPublicKey in rawPublicKeys {
var i = 0
for rawPublicKey in rawPublicKeys {
keyList.add(
PublicKey(
PublicKey(
publicKey: rawPublicKey.decodeHex(),
signatureAlgorithm: SignatureAlgorithm(rawValue: signAlgos[i])!
),
hashAlgorithm: HashAlgorithm(rawValue: hashAlgos[i])!,
weight: weights[i],
),
hashAlgorithm: HashAlgorithm(rawValue: hashAlgos[i])!,
weight: weights[i],
)
i = i + 1
}
}

let signatureSet: [Crypto.KeyListSignature] = []
let signatureSet: [Crypto.KeyListSignature] = []

var j = 0
for signature in signatures {
var j = 0
for signature in signatures {
signatureSet.append(
Crypto.KeyListSignature(
keyIndex: j,
signature: signature.decodeHex()
)
Crypto.KeyListSignature(
keyIndex: j,
signature: signature.decodeHex()
)
)
j = j + 1
}
j = j + 1
}

let signedData = message.decodeHex()
let signedData = message.decodeHex()

return keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)
return keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)
}
"""
}
Expand Down
Loading