Skip to content

Commit

Permalink
Merge branch 'main' into bush/encrypt-web
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar authored Dec 13, 2024
2 parents 22ccdd3 + e81f71c commit 3921e55
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 18 deletions.
5 changes: 5 additions & 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 Expand Up @@ -82,6 +83,7 @@ services:
image: keychainmdip/satoshi-mediator
environment:
- KC_GATEKEEPER_URL=http://gatekeeper:4224
- KC_KEYMASTER_URL=http://keymaster:4226
- KC_NODE_ID=${KC_NODE_ID}
- KC_ENCRYPTED_PASSPHRASE=${KC_ENCRYPTED_PASSPHRASE}
- KC_SAT_CHAIN=TFTC
Expand All @@ -104,6 +106,7 @@ services:
depends_on:
- tftc-node
- gatekeeper
- keymaster

tbtc-node:
image: keychainmdip/bitcoin-core:v28.0
Expand All @@ -117,6 +120,7 @@ services:
image: keychainmdip/satoshi-mediator
environment:
- KC_GATEKEEPER_URL=http://gatekeeper:4224
- KC_KEYMASTER_URL=http://keymaster:4226
- KC_NODE_ID=${KC_NODE_ID}
- KC_ENCRYPTED_PASSPHRASE=${KC_ENCRYPTED_PASSPHRASE}
- KC_SAT_CHAIN=TBTC
Expand All @@ -139,6 +143,7 @@ services:
depends_on:
- tbtc-node
- gatekeeper
- keymaster

ipfs-mediator:
build:
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 baseWallet;
let cachedWallet;

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

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

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

return cachedWallet;
}
10 changes: 10 additions & 0 deletions packages/keymaster/src/keymaster-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ export async function resolveDID(name) {
}
}

export async function createAsset(data, options = {}) {
try {
const response = await axios.post(`${URL}/api/v1/assets`, { data, options });
return response.data.did;
}
catch (error) {
throwError(error);
}
}

export async function resolveAsset(name) {
try {
const response = await axios.get(`${URL}/api/v1/assets/${name}`);
Expand Down
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_PASSPHRASE=
# 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
7 changes: 7 additions & 0 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 @@ -1031,6 +1033,11 @@ function getDBWallet() {
wallet = db_wallet_enc;
}

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

return wallet;
}

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;
10 changes: 8 additions & 2 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 @@ -574,8 +575,8 @@ v1router.post('/schemas/:id/template/', async (req, res) => {

v1router.post('/assets/', async (req, res) => {
try {
const { asset, options } = req.body;
const did = await keymaster.createAsset(asset, options);
const { data, options } = req.body;
const did = await keymaster.createAsset(data, options);
res.json({ did });
} catch (error) {
res.status(500).send({ error: error.toString() });
Expand Down Expand Up @@ -698,6 +699,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
1 change: 1 addition & 0 deletions services/mediators/satoshi/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dotenv.config();
const config = {
nodeID: process.env.KC_NODE_ID,
gatekeeperURL: process.env.KC_GATEKEEPER_URL || 'http://localhost:4224',
keymasterURL: process.env.KC_KEYMASTER_URL,
keymasterPassphrase: process.env.KC_ENCRYPTED_PASSPHRASE,
chain: process.env.KC_SAT_CHAIN || 'BTC',
network: process.env.KC_SAT_NETWORK || 'mainnet',
Expand Down
39 changes: 26 additions & 13 deletions services/mediators/satoshi/src/satoshi-mediator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import fs from 'fs';
import BtcClient from 'bitcoin-core';
import * as gatekeeper from '@mdip/gatekeeper/sdk';
import * as keymaster from '@mdip/keymaster/lib';
import * as keymaster_lib from '@mdip/keymaster/lib';
import * as keymaster_sdk from '@mdip/keymaster/sdk';
import * as wallet_json from '@mdip/keymaster/db/json';
import * as wallet_enc from '@mdip/keymaster/db/json/enc';
import * as cipher from '@mdip/cipher/node';
import config from './config.js';
import { InvalidParameterError } from '@mdip/common/errors';

const REGISTRY = config.chain;
let keymaster;

const client = new BtcClient({
network: config.network,
Expand Down Expand Up @@ -132,8 +134,6 @@ async function importBatch(item) {
return;
}

console.log(JSON.stringify(item, null, 4));

const batch = [];

for (let i = 0; i < queue.length; i++) {
Expand All @@ -151,8 +151,6 @@ async function importBatch(item) {
});
}

// console.log(JSON.stringify(batch, null, 4));

try {
item.imported = await gatekeeper.importBatch(batch);
item.processed = await gatekeeper.processEvents();
Expand Down Expand Up @@ -338,9 +336,10 @@ async function anchorBatch() {
}

const batch = await gatekeeper.getQueue(REGISTRY);
console.log(JSON.stringify(batch, null, 4));

if (batch.length > 0) {
console.log(JSON.stringify(batch, null, 4));

const did = await keymaster.createAsset({ batch }, { registry: 'hyperswarm', controller: config.nodeID });
const txid = await createOpReturnTxn(did);

Expand All @@ -367,7 +366,7 @@ async function anchorBatch() {
}
}
else {
console.log('empty batch');
console.log(`empty ${REGISTRY} queue`);
}
}

Expand Down Expand Up @@ -502,15 +501,29 @@ async function main() {
chatty: true,
});

let wallet = wallet_json;
if (config.keymasterURL) {
keymaster = keymaster_sdk;
await keymaster.start({
url: config.keymasterURL,
waitUntilReady: true,
intervalSeconds: 5,
chatty: true,
});
}
else {
keymaster = keymaster_lib;

let wallet = wallet_json;

if (config.keymasterPassphrase) {
wallet_enc.setPassphrase(config.keymasterPassphrase);
wallet_enc.setWallet(wallet);
wallet = wallet_enc;
}

if (config.keymasterPassphrase) {
wallet_enc.setPassphrase(config.keymasterPassphrase);
wallet_enc.setWallet(wallet);
wallet = wallet_enc;
await keymaster.start({ gatekeeper, wallet, cipher });
}

await keymaster.start({ gatekeeper, wallet, cipher });
await waitForNodeID();

if (config.importInterval > 0) {
Expand Down

0 comments on commit 3921e55

Please sign in to comment.