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

Add support for advanced flow.json service key types #237

Merged
merged 5 commits into from
May 24, 2024
Merged
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
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.

1 change: 1 addition & 0 deletions src/emulator/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Logger extends EventEmitter {
super(options)
this.handleMessage = this.handleMessage.bind(this)
this.process = null
this.setMaxListeners(100)
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/flow-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Flow JS Testing
*
* Copyright 2020-2021 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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
49 changes: 42 additions & 7 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* limitations under the License.
*/

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

const DEFAULT_COMPUTE_LIMIT = 9999

/**
* Inits framework variables, storing private key of service account and base path
Expand All @@ -30,12 +35,42 @@
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",
"f8d6e0586b0a20c7"
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 configDir = path.dirname(getConfigPath())
const resovledPath = path.resolve(configDir, value.location)
return fs.readFileSync(resovledPath, "utf8")
}

Check warning on line 60 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
default:
return null
}

Check warning on line 63 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} else if (typeof value === "string") {
if (value.startsWith("$")) {
return process.env[value.slice(1)]
} else {
return value
}
} else {
return null
}

Check warning on line 72 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

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
9 changes: 0 additions & 9 deletions test/basic/config.test.js

This file was deleted.

Loading