Skip to content

Commit

Permalink
feat: different seed for EVM based chains
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Sep 18, 2024
1 parent 684d768 commit 125d4d6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
8 changes: 4 additions & 4 deletions boltzr/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub struct GlobalConfig {
#[serde(rename = "otlpEndpoint")]
pub otlp_endpoint: Option<String>,

#[serde(rename = "mnemonicpath")]
pub mnemonic_path: Option<String>,
#[serde(rename = "mnemonicpathEvm")]
pub mnemonic_path_evm: Option<String>,

pub postgres: crate::db::Config,
pub rsk: Option<crate::evm::Config>,
Expand All @@ -52,8 +52,8 @@ pub fn parse_config(path: &str) -> Result<GlobalConfig, Box<dyn Error>> {
let mut config = toml::from_str::<GlobalConfig>(fs::read_to_string(path)?.as_ref())?;
trace!("Read config: {:#}", serde_json::to_string_pretty(&config)?);

if config.mnemonic_path.is_none() {
config.mnemonic_path = Some(
if config.mnemonic_path_evm.is_none() {
config.mnemonic_path_evm = Some(
Path::new(path)
.parent()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion boltzr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn main() {
let refund_signer = if let Some(rsk_config) = config.rsk {
Some(
evm::refund_signer::LocalRefundSigner::new_mnemonic_file(
config.mnemonic_path.unwrap(),
config.mnemonic_path_evm.unwrap(),
&rsk_config,
)
.await
Expand Down
1 change: 1 addition & 0 deletions lib/Boltz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class Boltz {
this.walletManager = new WalletManager(
this.logger,
this.config.mnemonicpath,
this.config.mnemonicpathEvm,
walletCurrencies,
this.ethereumManagers,
);
Expand Down
11 changes: 7 additions & 4 deletions lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ChainConfig = {
zmqpubhashblock?: string;

// API endpoint of a MempoolSpace instance running on the chain of the configured client
// Comma seperated for multiple endpoints
// Comma separated for multiple endpoints
mempoolSpace?: string;
};

Expand Down Expand Up @@ -175,6 +175,7 @@ type ConfigType = {

configpath: string;
mnemonicpath: string;
mnemonicpathEvm: string;

dbpath: string;
postgres?: PostgresConfig;
Expand Down Expand Up @@ -225,6 +226,7 @@ class Config {

public static defaultConfigPath = 'boltz.conf';
public static defaultMnemonicPath = 'seed.dat';
public static defaultMnemonicPathEvm = 'seed.dat';
public static defaultLogPath = 'boltz.log';
public static defaultDbPath = 'boltz.db';

Expand All @@ -239,13 +241,13 @@ class Config {
constructor() {
this.dataDir = getServiceDataDir('boltz');

const { dbpath, logpath, configpath, mnemonicpath } = this.getDataDirPaths(
this.dataDir,
);
const { dbpath, logpath, configpath, mnemonicpath, mnemonicpathEvm } =
this.getDataDirPaths(this.dataDir);

this.config = {
configpath,
mnemonicpath,
mnemonicpathEvm,
dbpath,
logpath,

Expand Down Expand Up @@ -505,6 +507,7 @@ class Config {
return {
configpath: path.join(dataDir, Config.defaultConfigPath),
mnemonicpath: path.join(dataDir, Config.defaultMnemonicPath),
mnemonicpathEvm: path.join(dataDir, Config.defaultMnemonicPathEvm),
dbpath: path.join(dataDir, Config.defaultDbPath),
logpath: path.join(dataDir, Config.defaultLogPath),
};
Expand Down
16 changes: 9 additions & 7 deletions lib/cli/ethereum/EthereumUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const getBoltzFilePath = (file: string): string =>
path.join(process.env.HOME!, '.boltz', file);

const getBoltzWallet = (): HDNodeWallet => {
const filePath = getBoltzFilePath('seed.dat');
for (const file in ['seed.dat', 'seedEvm.dat']) {
const filePath = getBoltzFilePath(file);

if (existsSync(filePath)) {
return Wallet.fromPhrase(
readFileSync(filePath, {
encoding: 'utf-8',
}).trim(),
);
if (existsSync(filePath)) {
return Wallet.fromPhrase(
readFileSync(filePath, {
encoding: 'utf-8',
}).trim(),
);
}
}

throw 'no Boltz wallet found';
Expand Down
17 changes: 9 additions & 8 deletions lib/wallet/WalletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class WalletManager {
public wallets = new Map<string, Wallet>();

private readonly mnemonic: string;
private readonly mnemonicEvm: string;

private readonly slip77: Slip77Interface;
private readonly masterNode: BIP32Interface;

Expand All @@ -67,16 +69,13 @@ class WalletManager {
constructor(
private logger: Logger,
mnemonicPath: string,
mnemonicPathEvm: string,
private currencies: Currency[],
public ethereumManagers: EthereumManager[],
) {
if (!fs.existsSync(mnemonicPath)) {
this.logger.info('Generated new mnemonic');

fs.writeFileSync(mnemonicPath, generateMnemonic());
}

this.mnemonic = this.loadMnemonic(mnemonicPath);
this.mnemonicEvm = this.loadMnemonic(mnemonicPathEvm);

this.masterNode = bip32.fromSeed(mnemonicToSeedSync(this.mnemonic));
this.slip77 = slip77.fromSeed(this.mnemonic);
}
Expand Down Expand Up @@ -168,7 +167,7 @@ class WalletManager {
}

for (const manager of this.ethereumManagers) {
const ethereumWallets = await manager.init(this.mnemonic);
const ethereumWallets = await manager.init(this.mnemonicEvm);

for (const [symbol, ethereumWallet] of ethereumWallets) {
this.wallets.set(symbol, ethereumWallet);
Expand All @@ -178,7 +177,9 @@ class WalletManager {

private loadMnemonic = (filename: string) => {
if (!fs.existsSync(filename)) {
throw Errors.NOT_INITIALIZED();
this.logger.info(`Generated new mnemonic: ${filename}`);

fs.writeFileSync(filename, generateMnemonic());
}

const mnemonic = fs.readFileSync(filename, 'utf-8').trim();
Expand Down
2 changes: 2 additions & 0 deletions test/integration/swap/UtxoNursery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('UtxoNursery', () => {
const walletManager = new WalletManager(
Logger.disabledLogger,
mnemonicPath,
mnemonicPath,
currencies,
[],
);
Expand Down Expand Up @@ -115,6 +116,7 @@ describe('UtxoNursery', () => {
},
lockupTracker,
{} as any,
{} as any,
);

beforeAll(async () => {
Expand Down
23 changes: 21 additions & 2 deletions test/unit/wallet/WalletManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const mockedLndClient = <jest.Mock<LndClient>>(<any>LndClient);
// TODO: test ethereum wallet initialization
describe('WalletManager', () => {
const mnemonicPath = 'seed.dat';
const mnemonicPathEvm = 'seedEvm.dat';

const database = new Database(Logger.disabledLogger, ':memory:');

Expand Down Expand Up @@ -100,6 +101,7 @@ describe('WalletManager', () => {
};

deleteFile(mnemonicPath);
deleteFile(mnemonicPathEvm);
};

beforeAll(async () => {
Expand All @@ -109,8 +111,15 @@ describe('WalletManager', () => {
});

test('should initialize with a new menmonic and write it to the disk', () => {
new WalletManager(Logger.disabledLogger, mnemonicPath, currencies, []);
new WalletManager(
Logger.disabledLogger,
mnemonicPath,
mnemonicPathEvm,
currencies,
[],
);
expect(fs.existsSync(mnemonicPath)).toBeTruthy();
expect(fs.existsSync(mnemonicPathEvm)).toBeTruthy();
});

test('should initialize a Bitcoin Core wallet when LND is not configured', async () => {
Expand All @@ -124,6 +133,7 @@ describe('WalletManager', () => {
const noLndWalletManager = new WalletManager(
Logger.disabledLogger,
mnemonicPath,
mnemonicPathEvm,
currenciesNoLnd,
[],
);
Expand All @@ -149,6 +159,7 @@ describe('WalletManager', () => {
const noLndWalletManager = new WalletManager(
Logger.disabledLogger,
mnemonicPath,
mnemonicPathEvm,
currenciesNoLnd,
[],
);
Expand All @@ -161,6 +172,7 @@ describe('WalletManager', () => {
walletManager = new WalletManager(
Logger.disabledLogger,
mnemonicPath,
mnemonicPathEvm,
currencies,
[],
);
Expand Down Expand Up @@ -208,7 +220,14 @@ describe('WalletManager', () => {
fs.writeFileSync(mnemonicPath, invalidMnemonic);

expect(
() => new WalletManager(Logger.disabledLogger, mnemonicPath, [], []),
() =>
new WalletManager(
Logger.disabledLogger,
mnemonicPath,
mnemonicPathEvm,
[],
[],
),
).toThrow(WalletErrors.INVALID_MNEMONIC(invalidMnemonic).message);
});

Expand Down

0 comments on commit 125d4d6

Please sign in to comment.