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

feat: Added wallet cache #477

Merged
merged 9 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ services:
- KC_KEYMASTER_PORT=4226
- KC_GATEKEEPER_URL=http://gatekeeper:4224
- KC_ENCRYPTED_PASSPHRASE=${KC_ENCRYPTED_PASSPHRASE}
- KC_WALLET_CACHE=${KC_WALLET_CACHE}
volumes:
- ./data:/app/keymaster/data
user: "${KC_UID}:${KC_GID}"
Expand Down
1 change: 1 addition & 0 deletions packages/keymaster/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"./sdk": "./src/keymaster-sdk.js",
"./db/json": "./src/db-wallet-json.js",
"./db/json/enc": "./src/db-wallet-json-enc.js",
"./db/cache": "./src/db-wallet-cache.js",
"./db/web": "./src/db-wallet-web.js"
},
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions packages/keymaster/src/db-wallet-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let actualWallet;
let cachedWallet;

export function setWallet(wallet) {
actualWallet = wallet;
}

export function saveWallet(wallet, overwrite = false) {
cachedWallet = wallet;
return actualWallet.saveWallet(wallet, overwrite);
}

export function loadWallet() {
if (!cachedWallet) {
cachedWallet = actualWallet.loadWallet();
}

return cachedWallet;
}
5 changes: 3 additions & 2 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ KC_GATEKEEPER_REGISTRIES=hyperswarm,TBTC,TFTC
KC_GATEKEEPER_PORT=4224
KC_GATEKEEPER_GC_INTERVAL=60

# Wallet
KC_ENCRYPTED_WALLET=false
# Keymaster
KC_ENCRYPTED_PASSPHRASE=""
KC_WALLET_CACHE=false

# CLI
KC_GATEKEEPER_URL=http://localhost:4224
Expand Down
2 changes: 1 addition & 1 deletion scripts/admin-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as wallet from '@mdip/keymaster/db/json';
import * as cipher from '@mdip/cipher/node';

dotenv.config();
const gatekeeperURL = process.env.KC_CLI_GATEKEEPER_URL || 'http://localhost:4224';
const gatekeeperURL = process.env.KC_GATEKEEPER_URL || 'http://localhost:4224';

program
.version('1.0.0')
Expand Down
14 changes: 12 additions & 2 deletions scripts/keychain-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as keymaster_lib from '@mdip/keymaster/lib';
import * as keymaster_sdk from '@mdip/keymaster/sdk';
import * as db_wallet_json from '@mdip/keymaster/db/json';
import * as db_wallet_enc from '@mdip/keymaster/db/json/enc';
import * as db_wallet_cache from '@mdip/keymaster/db/cache';
import * as cipher from '@mdip/cipher/node';

dotenv.config();
Expand All @@ -16,6 +17,7 @@ const gatekeeperURL = process.env.KC_GATEKEEPER_URL || 'http://localhost:4224';
const keymasterURL = process.env.KC_KEYMASTER_URL;

const keymasterPassphrase = process.env.KC_ENCRYPTED_PASSPHRASE;
const walletCache = process.env.KC_WALLET_CACHE ? process.env.KC_WALLET_CACHE === 'true' : false;

const UPDATE_OK = "OK";
const UPDATE_FAILED = "Update failed";
Expand Down Expand Up @@ -1006,11 +1008,19 @@ program
});

function getDBWallet() {
let wallet = db_wallet_json;

if (keymasterPassphrase) {
db_wallet_enc.setPassphrase(keymasterPassphrase);
return db_wallet_enc;
wallet = db_wallet_enc;
}

if (walletCache) {
db_wallet_cache.setWallet(wallet);
wallet = db_wallet_cache;
}
return db_wallet_json;

return wallet;
}

async function run() {
Expand Down
1 change: 1 addition & 0 deletions services/keymaster/server/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config = {
gatekeeperURL: process.env.KC_GATEKEEPER_URL || 'http://localhost:4224',
keymasterPort: process.env.KC_KEYMASTER_PORT ? parseInt(process.env.KC_KEYMASTER_PORT) : 4226,
keymasterPassphrase: process.env.KC_ENCRYPTED_PASSPHRASE,
walletCache: process.env.KC_WALLET_CACHE ? process.env.KC_WALLET_CACHE === 'true' : false,
};

export default config;
6 changes: 6 additions & 0 deletions services/keymaster/server/src/keymaster-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as gatekeeper from '@mdip/gatekeeper/sdk';
import * as keymaster from '@mdip/keymaster/lib';
import * as wallet_json from '@mdip/keymaster/db/json';
import * as wallet_enc from '@mdip/keymaster/db/json/enc';
import * as wallet_cache from '@mdip/keymaster/db/cache';
import * as cipher from '@mdip/cipher/node';
import config from './config.js';
const app = express();
Expand Down Expand Up @@ -697,6 +698,11 @@ app.listen(port, async () => {
wallet = wallet_enc;
}

if (config.walletCache) {
wallet_cache.setWallet(wallet);
wallet = wallet_cache;
}

await keymaster.start({ gatekeeper, wallet, cipher });
console.log(`keymaster server running on port ${port}`);

Expand Down
Loading