Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async getPublicKey(accountIndex: number): Promise {
if (this.appService.isDesktop()) {
const publicKeyHex: string = this.appService.sendIpcMessageSync('ledger-get-public-key', accountIndex);
if (publicKeyHex.startsWith('Error: ')) {
throw new Error(publicKeyHex);
}
return publicKeyHex;
} else {
const transport = await TransportWebUSB.create();
// todo: move this to a shared fn
const publicKey: Buffer = await transport.exchange(Buffer.from('800400' + this.byteToHex(accountIndex) + '00', 'hex'));
return publicKey.toString('hex').substr(0, 64);
}
}
async connect(timeout = INTERACTION_TIMEOUT) {
// assume well connection if connected once
if (this.cosmosApp) return
const transport = await TransportWebUSB.create(timeout)
const cosmosLedgerApp = new CosmosApp(transport)
this.cosmosApp = cosmosLedgerApp
await this.isSendingData()
await this.isReady()
}
async getCosmosAppVersion() {
const isWebUsbSupported = async () => {
const isSupported = await webUsbTransport.isSupported();
return (
isSupported && platform.os.family !== 'Windows' && platform.name !== 'Opera' // take it out later once the windows issue is fixed
);
};
useEffect(() => {
// TODO: Temporary workaround for Ledger USB issues on Windows
if (!navigator.platform.includes('Win')) {
TransportWebUSB.isSupported()
.then(setWebUSBSupported)
.catch(() => setWebUSBSupported(false));
}
}, []);
open: (id: string): ?Promise<*> => {
if (id.startsWith("webble")) {
const existingDevice = webbleDevices[id];
return existingDevice
? TransportWebBLE.open(existingDevice)
: TransportWebBLE.create();
}
return null;
},
disconnect: id =>
id.startsWith("webble")
? Promise.resolve() // nothing to do
: null,
discovery: Observable.create(TransportWebUSB.listen).pipe(
map(bleDevice => {
const id = "webble|" + bleDevice.id;
webbleDevices[id] = bleDevice;
return {
type: "add",
id,
name: bleDevice.name
};
})
)
});
// provide a basic mecanism to stop polling when you leave the tab
// & immediately poll when you come back.
const addExtraPollingHooks = (schedulePoll, cancelPoll) => {
function onWindowBlur() {
open: (id: string): ?Promise<*> => {
if (id.startsWith("webusb")) {
const existingDevice = webusbDevices[id];
return existingDevice
? TransportWebUSB.open(existingDevice)
: TransportWebUSB.create();
}
return null;
},
disconnect: id =>
id.startsWith("webusb")
? Promise.resolve() // nothing to do
: null,
discovery: Observable.create(TransportWebUSB.listen).pipe(
map(usbDevice => {
const id = "webusb|" + usbDevice.vendorId + "_" + usbDevice.productId;
webusbDevices[id] = usbDevice;
return {
type: "add",
id,
name: usbDevice.productName
};
})
)
});
const webbleDevices = {};
registerTransportModule({
id: "webble",
export async function createTransport() {
var transport = null;
transport = await TransportWebUSB.create();
console.log('USING WEBUSB');
var qrl = await new Qrl(transport);
return qrl
}
const CardanoLedgerCryptoProvider = async (ADALITE_CONFIG, walletState) => {
let transport
try {
transport = await LedgerTransportU2F.create()
} catch (u2fError) {
try {
transport = await LedgerTransportWebusb.create()
} catch (webUsbError) {
debugLog(webUsbError)
throw u2fError
}
}
transport.setExchangeTimeout(ADALITE_CONFIG.ADALITE_LOGOUT_AFTER * 1000)
const ledger = new Ledger(transport)
const state = Object.assign(walletState, {
derivationScheme: derivationSchemes.v2,
rootHdPassphrase: null,
derivedAddresses: {},
})
const isHwWallet = () => true
const getHwWalletName = () => 'Ledger'
execInQueue(async () => {
const { derivationMode, currency, index: account } = this.props;
try {
const p = transportGlobalP || TransportWebUSB.create();
transportGlobalP = p;
const transport = await p;
const { address, path } = await getAddress(transport, {
currency,
path: runDerivationScheme(
getDerivationScheme({ currency, derivationMode }),
currency,
{ account }
),
derivationMode,
verify
});
this.setState({ address, path });
} catch (error) {
this.setState({ error });
}
public async sign(accountIndex: number, message: string): Promise {
if (this.appService.isDesktop()) {
const signature: string = this.appService.sendIpcMessageSync('ledger-sign', {accountIndex, message});
if (signature.startsWith('Error: ')) {
throw new Error(signature);
}
return signature;
} else {
const transport = await TransportWebUSB.create();
let offset = 0;
while (offset !== message.length) {
let chunk: string;
if (message.length - offset > 510) {
chunk = message.substr(offset, 510);
} else {
chunk = message.substr(offset);
}
let apdu = '8002';
const final = (offset + chunk.length) === message.length;
apdu += final ? '80' : '00';
apdu += this.byteToHex(accountIndex);
apdu += this.byteToHex(chunk.length / 2);
apdu += chunk;
offset += chunk.length;
const result: Buffer = await transport.exchange(Buffer.from(apdu, 'hex'));