diff --git a/app/lnd/methods/index.js b/app/lnd/methods/index.js index 3f35be1b..6782a3d1 100644 --- a/app/lnd/methods/index.js +++ b/app/lnd/methods/index.js @@ -8,6 +8,7 @@ import disconnectpeer from './disconnectpeer' import info from './info' import invoice from './invoice' import invoices from './invoices' +import newaddress from './newaddress' import openchannel from './openchannel' import payinvoice from './payinvoice' import payments from './payments' @@ -22,6 +23,12 @@ export default function (lnd, event, msg, data) { .then(infoData => event.sender.send('receiveInfo', infoData)) .catch(error => console.log('info error: ', error)) break + case 'newaddress': + // Data looks like { address: '' } + newaddress(lnd, data.type) + .then(({ address }) => event.sender.send('receiveAddress', address)) + .catch(error => console.log('newaddress error: ', error)) + break case 'peers': // Data looks like { peers: [] } peers(lnd) diff --git a/app/lnd/methods/newaddress.js b/app/lnd/methods/newaddress.js new file mode 100644 index 00000000..d2a91d1f --- /dev/null +++ b/app/lnd/methods/newaddress.js @@ -0,0 +1,10 @@ +// LND Generate New Address +export default function info(lnd, type) { + return new Promise((resolve, reject) => { + lnd.newAddress({ type }, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} diff --git a/app/reducers/address.js b/app/reducers/address.js new file mode 100644 index 00000000..7ecb63f5 --- /dev/null +++ b/app/reducers/address.js @@ -0,0 +1,53 @@ +import { ipcRenderer } from 'electron' +// ------------------------------------ +// Constants +// ------------------------------------ +export const GET_ADDRESS = 'GET_ADDRESS' +export const RECEIVE_ADDRESS = 'RECEIVE_ADDRESS' + +// LND expects types to be sent as int, so this object will allow mapping from string to int +const addressTypes = { + p2wkh: 0, + np2wkh: 1, + p2pkh: 2 +} + +// ------------------------------------ +// Actions +// ------------------------------------ +export function getAddress() { + return { + type: GET_ADDRESS + } +} + +// Send IPC event for getinfo +export const newAddress = type => async (dispatch) => { + dispatch(getAddress()) + ipcRenderer.send('lnd', { msg: 'newaddress', data: { type: addressTypes[type] } }) +} + +// Receive IPC event for info +export const receiveAddress = (event, address) => dispatch => dispatch({ type: RECEIVE_ADDRESS, address }) + +// ------------------------------------ +// Action Handlers +// ------------------------------------ +const ACTION_HANDLERS = { + [GET_ADDRESS]: state => ({ ...state, addressLoading: true }), + [RECEIVE_ADDRESS]: (state, { address }) => ({ ...state, addressLoading: false, address }) +} + +// ------------------------------------ +// Reducer +// ------------------------------------ +const initialState = { + addressLoading: false, + address: '' +} + +export default function addressReducer(state = initialState, action) { + const handler = ACTION_HANDLERS[action.type] + + return handler ? handler(state, action) : state +} diff --git a/app/reducers/index.js b/app/reducers/index.js index d7df67f8..9126ea8b 100644 --- a/app/reducers/index.js +++ b/app/reducers/index.js @@ -9,6 +9,7 @@ import peers from './peers' import channels from './channels' import form from './form' import invoice from './invoice' +import address from './address' const rootReducer = combineReducers({ router, @@ -19,7 +20,8 @@ const rootReducer = combineReducers({ peers, channels, form, - invoice + invoice, + address }) export default rootReducer diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index 3d2ade7c..a0823a20 100644 --- a/app/reducers/ipc.js +++ b/app/reducers/ipc.js @@ -1,5 +1,6 @@ import createIpc from 'redux-electron-ipc' import { receiveInfo } from './info' +import { receiveAddress } from './address' import { receivePeers, connectSuccess, disconnectSuccess } from './peers' import { receiveChannels, @@ -30,7 +31,8 @@ const ipc = createIpc({ pushchannelerror, pushchannelstatus, connectSuccess, - disconnectSuccess + disconnectSuccess, + receiveAddress }) export default ipc diff --git a/app/routes/wallet/components/Wallet.js b/app/routes/wallet/components/Wallet.js index b6c69a58..f152203a 100644 --- a/app/routes/wallet/components/Wallet.js +++ b/app/routes/wallet/components/Wallet.js @@ -7,16 +7,18 @@ import styles from './Wallet.scss' class Wallet extends Component { componentWillMount() { - const { fetchInfo, fetchPeers, fetchChannels } = this.props + const { fetchInfo, fetchPeers, fetchChannels, newAddress } = this.props fetchInfo() fetchPeers() fetchChannels() + newAddress('p2pkh') } render() { const { info, + address: { address }, ticker, peers: { peersLoading, peers, peer, peerForm }, channels: { channelsLoading, channels, channel, channelForm, pendingChannels }, @@ -35,8 +37,17 @@ class Wallet extends Component { return (
- -

{info.data.identity_pubkey}

+
+ +

{info.data.identity_pubkey}

+

+ +

+
({ info: state.info, + address: state.address, ticker: state.ticker, peers: state.peers,