eth-onekey-bridge-keyring
Version:
A MetaMask compatible keyring, for onekey hardware wallets
238 lines • 9.32 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 constants_1 = require("./constants");
class OneKeyWebBridge {
constructor() {
this.isSDKInitialized = false;
this.sdk = undefined;
this.eventListeners = new Map();
console.log('OneKeyWebBridge constructor');
}
on(_event, callback) {
this.eventListeners.set(_event, callback);
}
off(_event) {
this.eventListeners.delete(_event);
}
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,
];
if (code && typeof code === 'number' && errorCodes.includes(code)) {
(_a = this.eventListeners.get(constants_1.ONEKEY_HARDWARE_UI_EVENT)) === null || _a === void 0 ? void 0 : _a(payload.payload);
}
}
async updateTransportMethod(transportType) {
if (!this.sdk) {
return;
}
await this.sdk.switchTransport(transportType);
}
async init() {
var _a;
if (this.isSDKInitialized) {
return;
}
const sdkLib = await Promise.resolve().then(() => __importStar(require('@onekeyfe/hd-web-sdk')));
const { HardwareWebSdk, HardwareSDKLowLevel } = sdkLib.default;
const settings = {
debug: true,
fetchConfig: false,
connectSrc: 'https://jssdk.onekey.so/1.0.26-alpha.3/',
env: 'webusb',
};
try {
await HardwareWebSdk.init(settings, HardwareSDKLowLevel);
this.isSDKInitialized = true;
this.sdk = HardwareWebSdk;
// eslint-disable-next-line id-length
(_a = this.sdk) === null || _a === void 0 ? void 0 : _a.on('UI_EVENT', (e) => {
var _a, _b;
const originEvent = e;
console.log('===>>>> onekey event UI_EVENT: ', originEvent);
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 (error) {
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: 800 },
};
}
return await this.sdk.evmGetPublicKey('', '', params).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,
},
};
}
this.handleBlockErrorEvent(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 batchGetPublicKey(params) {
if (!this.sdk) {
return {
success: false,
payload: { error: 'SDK not initialized', code: 800 },
};
}
return await this.sdk.evmGetPublicKey('', '', params).then((result) => {
var _a, _b;
if (result === null || result === void 0 ? void 0 : result.success) {
if (Array.isArray(result.payload)) {
return {
success: true,
payload: result.payload.map((item) => ({ pub: item.pub })),
};
}
return {
success: false,
payload: { error: 'No public key found', code: 800 },
};
}
this.handleBlockErrorEvent(result);
return {
success: false,
payload: {
error: (_a = result === null || result === void 0 ? void 0 : result.payload.error) !== null && _a !== void 0 ? _a : '',
code: (_b = result === null || result === void 0 ? void 0 : result.payload.code) !== null && _b !== void 0 ? _b : undefined,
},
};
});
}
async getPassphraseState() {
if (!this.sdk) {
return {
success: false,
payload: { error: 'SDK not initialized', code: 800 },
};
}
return await this.sdk.getPassphraseState('').then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
this.handleBlockErrorEvent(result);
}
return result;
});
}
async ethereumSignTransaction(params) {
if (!this.sdk) {
return {
success: false,
payload: { error: 'SDK not initialized', code: 800 },
};
}
return await this.sdk.evmSignTransaction('', '', params).then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
this.handleBlockErrorEvent(result);
}
return result;
});
}
async ethereumSignMessage(params) {
if (!this.sdk) {
return {
success: false,
payload: { error: 'SDK not initialized', code: 800 },
};
}
return await this.sdk.evmSignMessage('', '', params).then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
this.handleBlockErrorEvent(result);
}
return result;
});
}
async ethereumSignTypedData(params) {
if (!this.sdk) {
return {
success: false,
payload: { error: 'SDK not initialized', code: 800 },
};
}
return await this.sdk.evmSignTypedData('', '', params).then((result) => {
if (!(result === null || result === void 0 ? void 0 : result.success)) {
this.handleBlockErrorEvent(result);
}
return result;
});
}
}
exports.OneKeyWebBridge = OneKeyWebBridge;
//# sourceMappingURL=onekey-web-bridge.js.map