@hiveio/wax-signers-beekeeper
Version:
Wax signer library extending transaction signing possibilities by a 3rd party Web-only extension - Beekeeper
78 lines (77 loc) • 3.17 kB
JavaScript
import { AEncryptionProvider } from "@hiveio/wax";
// We do not extend from WaxError to avoid runtime dependencies, such as: /vite or /web - without it we can import only types
export class WaxBeekeeperProviderError extends Error {
}
/**
* Wax transaction signature provider using the Beekeeper.
*
* @example
* ```
* const provider = BeekeeperProvider.for(myWallet, "myaccount", "active", chain);
*
* // Create a transaction using the Wax Hive chain instance
* const tx = await chain.createTransaction();
*
* // Perform some operations, e.g. pushing operations...
*
* // Sign the transaction
* await provider.signTransaction(tx);
*
* // broadcast
* await chain.broadcast(tx);
* ```
*/
export class BeekeeperProvider extends AEncryptionProvider {
base;
wallet;
publicKey;
constructor(base, wallet, publicKey) {
super();
this.base = base;
this.wallet = wallet;
this.publicKey = publicKey;
}
static for(chainOrBase, wallet, publicKeyOrAccount, role) {
if (role === undefined)
return new BeekeeperProvider(chainOrBase, wallet, publicKeyOrAccount);
return chainOrBase.api.database_api.find_accounts({ accounts: [publicKeyOrAccount], delayed_votes_active: false }).then(({ accounts: [account] }) => {
if (account === undefined)
return Promise.reject(new WaxBeekeeperProviderError(`Account ${publicKeyOrAccount} not found`));
const actualRole = role === "memo" ? "memo_key" : role;
return account[actualRole] ? new BeekeeperProvider(chainOrBase, wallet, role === "memo" ? account.memo_key : account[role].key_auths[0][0]) : Promise.reject(new WaxBeekeeperProviderError(`Account ${publicKeyOrAccount} does not have ${role} key`));
});
}
/**
* Encrypts data using the Beekeeper.
*
* @param content The string to encrypt.
* @param recipient The public key of the recipient to encrypt the data for. The recipient should be a valid public key, starting with "STM".
* @returns A string containing the encrypted data. The string starts with the `#` prefix.
* @throws on any error from the Beekeeper invocation.
*/
async encryptData(content, recipient) {
return this.base.encrypt(this.wallet, content, this.publicKey, recipient);
}
/**
* Decrypts data using the Beekeeper.
*
* @param content The string to decrypt. The string should start with the `#` prefix.
* @returns The decrypted data as a string.
* @throws on any error from the Beekeeper invocation.
*/
async decryptData(content) {
return this.base.decrypt(this.wallet, content);
}
/**
* Generates signatures for given transaction using the Beekeeper.
*
* @param transaction The transaction to sign. The transaction should be created using the Wax Hive chain instance.
* @throws on any error from the Beekeeper invocation.
*/
async generateSignatures(transaction) {
const signature = this.wallet.signDigest(this.publicKey, transaction.sigDigest);
return [signature];
}
}
;
export default BeekeeperProvider;