Commit c5095c47 authored by Bitcoin Please's avatar Bitcoin Please

Light refactoring, plus added `blockchain` to store.

parent 0d794e19
......@@ -49,9 +49,7 @@ import { mapActions, mapGetters } from 'vuex'
import Email from './SigninEmail'
import Extensions from './SigninExtensions'
import Ledger from './SigninLedger'
/* Import icons. */
// import '@/compiled-icons/<icon-name>'
import Swal from 'sweetalert2'
/* Import JQuery. */
// FIXME: Remove ALL jQuery dependencies.
......@@ -98,9 +96,28 @@ export default {
/**
* Sign In
*/
signIn() {
$('.form-signin').fadeToggle()
$('#signinForm').fadeToggle()
async signIn() {
// $('.form-signin').fadeToggle()
// $('#signinForm').fadeToggle()
const { value: formValues } = await Swal.fire({
title: 'Multiple inputs',
html:
'<input id="swal-input1" class="swal2-input">' +
'<input id="swal-input2" class="swal2-input">',
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById('swal-input1').value,
document.getElementById('swal-input2').value
]
}
})
if (formValues) {
Swal.fire(JSON.stringify(formValues))
}
},
/**
......
/* Import modules (getters). */
// ...
/* Import modules (actions). */
import startWatching from './blockchain/actions/startWatching'
/* Import modules (mutations). */
import setWatching from './blockchain/mutations/setWatching'
/* Initialize state. */
const state = {
/**
* Watching
*
* 1. Addresses
* 2. UTXOs
*/
watching: null,
}
/* Getters. */
const getters = {
//
}
/* Actions. */
const actions = {
startWatching,
}
/* Mutations. */
const mutations = {
setWatching,
}
/* Export. */
// NOTE: We DO NOT namespace here to allow for global use of `dispatch`.
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
/**
* Start Watching
*
* Pulls the latest asset source from our Eternal Db.
*/
const startWatching = async ({ commit }, _request) => {
console.log('Start watching...', _request)
/* Commit wallet's master seed. */
commit('setWatching', _request)
}
/* Export module. */
export default startWatching
/* Import modules. */
import msgpack from 'msgpack-lite'
/**
* Set Watching
*
* Allows us to update a stored asset resource.
*/
const setWatching = (state, _watching) => {
/* Update the asset resource. */
state.watching = msgpack.encode(_watching).toString('hex')
}
/* Export module. */
export default setWatching
/* Import libraries. */
// import telr from '../../api/telr'
/* Import modules (getters). */
import getAsset from './campaigns/getters/getAsset'
import getCampaign from './campaigns/getters/getCampaign'
......@@ -9,6 +6,7 @@ import getFullfillment from './campaigns/getters/getFullfillment'
/* Import modules (actions). */
import addAssurance from './campaigns/actions/addAssurance'
import addPayout from './campaigns/actions/addPayout'
import updateAsset from './campaigns/actions/updateAsset'
import updateCampaign from './campaigns/actions/updateCampaign'
......@@ -38,6 +36,7 @@ const getters = {
/* Actions. */
const actions = {
addAssurance,
addPayout,
updateAsset,
updateCampaign,
}
......
/* Import modules. */
import superagent from 'superagent'
/**
* Add Payout
*/
const addPayout = async ({ rootGetters }, _payout) => {
// console.log('ADD PAYOUT:', _payout)
const signedPkg = rootGetters['profile/getSignedMessage'](JSON.stringify(_payout))
// console.log('SIGNED PACKAGE', signedPkg)
/* Set api target. */
const target = 'http://localhost:6767/v1/payouts'
// const target = 'https://api.causes.cash/v1/payouts'
const result = await superagent
.post(target)
.send(signedPkg)
// console.log('RESULT', result)
/* Return result. */
return result
}
/* Export module. */
export default addPayout
/* Import modules. */
const Nito = require('nitojs')
import Nito from 'nitojs'
const DUST_LIMIT = 546
const MAX_LIMIT = 2099999997690000
......
/* Import libraries. */
// import telr from '../../api/telr'
/* Import modules (getters). */
import getAvatar from './profile/getters/getAvatar'
import getEmail from './profile/getters/getEmail'
......
/* Initialize BITBOX. */
const Nito = require('nitojs')
/* Import modules. */
const moment = require('moment')
import Nito from 'nitojs'
/**
* Initialize Wallet
......@@ -28,6 +29,17 @@ const initProfile = async ({ state, commit }) => {
/* Set new master (private) key. */
commit('setMasterSeed', masterSeed)
/* Build metadata. */
const meta = {
label: 'My Causes Account',
addresses: {},
coins: {},
createdAt: moment().unix(),
}
/* Set metadata. */
commit('setMeta', meta)
}
/* Export module. */
......
......@@ -36,6 +36,9 @@ const updateMeta = async ({ commit, getters }, _meta) => {
/* Commit metadata. */
commit('setMeta', _meta)
const key = getters.getMasterSeed
console.log('MASTER SEED (key)', key)
/**
* Encrypt Metadata
*
......@@ -43,7 +46,7 @@ const updateMeta = async ({ commit, getters }, _meta) => {
* data repository. For privacy reasons, ALL metadata is first encrypted
* with a key ONLY know to this profile.
*/
const encrypted = _encrypt(JSON.stringify(_meta))
const encrypted = _encrypt(JSON.stringify(_meta), key)
console.log('UPDATE META (encrypted):', encrypted)
const signedPkg = getters.getSignedMessage(encrypted)
......
/* Import modules. */
const Nito = require('nitojs')
import Nito from 'nitojs'
/**
* Get Avatar
......
......@@ -60,7 +60,10 @@ const getMeta = async (state, getters, rootState, rootGetters) => {
/* Set contract path. */
const response = await superagent.get(target)
console.log('GET META', response)
console.log('GET META (decrypted)', _decrypt(response))
const key = getters.getMasterSeed
console.log('MASTER SEED (key)', key)
console.log('GET META (decrypted)', _decrypt(response, key))
return null
} else {
......
/* Import modules. */
import moment from 'moment'
const Nito = require('nitojs')
import Nito from 'nitojs'
/**
* Get Signed Message
......
/* Import libraries. */
// import telr from '../../api/telr'
/* Import modules (getters). */
import getIPFS from './storage/getters/getIPFS'
import getOrbitDB from './storage/getters/getOrbitDB'
......
/* Import libraries. */
// import telr from '../../api/telr'
/* Import modules (getters). */
import getAssetSource from './system/getters/getAssetSource'
import getEternalDbAddr from './system/getters/getEternalDbAddr'
......
/* Import libraries. */
// import telr from '../../api/telr'
/* Import modules (getters). */
import getBytesFromIPFS from './utils/getters/getBytesFromIPFS'
import getCompletedPct from './utils/getters/getCompletedPct'
......
......@@ -3,13 +3,15 @@ import getAccounts from './wallet/getters/getAccounts'
import getAddress from './wallet/getters/getAddress'
import getBalance from './wallet/getters/getBalance'
import getChangeAddress from './wallet/getters/getChangeAddress'
// import getCoinById from './wallet/getters/getCoinById'
import getCoins from './wallet/getters/getCoins'
import getDerivationPath from './wallet/getters/getDerivationPath'
import getDustAmount from './wallet/getters/getDustAmount'
import getHDNode from './wallet/getters/getHDNode'
import getHistory from './wallet/getters/getHistory'
import getMasterSeed from './wallet/getters/getMasterSeed'
import getInbox from './wallet/getters/getInbox'
import getMnemonic from './wallet/getters/getMnemonic'
import getWallet from './wallet/getters/getWallet'
import getOutbox from './wallet/getters/getMnemonic'
/* Import modules (actions). */
import addCoin from './wallet/actions/addCoin'
......@@ -20,29 +22,48 @@ import updateCoins from './wallet/actions/updateCoins'
import updateOutbox from './wallet/actions/updateOutbox'
/* Import modules (mutations). */
import setAccounts from './wallet/mutations/setAccounts'
import setCoins from './wallet/mutations/setCoins'
import setEmptyWallet from './wallet/mutations/setEmptyWallet'
import setWallet from './wallet/mutations/setWallet'
import setInbox from './wallet/mutations/setInbox'
import setOutbox from './wallet/mutations/setOutbox'
/* Initialize state. */
const state = {
/**
* Coins waiting to be sent out from the wallet.
* Accounts
*
* Manages the index of account addresses.
*/
outbox: null,
accounts: null,
/**
* Coins
*
* Manages coin info in an object:
* - status [ active | disabled | locked ]
* - txid
* - vout
* - satoshis
* - wif
* - cashAddress
* - legacyAddress
*/
coins: null,
/**
* Wallet Accounts
* Inbox
*
* Deposit / receiving accounts are distributed to the public; and can
* be pre-generated for specific purposes (eg. donation, invoice, etc).
* Requests waiting to be processed by the wallet.
*/
inbox: null,
/**
* Outbox
*
* NOTE: Dedicated derivation paths are used for ALL deposited / received
* coins and tokens. The value (0) is the community standard for
* change accounts, therefore:
* - Coins: m/44'/145'/0'/0/<index>
* - Tokens: m/44'/245'/0'/0/<index>
* Coins waiting to be sent out from the wallet.
*/
wallet: null,
outbox: null,
}
/* Getters. */
......@@ -51,13 +72,14 @@ const getters = {
getAddress,
getBalance,
getChangeAddress,
getCoins,
getDerivationPath,
getDustAmount,
getHDNode,
getHistory,
getMasterSeed,
getInbox,
getMnemonic,
getWallet,
getOutbox,
}
/* Actions. */
......@@ -72,8 +94,11 @@ const actions = {
/* Mutations. */
const mutations = {
setAccounts,
setCoins,
setEmptyWallet,
setWallet,
setInbox,
setOutbox,
}
/* Export. */
......
/* Import modules. */
const moment = require('moment')
/**
* Initialize Wallet
*/
......@@ -54,17 +51,6 @@ const initWallet = ({ commit }) => {
/* Commit wallet. */
commit('setWallet', walletModel)
/* Build metadata. */
const meta = {
label: 'My Causes Wallet',
addresses: {},
coins: {},
createdAt: moment().unix(),
}
/* Set metadata. */
commit('setMeta', meta)
/* Return success. */
return true
}
......
/* Import components. */
const Nito = require('nitojs')
import Nito from 'nitojs'
/**
* Update Status
......
/* Import modules. */
const Nito = require('nitojs')
import msgpack from 'msgpack-lite'
import Nito from 'nitojs'
/**
* Load (Derivation) Path
*/
const loadPath = (_getters, _accounts, _chainId, _acctIdx) => {
/* Initialize HD node. */
const hdNode = _getters.getHDNode
/* Set derivation path. */
const path = _getters.getDerivationPath(_chainId, _acctIdx)
// console.log('GET ACCOUNTS (path)', path)
/* Initialize HD node. */
const hdNode = _getters.getHDNode
/* Initialize child node. */
const childNode = hdNode.deriveChild(path)
/* Set WIF. */
const wif = childNode.privateKey.toWIF()
/* Set account (address). */
const address = Nito.Address.toCashAddress(childNode)
// console.log('GET ACCOUNTS (address)', address)
/* Set WIF. */
const wif = childNode.privateKey.toWIF()
/* Add to all receiving (pool). */
_accounts.push({
acctIdx: _acctIdx,
chainId: _chainId,
address,
wif,
......@@ -38,35 +40,34 @@ const loadPath = (_getters, _accounts, _chainId, _acctIdx) => {
* (incl. index and WIF) for ALL derivation paths in-use for a wallet.
*/
const getAccounts = (state, getters) => {
/* Initialize accounts. */
const accounts = []
/* Validate wallet. */
if (!getters.getWallet) {
/* Validate state. */
if (!state || !state.accounts) {
return null
}
/* Initialize wallet. */
const wallet = getters.getWallet
// console.log('GET ACCOUNTS (wallet):', wallet)
/* Initialize accounts. */
const accounts = []
/* Initialize account indexes. */
const acctIndexes = msgpack.decode(Buffer.from(state.accounts, 'hex'))
/* Loop through ALL (deposit) indexes (inclusive). */
for (let i = 0; i <= wallet.accounts.deposit; i++) {
for (let i = 0; i <= acctIndexes.deposit; i++) {
loadPath(getters, accounts, 0, i)
}
/* Loop through ALL (change) indexes (inclusive). */
for (let i = 0; i <= wallet.accounts.change; i++) {
for (let i = 0; i <= acctIndexes.change; i++) {
loadPath(getters, accounts, 1, i)
}
/* Loop through ALL (causes) indexes (inclusive). */
for (let i = 0; i <= wallet.accounts.causes; i++) {
for (let i = 0; i <= acctIndexes.causes; i++) {
loadPath(getters, accounts, 6767, i)
}
/* Loop through ALL (nito) indexes (inclusive). */
for (let i = 0; i <= wallet.accounts.nito; i++) {
for (let i = 0; i <= acctIndexes.nito; i++) {
loadPath(getters, accounts, 7867, i)
}
// console.log('GET ACCOUNTS (accounts):', accounts)
......
/* Import modules. */
const Nito = require('nitojs')
import Nito from 'nitojs'
/**
* Get Address
......
/* Import modules. */
const Nito = require('nitojs')
import Nito from 'nitojs'
/**
* Get Balance by Session Id
......@@ -11,7 +11,10 @@ const Nito = require('nitojs')
*/
const getBalanceBySessionId = (
state, getters, rootState, rootGetters
) => async (_currency) => {
) => async (_quoteCurrency) => {
/* Set base currency. */
const baseCurrency = 'BCH'
/* Retrieve accounts. */
const accounts = getters.getAccounts
console.log('GET BALANCE (accounts)', accounts)
......@@ -59,15 +62,15 @@ const getBalanceBySessionId = (
}
/* Retrieve market price. */
const marketPrice = await Nito.Markets.getTicker('BCH', _currency)
console.info(`Market price (${_currency})`, marketPrice) // eslint-disable-line no-console
const marketPrice = await Nito.Markets.getTicker(baseCurrency, _quoteCurrency)
console.info(`Market price (${_quoteCurrency})`, marketPrice) // eslint-disable-line no-console
/* Validate market price. */
if (marketPrice) {
/* Format balance (for display). */
// TODO: Support additional currencies.
const formattedBalance =
rootGetters['utils/getFormattedValue'](balance, marketPrice, 'USD')
rootGetters['utils/getFormattedValue'](balance, marketPrice, _quoteCurrency)
/* Return (formatted) balance. */
return formattedBalance
......
/* Import modules. */
import msgpack from 'msgpack-lite'
const Nito = require('nitojs')
import Nito from 'nitojs'
/**
* Get Change Address
......
......@@ -2,22 +2,20 @@
import msgpack from 'msgpack-lite'
/**
* Get Wallet
*
* Returns all addresses and metadata for the current wallet.
* Get Coins
*/
const getWallet = (state) => {
const getCoins = (state) => {
/* Validate state. */
if (!state || !state.wallet) {
if (!state || !state.coins) {
return null
}
/* Initialize wallet. */
const wallet = msgpack.decode(Buffer.from(state.wallet, 'hex'))
/* Initialize coins. */
const coins = msgpack.decode(Buffer.from(state.coins, 'hex'))
/* Return wallet. */