Skip to content

Commit

Permalink
implement ledger support
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
  • Loading branch information
gregdhill committed Aug 19, 2020
1 parent 87040c4 commit 27e73d2
Show file tree
Hide file tree
Showing 19 changed files with 1,242 additions and 98 deletions.
47 changes: 4 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
# Ledger BTC Wallet - React

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
```bash
HTTPS=true yarn start
```
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@interlay/esplora-btc-api": "^0.1.6",
"@ledgerhq/hw-app-btc": "^5.22.0",
"@ledgerhq/hw-transport-u2f": "^5.22.0",
"@ledgerhq/hw-transport-webusb": "^5.22.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/ledgerhq__hw-app-btc": "^5.19.1",
"@types/ledgerhq__hw-transport-u2f": "^4.21.1",
"@types/ledgerhq__hw-transport-webusb": "^4.70.0",
"@types/node": "^12.0.0",
"@types/platform": "^1.3.2",
"@types/react": "^16.9.0",
"@types/react-copy-to-clipboard": "^4.3.0",
"@types/react-dom": "^16.9.0",
"bitcoinjs-lib": "^5.1.10",
"bootstrap": "^4.5.2",
"platform": "^1.3.6",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-copy-to-clipboard": "^5.0.2",
"react-dom": "^16.13.1",
"react-icons": "^3.11.0",
"react-scripts": "3.4.3",
"typescript": "~3.7.2"
"typescript": "~3.8.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
Binary file added public/android-chrome-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/android-chrome-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/favicon.ico
Binary file not shown.
Binary file added public/favicon_io.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
content="Ledger Bitcoin Web Wallet"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/lgo.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand All @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Ledger BTC</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/site.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
209 changes: 185 additions & 24 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,187 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
import React, { Component } from "react";
import { BitcoinApi, satToBtc } from "./bitcoin";
import * as ledger from "./ledger";
import {
Container,
Row,
Col,
Table,
Button,
Spinner,
Jumbotron,
} from "react-bootstrap";
import "./App.css";
import Wizard from "./Wizard";

const ENTRIES_PER_PAGE = 3;

interface State {
appBtc?: ledger.AppBtc;
apiBtc: BitcoinApi;
ready: boolean;
loaded: boolean;
accIndex: number;
accounts: Record<string, { index: number; value: number }>;
showModal: boolean;
btcAddress: string;
btcIndex: number;
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(() => resolve(), ms));
}

export default App;
export default class App extends Component<{}, State> {
state: State = {
apiBtc: new BitcoinApi(),
ready: false,
loaded: false,
accIndex: 0,
accounts: {},
showModal: false,
btcAddress: "",
btcIndex: 0,
};

async componentDidMount() {
while (true) {
try {
const app = await ledger.connect();
await ledger.getWalletAddress(app, 0);
this.setState({ appBtc: app, ready: true });
break;
} catch (error) {
console.log(error);
}
await sleep(1000);
}

if (this.state.ready) {
await this.loadAccounts(ENTRIES_PER_PAGE);
}
}

async loadAccounts(length: number) {
const index = this.state.accIndex;
let promises = [];
for (let i = index; i < index + length; i++) {
const addr = await ledger.getWalletAddress(this.state.appBtc!, i);
const accounts = this.state.accounts;
accounts[addr] = { index: i, value: 0 };

promises.push(
this.state.apiBtc.getAccountValue(addr).then((value) => {
const accounts = this.state.accounts;
accounts[addr].value = value;
this.setState({
accounts: accounts,
});
})
);

this.setState({
accounts: accounts,
});
}
await Promise.all(promises);
this.setState({
loaded: true,
accIndex: index + length,
});
}

async showModal(addr: string, index: number) {
this.setState({
btcAddress: addr,
btcIndex: index,
showModal: true,
});
}

exitModal() {
this.setState({ showModal: false });
}

render() {
return (
<div className="App">
<Container>
{!this.state.ready && (
<div>
<Jumbotron>
<h1 className="header">Connect Your Device</h1>
<p>You may need to open the Bitcoin app.</p>
</Jumbotron>
<Spinner
as="span"
animation="border"
role="status"
aria-hidden="true"
/>
</div>
)}

{this.state.ready && (
<div>
<Jumbotron>
<h1 className="header">Select An Address</h1>
</Jumbotron>

<Row className="justify-content-md-center">
<Col>
<Table striped bordered hover>
<thead>
<tr>
<th>Address</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{Object.keys(this.state.accounts).map((addr) => {
return (
<tr
key={addr}
onClick={() =>
this.showModal(
addr,
this.state.accounts[addr].index
)
}
>
<td>{addr}</td>
<td>
{satToBtc(this.state.accounts[addr].value)} BTC
</td>
</tr>
);
})}
</tbody>
</Table>
</Col>
</Row>
<Row className="justify-content-md-center">
{this.state.loaded && (
<Button
onClick={() => {
this.setState({ loaded: false });
this.loadAccounts(ENTRIES_PER_PAGE);
}}
>
Show More
</Button>
)}
</Row>
</div>
)}
</Container>
{this.state.showModal && this.state.appBtc && (
<Wizard
exitModal={this.exitModal.bind(this)}
appBtc={this.state.appBtc}
{...this.state}
></Wizard>
)}
</div>
);
}
}
Loading

0 comments on commit 27e73d2

Please sign in to comment.