Skip to content
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ target
**/node_modules
**/config.json

# Do not remove the following line
.test-env
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Where CONFIG_FILE is the name of a file inside the project, with the following f
}
```

Some scripts that run on test networks rely on a 24-word mnemonic which must be set on a `.test-env` file at src:
```js
SEED ="soda water ..."
```
Note: For safety, always use a mnemonic intended only for testing. Never use real or production mnemonics.

## Run Instructions

### Server
Expand Down Expand Up @@ -94,7 +100,7 @@ First, a channel is created with an initial deposit of 6 tokens. Then, an update
```

### Test with testnet
To emulate this operations on preview, complete the `.env` file at the src with:
To emulate this operations on testnet, complete the `.test-env` file at the src with:
```js
SEED="decrease cash kangaroo ..."
```
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/Channel"
$ref: "#/components/schemas/Channel"
/channels-from-sender:
get:
summary: Get channels by sender
Expand Down
22 changes: 20 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const envSchema = z
PROVIDER_URL: z.string(),
NETWORK: z.string(),
CONFIG_FILE: z.string(),
SEED: z.string().optional(),
})
.readonly();

Expand All @@ -32,4 +31,23 @@ const configFileSchema = z.object({
}),
});
const config = configFileSchema.parse(configFile);
export { config, env };

// For testing purposes only
const seedSchema = z
.string()
.refine(
(s) =>
s.startsWith("SEED=") &&
s.split("=").length === 2 &&
s.split("=").pop()?.split(" ").length === 24,
{
message:
"Invalid .test-env format. See the README file for the correct format.",
},
)
.transform((s) => s.split("=").pop() as string);
const testEnvFile = fs.readFileSync("./.test-env", "utf-8");
const testEnv = {
SEED: seedSchema.parse(testEnvFile),
};
export { config, env, testEnv };
7 changes: 5 additions & 2 deletions src/offchain/test/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Blockfrost, Lucid } from "@spacebudz/lucid";
import { env } from "../../config.ts";
import { testEnv, env } from "../../config.ts";
import { deployScript } from "../builders/deploy-script.ts";

const lucid = new Lucid({
provider: new Blockfrost(env.PROVIDER_URL, env.PROVIDER_PROJECT_ID),
});
lucid.selectWalletFromSeed(process.env.SEED as string);

if (!testEnv.SEED) throw new Error("SEED not found in .test-env file");

lucid.selectWalletFromSeed(testEnv.SEED);
const { cbor } = await deployScript(lucid);
const txId = await lucid
.fromTx(cbor)
Expand Down
8 changes: 6 additions & 2 deletions src/offchain/test/sign-msg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Data } from "@spacebudz/lucid";
import { getCMLPrivateKey, signMessage } from "./utils.ts";
import { env } from "../../config.ts";
import { testEnv } from "../../config.ts";
import assert from "assert";

const args = process.argv.slice(2);
Expand Down Expand Up @@ -31,9 +31,13 @@ assert(
"USAGE: npm run sign -- -c <channelId> -n <nonce> -a <amount>",
);

if (!testEnv.SEED) {
throw new Error("SEED not found in .test-env file");
}

console.log(
signMessage(
getCMLPrivateKey(env.SEED as string),
getCMLPrivateKey(testEnv.SEED),
Data.to(
[nonce, channelId, amount] as [bigint, string, bigint],
Data.Tuple([Data.Integer(), Data.Bytes(), Data.Integer()]),
Expand Down
6 changes: 3 additions & 3 deletions src/offchain/test/testnet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blockfrost, Crypto, Lucid } from "@spacebudz/lucid";
import promptSync from "prompt-sync";
import { env } from "../../config.ts";
import { env, testEnv } from "../../config.ts";
import { ClaimChannelParams } from "../../shared/api-types.ts";
import { buildMessage } from "../builders/build-message.ts";
import { getChannelById } from "../queries/channel-by-id.ts";
Expand Down Expand Up @@ -28,8 +28,8 @@ const lucid = new Lucid({
),
});

const seed = env.SEED;
if (!seed) throw Error("Unable to read wallet's seed from env");
const seed = testEnv.SEED;
if (!seed) throw Error("Unable to read wallet's seed from .test-env");
lucid.selectWalletFromSeed(seed);
const { privateKey, publicKey } = Crypto.seedToDetails(seed, 0, "Payment");
const address = await lucid.wallet.address();
Expand Down