/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see .
*/
import {
QRLExecutionAPI,
Bytes,
Transaction,
KeyStore,
QRL_DATA_FORMAT,
} from '@theqrl/web3-types';
import { format } from '@theqrl/web3-utils';
import { Web3Context } from '@theqrl/web3-core';
import { prepareTransactionForSigning } from '@theqrl/web3-qrl';
import {
create,
decrypt,
encrypt,
hashMessage,
recoverTransaction,
signTransaction,
sign,
Wallet,
seedToAccount,
} from '@theqrl/web3-qrl-accounts';
/**
* Initialize the accounts module for the given context.
*
* To avoid multiple package dependencies for `@theqrl/web3-qrl-accounts` we are creating
* this function in `web3` package. In future the actual `@theqrl/web3-qrl-accounts` package
* should be converted to context aware.
*/
export const initAccountsForContext = (context: Web3Context) => {
const signTransactionWithContext = async (transaction: Transaction, seed: Bytes) => {
const tx = await prepareTransactionForSigning(transaction, context);
const seedBytes = format({ format: 'bytes' }, seed, QRL_DATA_FORMAT);
return signTransaction(tx, seedBytes);
};
const wrapAccount = unknown>(
account: ReturnType,
signTx: S,
) => {
const wrapper = { ...account, signTransaction: signTx };
Object.defineProperty(wrapper, 'seed', {
value: account.seed,
enumerable: false,
writable: false,
configurable: false,
});
Object.defineProperty(wrapper, 'toJSON', {
value: () => ({ address: wrapper.address, seed: '' }),
enumerable: false,
});
return wrapper;
};
const seedToAccountWithContext = (seed: Uint8Array | string) => {
const account = seedToAccount(seed);
return wrapAccount(account, async (transaction: Transaction) =>
signTransactionWithContext(transaction, account.seed),
);
};
const decryptWithContext = async (
keystore: KeyStore | string,
password: string,
options?: Record,
) => {
const account = await decrypt(keystore, password, (options?.nonStrict as boolean) ?? true);
return wrapAccount(account, async (transaction: Transaction) =>
signTransactionWithContext(transaction, account.seed),
);
};
const createWithContext = () => {
const account = create();
return wrapAccount(account, async (transaction: Transaction) =>
signTransactionWithContext(transaction, account.seed),
);
};
const wallet = new Wallet({
create: createWithContext,
seedToAccount: seedToAccountWithContext,
decrypt: decryptWithContext,
});
return {
signTransaction: signTransactionWithContext,
create: createWithContext,
seedToAccount: seedToAccountWithContext,
decrypt: decryptWithContext,
recoverTransaction,
hashMessage,
sign,
encrypt,
wallet,
};
};