-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Final integration to Bitbox02, Ledger and Portal NFC
- Loading branch information
Showing
8 changed files
with
438 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,57 @@ | ||
import { BitBox } from './bitbox'; | ||
import * as bitbox from 'bitbox-api'; // Ensure the 'bitbox-api' module is correctly installed and accessible | ||
|
||
export { BitBox }; | ||
// Main function to get and interact with the BitBox hardware | ||
async function getHardware(): Promise<void> { | ||
try { | ||
const onClose = () => { | ||
console.log('BitBox device disconnected.'); | ||
// Additional logic to handle disconnect | ||
}; | ||
|
||
// Automatically connect to BitBox | ||
const unpaired = await bitbox.bitbox02ConnectAuto(onClose); | ||
console.log('Unpaired BitBox connected.'); | ||
|
||
// Handle pairing and unlocking | ||
const pairing = await unpaired.unlockAndPair(); | ||
const pairingCode = pairing.getPairingCode(); | ||
|
||
if (pairingCode) { | ||
console.log('Display this pairing code to the user:', pairingCode); | ||
// Add logic here to visually display the pairing code in your UI if needed | ||
} | ||
|
||
// Wait for user to confirm pairing on the device | ||
const bb02 = await pairing.waitConfirm(); | ||
console.log('Device paired successfully.'); | ||
|
||
// Retrieve information from the device | ||
console.log('Product:', bb02.product()); | ||
console.log('Supports Ethereum functionality?', bb02.ethSupported()); | ||
const deviceInfos = await bb02.deviceInfo(); | ||
console.log('Device information:', deviceInfos); | ||
} catch (err) { | ||
const typedErr = bitbox.ensureError(err); // Ensure errors are properly typed and handled | ||
console.error('An error occurred while connecting to BitBox:', typedErr); | ||
} | ||
} | ||
|
||
// Placeholder object for BitBox functions | ||
export const BitBox = { | ||
connect: async (): Promise<void> => { | ||
await getHardware(); | ||
}, | ||
signTransaction: async (transaction: object): Promise<string> => { | ||
console.log('Signing transaction:', transaction); | ||
// Add logic here to sign a transaction using BitBox | ||
return 'SignedTransactionHash'; // Replace with the actual result from the API | ||
}, | ||
getAddress: async (path: string): Promise<string> => { | ||
console.log(`Retrieving address for path: ${path}`); | ||
// Add logic here to fetch an address using BitBox | ||
return 'bitbox-address-123'; // Replace with the actual result from the API | ||
}, | ||
}; | ||
|
||
// Export the `getHardware` function as well for standalone usage | ||
export { getHardware }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,50 @@ | ||
import { ReactPortal } from "react"; | ||
import { bitbox } from "./bitbox/bitbox"; | ||
import TrezorConnect, { Params, SignTransaction } from '@trezor/connect'; | ||
|
||
aysnc function getHardware(): Promise<ReactPortal> { | ||
return bitbox(); | ||
} | ||
async function connectToTrezor() { | ||
try { | ||
// Initialize Trezor Connect | ||
TrezorConnect.init({ | ||
manifest: { | ||
email: 'your-email@example.com', // Your email address (required for Trezor policy compliance) | ||
appUrl: 'https://your-app-url.com', // Your application's URL | ||
}, | ||
}); | ||
|
||
// Check device and get public key | ||
const response = await TrezorConnect.getPublicKey({ | ||
path: "m/44'/0'/0'/0/0", // Example BIP44 path (update according to your requirements) | ||
}); | ||
|
||
if (response.success) { | ||
console.log('Public Key:', response.payload.publicKey); | ||
console.log('Serialized Path:', response.payload.serializedPath); | ||
console.log('XPub:', response.payload.xpub); | ||
} else { | ||
console.error('Failed to connect:', response.payload.error); | ||
} | ||
} catch (error) { | ||
console.error('An error occurred while connecting to Trezor:', error); | ||
} | ||
} | ||
// Example of signing a transaction with Trezor | ||
async function signTransaction(transaction: Params<SignTransaction>) { | ||
try { | ||
const response = await TrezorConnect.signTransaction(transaction); | ||
|
||
if (response.success) { | ||
console.log('Transaction Signed:', response.payload); | ||
return response.payload.signatures; | ||
} else { | ||
console.error('Transaction signing failed:', response.payload.error); | ||
return null; | ||
} | ||
} catch (error) { | ||
console.error('An error occurred while signing transaction with Trezor:', error); | ||
return null; | ||
} | ||
} | ||
// Export reusable functions for connecting and using Trezor | ||
export const Trezor = { | ||
connect: connectToTrezor, | ||
signTransaction, | ||
}; |
Oops, something went wrong.