UNPKG

@aeternity/aepp-sdk

Version:

SDK for the æternity blockchain

89 lines (81 loc) 2.64 kB
import { Buffer as _Buffer } from "buffer"; import AccountBase from './Base.js'; import { ArgumentError, InternalError, NotImplementedError } from '../utils/errors.js'; export const snapId = 'npm:@aeternity-snap/plugin'; export async function invokeSnap(provider, method, params, key) { const response = await provider.request({ method: 'wallet_invokeSnap', params: { snapId, request: { method, params } } }); if (response == null) throw new InternalError('Empty MetaMask response'); if (!(key in response)) { throw new InternalError(`Key ${key} missed in response ${JSON.stringify(response)}`); } return response[key]; } /** * Account connected to Aeternity Snap for MetaMask * https://www.npmjs.com/package/\@aeternity-snap/plugin * @category account */ export default class AccountMetamask extends AccountBase { /** * @deprecated this class is not intended to provide raw access to the provider */ /** * @param address - Address of account */ constructor(provider, index, address) { super(); this.provider = provider; this.index = index; this.address = address; } /** * @deprecated Use `unsafeSign` method instead */ // eslint-disable-next-line class-methods-use-this async sign() { return this.unsafeSign(); } // eslint-disable-next-line class-methods-use-this async unsafeSign() { throw new NotImplementedError('RAW signing using MetaMask'); } // eslint-disable-next-line class-methods-use-this async signTypedData() { throw new NotImplementedError('Typed data signing using MetaMask'); } // eslint-disable-next-line class-methods-use-this async signDelegation() { throw new NotImplementedError('signing delegation using MetaMask'); } // eslint-disable-next-line class-methods-use-this async signTransaction(tx, { innerTx, networkId } = {}) { if (innerTx != null) throw new NotImplementedError('innerTx option in AccountMetamask'); if (networkId == null) throw new ArgumentError('networkId', 'provided', networkId); return invokeSnap(this.provider, 'signTransaction', { derivationPath: [`${this.index}'`, "0'", "0'"], tx, networkId }, 'signedTx'); } // eslint-disable-next-line class-methods-use-this async signMessage(message) { const signature = await invokeSnap(this.provider, 'signMessage', { derivationPath: [`${this.index}'`, "0'", "0'"], message: _Buffer.from(message).toString('base64') }, 'signature'); return _Buffer.from(signature, 'base64'); } } //# sourceMappingURL=Metamask.js.map