eth-onekey-bridge-keyring
Version:
A MetaMask compatible keyring, for OneKey hardware wallets
217 lines • 9.37 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _OneKeyWebBridge_instances, _OneKeyWebBridge_onUIEvent, _OneKeyWebBridge_handleBlockErrorEvent;
function $importDefault(module) {
if (module === null || module === void 0 ? void 0 : module.__esModule) {
return module.default;
}
return module;
}
import { UI_REQUEST, UI_RESPONSE } from "@onekeyfe/hd-core";
import { HardwareErrorCode } from "@onekeyfe/hd-shared";
import $hardwareWebSdk from "@onekeyfe/hd-web-sdk";
const hardwareWebSdk = $importDefault($hardwareWebSdk);
export class OneKeyWebBridge {
constructor() {
_OneKeyWebBridge_instances.add(this);
this.isSDKInitialized = false;
this.sdk = undefined;
_OneKeyWebBridge_onUIEvent.set(this, void 0);
}
async updateTransportMethod(transportType) {
if (!this.sdk) {
return;
}
await this.sdk.switchTransport(transportType);
}
setUiEventCallback(callback) {
__classPrivateFieldSet(this, _OneKeyWebBridge_onUIEvent, callback, "f");
}
async init() {
var _a;
if (this.isSDKInitialized) {
return;
}
const settings = {
debug: false,
fetchConfig: false,
connectSrc: 'https://jssdk.onekey.so/1.1.0/',
env: 'webusb',
};
try {
await hardwareWebSdk.HardwareWebSdk.init(settings, hardwareWebSdk.HardwareSDKLowLevel);
this.isSDKInitialized = true;
this.sdk = hardwareWebSdk.HardwareWebSdk;
(_a = this.sdk) === null || _a === void 0 ? void 0 : _a.on('UI_EVENT', (originEvent) => {
var _a, _b;
if (originEvent.type === UI_REQUEST.REQUEST_PIN) {
(_a = this.sdk) === null || _a === void 0 ? void 0 : _a.uiResponse({
type: UI_RESPONSE.RECEIVE_PIN,
payload: '@@ONEKEY_INPUT_PIN_IN_DEVICE',
});
}
if (originEvent.type === UI_REQUEST.REQUEST_PASSPHRASE) {
(_b = this.sdk) === null || _b === void 0 ? void 0 : _b.uiResponse({
type: UI_RESPONSE.RECEIVE_PASSPHRASE,
payload: {
value: '',
passphraseOnDevice: true,
save: false,
},
});
}
});
}
catch (_b) {
this.isSDKInitialized = false;
}
}
async destroy() {
this.isSDKInitialized = false;
this.sdk = undefined;
}
async dispose() {
var _a;
(_a = this.sdk) === null || _a === void 0 ? void 0 : _a.dispose();
return Promise.resolve();
}
getModel() {
return this.model;
}
async getPublicKey(params) {
if (!this.sdk) {
return {
success: false,
payload: {
error: 'SDK not initialized',
code: HardwareErrorCode.NotInitialized,
},
};
}
return await this.sdk
.evmGetPublicKey('', '', Object.assign(Object.assign({}, params), { skipPassphraseCheck: true }))
.then((result) => {
var _a, _b, _c;
if (result === null || result === void 0 ? void 0 : result.success) {
return {
success: true,
payload: {
publicKey: result.payload.pub,
chainCode: result.payload.node.chain_code,
},
};
}
__classPrivateFieldGet(this, _OneKeyWebBridge_instances, "m", _OneKeyWebBridge_handleBlockErrorEvent).call(this, result);
return {
success: false,
payload: {
error: (_a = result === null || result === void 0 ? void 0 : result.payload.error) !== null && _a !== void 0 ? _a : '',
code: typeof ((_b = result === null || result === void 0 ? void 0 : result.payload) === null || _b === void 0 ? void 0 : _b.code) === 'number'
? (_c = result === null || result === void 0 ? void 0 : result.payload) === null || _c === void 0 ? void 0 : _c.code
: undefined,
},
};
});
}
async getPassphraseState() {
if (!this.sdk) {
return {
success: false,
payload: {
error: 'SDK not initialized',
code: HardwareErrorCode.NotInitialized,
},
};
}
return await this.sdk.getPassphraseState('').then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
__classPrivateFieldGet(this, _OneKeyWebBridge_instances, "m", _OneKeyWebBridge_handleBlockErrorEvent).call(this, result);
}
return result;
});
}
async ethereumSignTransaction(params) {
if (!this.sdk) {
return {
success: false,
payload: {
error: 'SDK not initialized',
code: HardwareErrorCode.NotInitialized,
},
};
}
return await this.sdk
.evmSignTransaction('', '', Object.assign(Object.assign({}, params), { skipPassphraseCheck: true }))
.then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
__classPrivateFieldGet(this, _OneKeyWebBridge_instances, "m", _OneKeyWebBridge_handleBlockErrorEvent).call(this, result);
}
return result;
});
}
async ethereumSignMessage(params) {
if (!this.sdk) {
return {
success: false,
payload: {
error: 'SDK not initialized',
code: HardwareErrorCode.NotInitialized,
},
};
}
return await this.sdk
.evmSignMessage('', '', Object.assign(Object.assign({}, params), { skipPassphraseCheck: true }))
.then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
__classPrivateFieldGet(this, _OneKeyWebBridge_instances, "m", _OneKeyWebBridge_handleBlockErrorEvent).call(this, result);
}
return result;
});
}
async ethereumSignTypedData(params) {
if (!this.sdk) {
return {
success: false,
payload: {
error: 'SDK not initialized',
code: HardwareErrorCode.NotInitialized,
},
};
}
return await this.sdk
.evmSignTypedData('', '', Object.assign(Object.assign({}, params), { skipPassphraseCheck: true }))
.then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
__classPrivateFieldGet(this, _OneKeyWebBridge_instances, "m", _OneKeyWebBridge_handleBlockErrorEvent).call(this, result);
}
return result;
});
}
}
_OneKeyWebBridge_onUIEvent = new WeakMap(), _OneKeyWebBridge_instances = new WeakSet(), _OneKeyWebBridge_handleBlockErrorEvent = function _OneKeyWebBridge_handleBlockErrorEvent(payload) {
var _a;
const { code } = payload.payload;
const errorCodes = [
HardwareErrorCode.WebDeviceNotFoundOrNeedsPermission,
HardwareErrorCode.BridgeNotInstalled,
HardwareErrorCode.NewFirmwareForceUpdate,
HardwareErrorCode.NotAllowInBootloaderMode,
HardwareErrorCode.CallMethodNeedUpgradeFirmware,
HardwareErrorCode.DeviceCheckPassphraseStateError,
HardwareErrorCode.DeviceCheckUnlockTypeError,
HardwareErrorCode.SelectDevice,
];
if (code && typeof code === 'number' && errorCodes.includes(code)) {
(_a = __classPrivateFieldGet(this, _OneKeyWebBridge_onUIEvent, "f")) === null || _a === void 0 ? void 0 : _a.call(this, payload.payload);
}
};
//# sourceMappingURL=onekey-web-bridge.mjs.map