UNPKG

eth-onekey-bridge-keyring

Version:
217 lines 9.64 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _OneKeyWebBridge_instances, _OneKeyWebBridge_onUIEvent, _OneKeyWebBridge_handleBlockErrorEvent; Object.defineProperty(exports, "__esModule", { value: true }); exports.OneKeyWebBridge = void 0; const hd_core_1 = require("@onekeyfe/hd-core"); const hd_shared_1 = require("@onekeyfe/hd-shared"); const hd_web_sdk_1 = __importDefault(require("@onekeyfe/hd-web-sdk")); 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 hd_web_sdk_1.default.HardwareWebSdk.init(settings, hd_web_sdk_1.default.HardwareSDKLowLevel); this.isSDKInitialized = true; this.sdk = hd_web_sdk_1.default.HardwareWebSdk; (_a = this.sdk) === null || _a === void 0 ? void 0 : _a.on('UI_EVENT', (originEvent) => { var _a, _b; if (originEvent.type === hd_core_1.UI_REQUEST.REQUEST_PIN) { (_a = this.sdk) === null || _a === void 0 ? void 0 : _a.uiResponse({ type: hd_core_1.UI_RESPONSE.RECEIVE_PIN, payload: '@@ONEKEY_INPUT_PIN_IN_DEVICE', }); } if (originEvent.type === hd_core_1.UI_REQUEST.REQUEST_PASSPHRASE) { (_b = this.sdk) === null || _b === void 0 ? void 0 : _b.uiResponse({ type: hd_core_1.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: hd_shared_1.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: hd_shared_1.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: hd_shared_1.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: hd_shared_1.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: hd_shared_1.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; }); } } exports.OneKeyWebBridge = OneKeyWebBridge; _OneKeyWebBridge_onUIEvent = new WeakMap(), _OneKeyWebBridge_instances = new WeakSet(), _OneKeyWebBridge_handleBlockErrorEvent = function _OneKeyWebBridge_handleBlockErrorEvent(payload) { var _a; const { code } = payload.payload; const errorCodes = [ hd_shared_1.HardwareErrorCode.WebDeviceNotFoundOrNeedsPermission, hd_shared_1.HardwareErrorCode.BridgeNotInstalled, hd_shared_1.HardwareErrorCode.NewFirmwareForceUpdate, hd_shared_1.HardwareErrorCode.NotAllowInBootloaderMode, hd_shared_1.HardwareErrorCode.CallMethodNeedUpgradeFirmware, hd_shared_1.HardwareErrorCode.DeviceCheckPassphraseStateError, hd_shared_1.HardwareErrorCode.DeviceCheckUnlockTypeError, hd_shared_1.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.cjs.map