Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ jobs:

- name: Create and fund an account
run: |
go run ./cmd/golembase account create
go run ./cmd/golembase account fund
printf "password" | go run ./cmd/golembase account create
printf "password" | go run ./cmd/golembase account fund
working-directory: ./gb-op-geth

- name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The repo also contains an example application to showcase how you can use this S

(Note: As an alternative to installing the demo CLI, you can build the [actual CLI](https://github.com/Golem-Base/golembase-op-geth/blob/main/cmd/golembase/README.md) as it's included in the golembase-op-geth repo.)

When you create a user, it will generate a private key file called `private.key` and store it in:
When you create a user, it will generate an encrypted keystore file with a password you provide called `wallet.json` and store it in:

- `~/.config/golembase/` on **Linux**
- `~/Library/Application Support/golembase/` on **macOS**
Expand Down Expand Up @@ -126,7 +126,7 @@ This is a basic TypeScript application that:
* `type GolemBaseCreate`: A type representing a create transaction in GolemBase
* `Annotation`: A type representing an annotation with a key and a value, used for efficient lookups

2. Reads the private key, which it locates through the `xdg-portable` module.
2. Reads the private key from a wallet, which it locates through the `xdg-portable` module.

3. Create a logger using the `tslog` TypeScript Logger

Expand Down
46 changes: 43 additions & 3 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as fs from "fs"
import { readFileSync } from "fs"
import { join } from "path"
import { stdin, stdout } from "process"
import { createInterface } from "readline";
import {
type ILogObj,
Logger
} from "tslog"
import xdg from "xdg-portable"
import { Wallet, getBytes } from "ethers"
import {
createClient,
formatEther,
Expand All @@ -13,7 +17,39 @@ import {
type AccountData,
} from "golem-base-sdk"

const keyBytes = fs.readFileSync(xdg.config() + '/golembase/private.key');
// Path to a golembase wallet
const walletPath = join(xdg.config(), 'golembase', 'wallet.json');
const keystore = readFileSync(walletPath, 'utf8');

/**
* Read password either from piped stdin or interactively from the terminal.
*/
async function readPassword(prompt: string = "Enter wallet password: "): Promise<string> {
if (stdin.isTTY) {
// Interactive prompt
const rl = createInterface({
input: stdin,
output: stdout,
terminal: true,
});

return new Promise((resolve) => {
rl.question(prompt, (password) => {
rl.close();
resolve(password.trim());
});
// Hide input for security
(rl as any)._writeToOutput = () => {};
});
} else {
// Input is piped
const chunks: Buffer[] = [];
for await (const chunk of stdin) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString().trim();
}
}

const encoder = new TextEncoder()
const decoder = new TextDecoder()
Expand All @@ -35,7 +71,11 @@ async function asyncFilter<T>(arr: T[], callback: (item: T) => Promise<boolean>)
};

async function main() {
const key: AccountData = new Tagged("privatekey", keyBytes)
log.info("Attempting to decrypt wallet", walletPath);
const wallet = Wallet.fromEncryptedJsonSync(keystore, await readPassword());
log.info("Successfully decrypted wallet for account", wallet.address);

const key: AccountData = new Tagged("privatekey", getBytes(wallet.privateKey))
const client = {
local: await createClient(
1337,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"packageManager": "pnpm@10.5.2",
"dependencies": {
"@types/node": "^22.15.32",
"ethers": "^6.15.0",
"tslog": "^4.9.3",
"viem": "^2.31.3"
},
Expand Down
74 changes: 74 additions & 0 deletions pnpm-lock.yaml

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

13 changes: 10 additions & 3 deletions test/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs'
import { readFileSync } from "fs"
import { join } from "path"
import {
expect
} from "chai"
Expand All @@ -8,6 +9,7 @@ import {
Logger
} from "tslog"
import xdg from "xdg-portable"
import { Wallet, getBytes } from "ethers"
import {
createClient,
type GolemBaseClient,
Expand All @@ -28,7 +30,12 @@ const log = new Logger<ILogObj>({
minLevel: 3,
})

const keyBytes = fs.readFileSync(xdg.config() + '/golembase/private.key');
// Path to a golembase wallet
const walletPath = join(xdg.config(), 'golembase', 'wallet.json');
// The password that the test wallet was encrypted with
const walletTestPassword = "password";
const keystore = readFileSync(walletPath, 'utf8');
const wallet = Wallet.fromEncryptedJsonSync(keystore, walletTestPassword);

let entitiesOwnedCount = 0
let entityKey: Hex = "0x"
Expand All @@ -37,7 +44,7 @@ let client: GolemBaseClient

describe("the golem-base client", () => {
it("can be created", async () => {
const key: AccountData = new Tagged("privatekey", keyBytes)
const key: AccountData = new Tagged("privatekey", getBytes(wallet.privateKey))
client = {
local: await createClient(
1337,
Expand Down
14 changes: 11 additions & 3 deletions test/internal/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs'
import { readFileSync } from "fs"
import { join } from "path"
import {
expect
} from "chai"
Expand All @@ -8,6 +9,7 @@ import {
Logger
} from "tslog"
import xdg from "xdg-portable"
import { Wallet, getBytes } from "ethers"
import {
internal,
type GolemBaseCreate,
Expand Down Expand Up @@ -61,7 +63,13 @@ async function ownerAddress(client: internal.GolemBaseClient): Promise<Hex> {
return (await client.walletClient.getAddresses())[0]
}

const keyBytes = fs.readFileSync(xdg.config() + '/golembase/private.key');
// Path to a golembase wallet
const walletPath = join(xdg.config(), 'golembase', 'wallet.json');
// The password that the test wallet was encrypted with
const walletTestPassword = "password";
const keystore = readFileSync(walletPath);
const wallet = Wallet.fromEncryptedJsonSync(keystore, walletTestPassword);

let client: GolemBaseClient

const data = generateRandomBytes(32)
Expand All @@ -73,7 +81,7 @@ let expirationBlock: number

describe("the internal golem-base client", () => {
it("can be created", async () => {
const key: AccountData = new Tagged("privatekey", keyBytes)
const key: AccountData = new Tagged("privatekey", getBytes(wallet.privateKey))
client = {
local: await internal.createClient(
1337,
Expand Down