@bsv/sdk
Version:
BSV Blockchain Software Development Kit
137 lines • 4.77 kB
JavaScript
import Random from '../../primitives/Random.js';
import * as Utils from '../../primitives/utils.js';
import { WalletError } from '../WalletError.js';
/**
* Facilitates wallet operations over cross-document messaging.
*/
export default class ReactNativeWebView {
domain;
constructor(domain = '*') {
if (typeof window !== 'object') {
throw new Error('The XDM substrate requires a global window object.');
}
if (!window.hasOwnProperty("ReactNativeWebView")) {
throw new Error('The window object does not have a ReactNativeWebView property.');
}
if (typeof window.ReactNativeWebView.postMessage !== 'function') {
throw new Error('The window.ReactNativeWebView property does not seem to support postMessage calls.');
}
this.domain = domain;
}
async invoke(call, args) {
return await new Promise((resolve, reject) => {
const id = Utils.toBase64(Random(12));
const listener = (e) => {
const data = JSON.parse(e.data);
if (data.type !== 'CWI' ||
data.id !== id ||
data.isInvocation === true) {
return;
}
if (typeof window.removeEventListener === 'function') {
window.removeEventListener('message', listener);
}
if (data.status === 'error') {
const err = new WalletError(data.description, data.code);
reject(err);
}
else {
resolve(data.result);
}
};
window.addEventListener('message', listener);
window.ReactNativeWebView.postMessage(JSON.stringify({
type: 'CWI',
isInvocation: true,
id,
call,
args
}));
});
}
async createAction(args) {
return await this.invoke('createAction', args);
}
async signAction(args) {
return await this.invoke('signAction', args);
}
async abortAction(args) {
return await this.invoke('abortAction', args);
}
async listActions(args) {
return await this.invoke('listActions', args);
}
async internalizeAction(args) {
return await this.invoke('internalizeAction', args);
}
async listOutputs(args) {
return await this.invoke('listOutputs', args);
}
async relinquishOutput(args) {
return await this.invoke('relinquishOutput', args);
}
async getPublicKey(args) {
return await this.invoke('getPublicKey', args);
}
async revealCounterpartyKeyLinkage(args) {
return await this.invoke('revealCounterpartyKeyLinkage', args);
}
async revealSpecificKeyLinkage(args) {
return await this.invoke('revealSpecificKeyLinkage', args);
}
async encrypt(args) {
return await this.invoke('encrypt', args);
}
async decrypt(args) {
return await this.invoke('decrypt', args);
}
async createHmac(args) {
return await this.invoke('createHmac', args);
}
async verifyHmac(args) {
return await this.invoke('verifyHmac', args);
}
async createSignature(args) {
return await this.invoke('createSignature', args);
}
async verifySignature(args) {
return await this.invoke('verifySignature', args);
}
async acquireCertificate(args) {
return await this.invoke('acquireCertificate', args);
}
async listCertificates(args) {
return await this.invoke('listCertificates', args);
}
async proveCertificate(args) {
return await this.invoke('proveCertificate', args);
}
async relinquishCertificate(args) {
return await this.invoke('relinquishCertificate', args);
}
async discoverByIdentityKey(args) {
return await this.invoke('discoverByIdentityKey', args);
}
async discoverByAttributes(args) {
return await this.invoke('discoverByAttributes', args);
}
async isAuthenticated(args) {
return await this.invoke('isAuthenticated', args);
}
async waitForAuthentication(args) {
return await this.invoke('waitForAuthentication', args);
}
async getHeight(args) {
return await this.invoke('getHeight', args);
}
async getHeaderForHeight(args) {
return await this.invoke('getHeaderForHeight', args);
}
async getNetwork(args) {
return await this.invoke('getNetwork', args);
}
async getVersion(args) {
return await this.invoke('getVersion', args);
}
}
//# sourceMappingURL=ReactNativeWebView.js.map