Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@digix/react-trezor-container

Package Overview
Dependencies
Maintainers
8
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@digix/react-trezor-container

React Component that implements [Trezor Connect](https://github.com/trezor/connect)

  • 1.1.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
8
Weekly downloads
 
Created
Source

React Trezor Container

React Component that implements Trezor Connect

Features

  • Public Address retrieval
    • Returns an HD wallet that allows you to retrieve public addresses based ond the provided path
  • Transactions
    • signTransaction
    • sigMessage
    • getAddress
  • getAddresses option for pulling addresses based on the specified path (defaults to first account if not provided)
  • expect option for validating address
  • onReady event handler for triggering actions

Example

  • Getting addresses
  import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Table, Message } from 'semantic-ui-react';

import TrezorContainer from '@digix/react-trezor-container';

import TrezorAddressList from './trezor_keystore_address_list';
import TrezorKeystoreAddressItem from './trezor_keystore_address_item';

export default class TrezorKeystoreCreationForm extends Component {
  constructor(props) {
    super(props);
    this.renderItem = this.renderItem.bind(this);
    this.handleItemChange = this.handleItemChange.bind(this);
    this.renderContainer = this.renderContainer.bind(this);
  }
  handleItemChange(e) {
    const { name, value } = e;
    const { enabled } = value;
    const addresses = (this.props.formData || {}).addresses || {};
    if (!enabled && addresses) {
      delete addresses[name];
      this.props.formChange({ name: 'addresses', value: { ...addresses } });
    } else {
      const update = {
        networks: (addresses[name] || {}).networks || this.props.formData.networks,
        tokens: (addresses[name] || {}).tokens || this.props.formData.tokens,
        ...value,
      };
      this.props.formChange({ name: 'addresses', value: { ...addresses, [name]: { ...addresses[name], ...update } } });
    }
  }

  renderContainer({ renderItems }) {
    const count = Object.values(this.props.formData.addresses || {}).filter(a => a.enabled).length;
    return (
      <Table>
        <Table.Header>
          <Table.Row>
            <Table.HeaderCell>Select addresses to enable</Table.HeaderCell>
            <Table.HeaderCell textAlign="right" colSpan={100}>
              {count} enabled
            </Table.HeaderCell>
          </Table.Row>
        </Table.Header>
        <Table.Body>{renderItems()}</Table.Body>
      </Table>
    );
  }
  renderError({ error }) {
    return <Message icon="warning sign" negative header="Trezor Error" content={`${error}`} />;
  }
  renderItem(item) {
    if (!item.address) {
      return (
        <Table.Row disabled key={item.kdPath}>
          <Table.Cell colSpan={100}>Getting Address Info...</Table.Cell>
        </Table.Row>
      );
    }
    const data = ((this.props.formData || {}).addresses || {})[item.kdPath] || {};
    return <TrezorKeystoreAddressItem key={item.kdPath} {...item} data={data} onChange={this.handleItemChange} />;
  }
  render() {
    const { renderContainer, renderItem } = this;
    // return <TrezorAddressList {...this.props} {...{ renderContainer, renderItem }} />;
    return (
      <TrezorContainer
        renderError={this.renderError}
        renderLoading={() => (
          <Message
            icon="info circle"
            info
            header="Connecting to Trezor"
            content="Please follow instructions on the Trezor Popup window to continue."
          />
        )}
        getAddresses={props => <TrezorAddressList {...props} {...{ renderContainer, renderItem }} />}
      />
    );
  }
}

TrezorKeystoreCreationForm.propTypes = {
  formData: PropTypes.object.isRequired,
  formChange: PropTypes.func.isRequired,
};
  • Signing Transaction
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Message } from 'semantic-ui-react';
import TrezorContainer from '@digix/react-trezor-container';

export default class TrezorKeystoreTransactionSigner extends Component {
  constructor(props) {
    super(props);
    this.handleSign = this.handleSign.bind(this);
    this.state = { signed: false };
  }
  handleSign({ signTransaction }) {
    const { txData, address, hideTxSigningModal } = this.props;
    const { kdPath } = address;
    signTransaction(kdPath, txData, hideTxSigningModal)
      .then((signedTx) => {
        this.setState({ signed: true });
        hideTxSigningModal({ signedTx });
      })
      .catch(error => this.setState({ error }));
  }
  renderError() {
    const { error } = this.state;
    return <Message icon="warning sign" negative header={'Trezor Transaction'} content={error} />;
  }
  render() {
    const { kdPath, address } = this.props.address;
    const { error } = this.state;
    if (error) {
      return this.renderError();
    }
    const { signed } = this.state;
    return (
      <TrezorContainer
        expect={{ kdPath, address }}
        onReady={this.handleSign}
        signed={signed}
        renderReady={() => (
          <Message
            icon="check"
            positive
            header={'Ready to Sign Transaction'}
            content="Trezor Popup window is loading. Please follow instructions from the popup and your Trezor Wallet to continue."
          />
        )}
      />
    );
  }
}

TrezorKeystoreTransactionSigner.propTypes = {
  hideTxSigningModal: PropTypes.func.isRequired,
  address: PropTypes.object.isRequired,
  txData: PropTypes.object.isRequired,
};

TODO

  • Bitcoin support

Keywords

FAQs

Package last updated on 28 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc