Skip to content

Commit

Permalink
Add support for file and env service key types
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed May 24, 2024
1 parent 1b630d2 commit ca10ea7
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 100 deletions.
10 changes: 10 additions & 0 deletions .changeset/big-crews-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@onflow/flow-js-testing": minor
---

Allow loading service key from environment variables and files.

**BREAKING CHANGES**

- `getConfigValue` and `set` have been removed as these were just a confusing abstraction above the `@onflow/config` packages
- They have been replaced by exporting they `config` instance directly from the package
15 changes: 2 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"dependencies": {
"@onflow/fcl": "1.3.2",
"@onflow/fcl-config": "^0.0.1",
"@onflow/flow-cadut": "^0.3.0-stable-cadence.1",
"elliptic": "^6.5.4",
"esm": "^3.2.25",
Expand Down
77 changes: 0 additions & 77 deletions src/config.js

This file was deleted.

48 changes: 48 additions & 0 deletions src/flow-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from "path"
import fs from "fs"

const TARGET = "flow.json"
let configPath = null
let config = null

function isDir(dir) {
return fs.lstatSync(dir).isDirectory()
}

function listFiles(dir) {
return new Set(fs.readdirSync(dir))
}

function parentDir(dir) {
return path.dirname(dir)
}

function findTarget(dir) {
if (!isDir(dir)) throw new Error(`Not a directory: ${dir}`)
return listFiles(dir).has(TARGET) ? path.resolve(dir, TARGET) : null
}

export function getConfigPath(dir) {
if (configPath != null) return configPath

const filePath = findTarget(dir)
if (filePath == null) {
if (dir === parentDir(dir)) {
throw new Error("No flow.json found")
}
return getConfigPath(parentDir(dir))
}

configPath = filePath
return configPath
}

export function flowConfig() {
if (config != null) return config

const filePath = getConfigPath(process.cwd())
const content = fs.readFileSync(filePath, "utf8")
config = JSON.parse(content)

return config
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

export {init} from "./init"
export {set, getConfigValue} from "./config"
export {config} from "@onflow/fcl"
export {
getTemplate,
getScriptCode,
Expand Down
44 changes: 38 additions & 6 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* limitations under the License.
*/

import {set} from "./config"
import {config} from "@onflow/fcl"
import {flowConfig, getConfigPath} from "./flow-config"

const DEFAULT_COMPUTE_LIMIT = 9999

/**
* Inits framework variables, storing private key of service account and base path
Expand All @@ -30,12 +33,41 @@ export const init = async (basePath, props = {}) => {
pkey = "48a1f554aeebf6bf9fe0d7b5b79d080700b073ee77909973ea0b2f6fbc902",
} = props

set("PRIVATE_KEY", process.env.PK, "accounts/emulator-account/key", pkey)
set(
const cfg = flowConfig()

config().put("PRIVATE_KEY", getServiceKey(cfg), pkey)
config().put(
"SERVICE_ADDRESS",
process.env.SERVICE_ADDRESS,
"accounts/emulator-account/address",
cfg?.accounts?.["emulator-account"]?.address,
"f8d6e0586b0a20c7"
)
set("BASE_PATH", process.env.BASE_PATH, "testing/paths", basePath)
config().put("BASE_PATH", cfg?.testing?.paths, basePath)
config().put("fcl.limit", DEFAULT_COMPUTE_LIMIT)
}

function getServiceKey(cfg) {
const value = cfg?.accounts?.["emulator-account"]?.key
if (value) {
if (typeof value === "object") {
switch (value.type) {
case "hex":
return value.privateKey
case "file":
const resovledPath = path.resolve(getConfigPath(), value.location)
return fs.readFileSync(resovledPath, "utf8")
default:
return null
}
} else if (typeof value === "string") {
if (value.startsWith("$")) {
return process.env[value.slice(1)]
} else {
return value
}
} else {
return null
}
}

return null
}
3 changes: 1 addition & 2 deletions src/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import * as fcl from "@onflow/fcl"
import {resolveArguments} from "@onflow/flow-cadut"
import {DEFAULT_COMPUTE_LIMIT} from "./config"
import {authorization} from "./crypto"
import emulator from "./emulator/emulator"
import {getTransactionCode, getScriptCode, defaultsByName} from "./file"
Expand Down Expand Up @@ -66,7 +65,7 @@ export const extractParameters = ixType => {
}

// Check that limit is always set
ixLimit = ixLimit || DEFAULT_COMPUTE_LIMIT
ixLimit = ixLimit || (await fcl.config().get("fcl.limit"))

if (ixName) {
const getIxTemplate =
Expand Down

0 comments on commit ca10ea7

Please sign in to comment.