Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit eceb9d6

Browse files
authored
Add support for advanced flow.json service key types (#237)
1 parent 1b630d2 commit eceb9d6

File tree

10 files changed

+123
-110
lines changed

10 files changed

+123
-110
lines changed

.changeset/big-crews-work.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@onflow/flow-js-testing": minor
3+
---
4+
5+
Allow loading service key from environment variables and files.
6+
7+
**BREAKING CHANGES**
8+
9+
- `getConfigValue` and `set` have been removed as these were just a confusing abstraction above the `@onflow/config` packages
10+
- They have been replaced by exporting they `config` instance directly from the package

package-lock.json

+2-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
},
5757
"dependencies": {
5858
"@onflow/fcl": "1.3.2",
59-
"@onflow/fcl-config": "^0.0.1",
6059
"@onflow/flow-cadut": "^0.3.0-stable-cadence.1",
6160
"elliptic": "^6.5.4",
6261
"esm": "^3.2.25",

src/config.js

-77
This file was deleted.

src/emulator/logger.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class Logger extends EventEmitter {
4141
super(options)
4242
this.handleMessage = this.handleMessage.bind(this)
4343
this.process = null
44+
this.setMaxListeners(100)
4445
}
4546

4647
/**

src/flow-config.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Flow JS Testing
3+
*
4+
* Copyright 2020-2021 Dapper Labs, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import path from "path"
20+
import fs from "fs"
21+
22+
const TARGET = "flow.json"
23+
let configPath = null
24+
let config = null
25+
26+
function isDir(dir) {
27+
return fs.lstatSync(dir).isDirectory()
28+
}
29+
30+
function listFiles(dir) {
31+
return new Set(fs.readdirSync(dir))
32+
}
33+
34+
function parentDir(dir) {
35+
return path.dirname(dir)
36+
}
37+
38+
function findTarget(dir) {
39+
if (!isDir(dir)) throw new Error(`Not a directory: ${dir}`)
40+
return listFiles(dir).has(TARGET) ? path.resolve(dir, TARGET) : null
41+
}
42+
43+
export function getConfigPath(dir) {
44+
if (configPath != null) return configPath
45+
46+
const filePath = findTarget(dir)
47+
if (filePath == null) {
48+
if (dir === parentDir(dir)) {
49+
throw new Error("No flow.json found")
50+
}
51+
return getConfigPath(parentDir(dir))
52+
}
53+
54+
configPath = filePath
55+
return configPath
56+
}
57+
58+
export function flowConfig() {
59+
if (config != null) return config
60+
61+
const filePath = getConfigPath(process.cwd())
62+
const content = fs.readFileSync(filePath, "utf8")
63+
config = JSON.parse(content)
64+
65+
return config
66+
}

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
export {init} from "./init"
20-
export {set, getConfigValue} from "./config"
20+
export {config} from "@onflow/fcl"
2121
export {
2222
getTemplate,
2323
getScriptCode,

src/init.js

+42-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
* limitations under the License.
1717
*/
1818

19-
import {set} from "./config"
19+
import {config} from "@onflow/fcl"
20+
import {flowConfig, getConfigPath} from "./flow-config"
21+
import path from "path"
22+
import fs from "fs"
23+
24+
const DEFAULT_COMPUTE_LIMIT = 9999
2025

2126
/**
2227
* Inits framework variables, storing private key of service account and base path
@@ -30,12 +35,42 @@ export const init = async (basePath, props = {}) => {
3035
pkey = "48a1f554aeebf6bf9fe0d7b5b79d080700b073ee77909973ea0b2f6fbc902",
3136
} = props
3237

33-
set("PRIVATE_KEY", process.env.PK, "accounts/emulator-account/key", pkey)
34-
set(
38+
const cfg = flowConfig()
39+
40+
config().put("PRIVATE_KEY", getServiceKey(cfg) ?? pkey)
41+
config().put(
3542
"SERVICE_ADDRESS",
36-
process.env.SERVICE_ADDRESS,
37-
"accounts/emulator-account/address",
38-
"f8d6e0586b0a20c7"
43+
cfg?.accounts?.["emulator-account"]?.address ?? "f8d6e0586b0a20c7"
3944
)
40-
set("BASE_PATH", process.env.BASE_PATH, "testing/paths", basePath)
45+
config().put("BASE_PATH", cfg?.testing?.paths ?? basePath)
46+
config().put("fcl.limit", DEFAULT_COMPUTE_LIMIT)
47+
}
48+
49+
function getServiceKey(cfg) {
50+
const value = cfg?.accounts?.["emulator-account"]?.key
51+
if (value) {
52+
if (typeof value === "object") {
53+
switch (value.type) {
54+
case "hex":
55+
return value.privateKey
56+
case "file": {
57+
const configDir = path.dirname(getConfigPath())
58+
const resovledPath = path.resolve(configDir, value.location)
59+
return fs.readFileSync(resovledPath, "utf8")
60+
}
61+
default:
62+
return null
63+
}
64+
} else if (typeof value === "string") {
65+
if (value.startsWith("$")) {
66+
return process.env[value.slice(1)]
67+
} else {
68+
return value
69+
}
70+
} else {
71+
return null
72+
}
73+
}
74+
75+
return null
4176
}

src/interaction.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import * as fcl from "@onflow/fcl"
2020
import {resolveArguments} from "@onflow/flow-cadut"
21-
import {DEFAULT_COMPUTE_LIMIT} from "./config"
2221
import {authorization} from "./crypto"
2322
import emulator from "./emulator/emulator"
2423
import {getTransactionCode, getScriptCode, defaultsByName} from "./file"
@@ -66,7 +65,7 @@ export const extractParameters = ixType => {
6665
}
6766

6867
// Check that limit is always set
69-
ixLimit = ixLimit || DEFAULT_COMPUTE_LIMIT
68+
ixLimit = ixLimit || (await fcl.config().get("fcl.limit"))
7069

7170
if (ixName) {
7271
const getIxTemplate =

test/basic/config.test.js

-9
This file was deleted.

0 commit comments

Comments
 (0)