From 68dbe3f2bcec1dd44bac725cbde51bd4231ca516 Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Thu, 12 Dec 2024 10:53:01 -0500 Subject: [PATCH] Updated satoshi-mediator to use keymaster SDK --- docker-compose.yml | 5 ++- packages/keymaster/src/db-wallet-cache.js | 8 ++-- .../keymaster/server/src/keymaster-api.js | 1 + services/mediators/satoshi/src/config.js | 1 + .../mediators/satoshi/src/satoshi-mediator.js | 40 +++++++++++++------ 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5df1ee1c..9d1d3d09 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -83,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 @@ -105,12 +106,12 @@ services: depends_on: - tftc-node - gatekeeper + - keymaster tbtc-node: image: keychainmdip/bitcoin-core:v28.0 volumes: - ./data/tbtc:/root/.bitcoin - tbtc-mediator: build: context: . @@ -118,6 +119,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 @@ -140,6 +142,7 @@ services: depends_on: - tbtc-node - gatekeeper + - keymaster ipfs-mediator: build: diff --git a/packages/keymaster/src/db-wallet-cache.js b/packages/keymaster/src/db-wallet-cache.js index 14a887ac..247fd448 100644 --- a/packages/keymaster/src/db-wallet-cache.js +++ b/packages/keymaster/src/db-wallet-cache.js @@ -1,18 +1,18 @@ -let actualWallet; +let baseWallet; let cachedWallet; export function setWallet(wallet) { - actualWallet = wallet; + baseWallet = wallet; } export function saveWallet(wallet, overwrite = false) { cachedWallet = wallet; - return actualWallet.saveWallet(wallet, overwrite); + return baseWallet.saveWallet(wallet, overwrite); } export function loadWallet() { if (!cachedWallet) { - cachedWallet = actualWallet.loadWallet(); + cachedWallet = baseWallet.loadWallet(); } return cachedWallet; diff --git a/services/keymaster/server/src/keymaster-api.js b/services/keymaster/server/src/keymaster-api.js index 6a2ae183..6b0947c5 100644 --- a/services/keymaster/server/src/keymaster-api.js +++ b/services/keymaster/server/src/keymaster-api.js @@ -695,6 +695,7 @@ app.listen(port, async () => { if (config.keymasterPassphrase) { wallet_enc.setPassphrase(config.keymasterPassphrase); + wallet_enc.setWallet(wallet); wallet = wallet_enc; } diff --git a/services/mediators/satoshi/src/config.js b/services/mediators/satoshi/src/config.js index 67366e43..567f8f2f 100644 --- a/services/mediators/satoshi/src/config.js +++ b/services/mediators/satoshi/src/config.js @@ -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', diff --git a/services/mediators/satoshi/src/satoshi-mediator.js b/services/mediators/satoshi/src/satoshi-mediator.js index 48e1a5e4..67a9a183 100644 --- a/services/mediators/satoshi/src/satoshi-mediator.js +++ b/services/mediators/satoshi/src/satoshi-mediator.js @@ -1,7 +1,8 @@ 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'; @@ -9,6 +10,7 @@ import config from './config.js'; import { InvalidParameterError } from '@mdip/common/errors'; const REGISTRY = config.chain; +let keymaster; const client = new BtcClient({ network: config.network, @@ -495,21 +497,35 @@ async function main() { return; } - await gatekeeper.start({ - url: config.gatekeeperURL, - waitUntilReady: true, - intervalSeconds: 5, - chatty: true, - }); + if (config.keymasterURL) { + keymaster = keymaster_sdk; + await keymaster.start({ + url: config.keymasterURL, + waitUntilReady: true, + intervalSeconds: 5, + chatty: true, + }); + } + else { + keymaster = keymaster_lib; + await gatekeeper.start({ + url: config.gatekeeperURL, + waitUntilReady: true, + intervalSeconds: 5, + chatty: true, + }); + + let wallet = wallet_json; - 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 = wallet_enc; + await keymaster.start({ gatekeeper, wallet, cipher }); } - await keymaster.start({ gatekeeper, wallet, cipher }); await waitForNodeID(); if (config.importInterval > 0) {