@tatumio/tezos-wallet-provider
Version:
Tezos provider with local wallet operations
74 lines • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TezosWalletProvider = void 0;
const signer_1 = require("@taquito/signer");
const taquito_1 = require("@taquito/taquito");
const tatum_1 = require("@tatumio/tatum");
const bip39_1 = require("bip39");
const sotez_1 = require("sotez");
const consts_1 = require("./consts");
const tatum_provider_1 = require("./tatum.provider");
class TezosWalletProvider extends tatum_1.TatumSdkWalletProvider {
constructor(tatumSdkContainer) {
super(tatumSdkContainer);
this.supportedNetworks = [tatum_1.Network.TEZOS, tatum_1.Network.TEZOS_TESTNET];
this.rpc = this.tatumSdkContainer.getRpc();
this.chain = 'main';
}
/**
* Generates a mnemonic seed phrase.
* @returns {string} A mnemonic seed phrase.
*/
generateMnemonic() {
return (0, bip39_1.generateMnemonic)(256);
}
/**
* Generates a private key based on a mnemonic and a derivation path.
* If no derivation path is provided, default is used.
* @param {string} mnemonic - The mnemonic seed phrase.
* @param {string} [path] - The derivation path.
* @returns {string} A private key in a string format.
*/
async generatePrivateKeyFromMnemonic(mnemonic, path) {
path = path || consts_1.TEZOS_DERIVATION_PATH;
const { sk: privateKey } = await sotez_1.cryptoUtils.generateKeys(mnemonic, undefined, path);
return privateKey;
}
/**
* Generates an address from a given private key.
* @param {string} privateKey - The private key in string format.
* @returns {string} An address in string format.
*/
async generateAddressFromPrivateKey(privateKey) {
const { pkh: address } = await sotez_1.cryptoUtils.extractKeys(privateKey);
return address;
}
/**
* Gets a Tezos wallet, which includes an address, private key, and a mnemonic.
* @returns {TezosWallet} An object containing address, private key, and mnemonic.
*/
async getWallet() {
const mnemonic = this.generateMnemonic();
const privateKey = await this.generatePrivateKeyFromMnemonic(mnemonic);
const address = await this.generateAddressFromPrivateKey(privateKey);
return { address, privateKey, mnemonic };
}
/**
* Signs and broadcasts a Tezos transaction payload.
* @param {TezosTxPayload} payload - The Tezos transaction payload, which includes private key and transaction details.
* @returns {Promise<string>} A promise that resolves to the transaction hash.
*/
async signAndBroadcast(payload) {
const tezos = new taquito_1.TezosToolkit(new tatum_provider_1.TatumProvider(this.rpc, this.chain));
tezos.setProvider({
signer: await signer_1.InMemorySigner.fromSecretKey(payload.privateKey),
});
const op = await tezos.contract.transfer({
to: payload.to,
amount: payload.amount,
});
return op.hash;
}
}
exports.TezosWalletProvider = TezosWalletProvider;
//# sourceMappingURL=extension.js.map