Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance server capabilities for miniwallet and sms callbacks #6

Closed
wants to merge 9 commits into from
Closed
30 changes: 30 additions & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Client Configuration

# Values set for local development
DEBUG=1
RPC='http://localhost:8545'
ROOT_URL='https://localhost:3100/'
SERVER_NETWORK='eth-ganache'
ASSET_MANAGER_GANACHE=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0

# Mandatory Values
IPFS_GATEWAY='https://ipfs.infura.io/ipfs/{{hash}}'


# Default Values (can be omitted)
GAS_PRICE=1000
EXPLORER_URL='https://explorer.harmony.one/#/tx/{{txId}}'
SERVER_URL='https://localhost:8443'
SERVER_SECRET='none'

# TRANSAK Information

## TRANSAK Staging
TRANSAK_STAGING_API_KEY: '12a1a123-1234-1234-a123-12345ab12a12'
TRANSAK_STAGING_DEFAULT_CURRENCY: 'USD'
TRANSAK_STAGING_ENVIRONMENT: 'STAGING'

## TRANSAK Production
TRANSAK_PRODUCTION_API_KEY: '12a1a123-1234-1234-a123-12345ab12a12'
TRANSAK_PRODUCTION_DEFAULT_CURRENCY: 'USD'
TRANSAK_PRODUCTION_ENVIRONMENT: 'PRODUCTION'
12 changes: 6 additions & 6 deletions client/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const config = {
network: process.env.SERVER_NETWORK || 'harmony',
transak: {
staging: {
apiKey: '50f1c430-7807-4760-a337-57583de69f73',
defaultCurrency: 'USD',
environment: 'STAGING'
apiKey: process.env.TRANSAK_STAGING_API_KEY,
defaultCurrency: process.env.TRANSAK_STAGING_DEFAULT_CURRENCY || 'USD',
environment: process.env.TRANSAK_STAGING_ENVIRONMENT || 'STAGING'
},
production: {
apiKey: '28c4ba82-b701-4d05-a44c-1466fbb99265',
defaultCurrency: 'USD',
environment: 'PRODUCTION'
apiKey: process.env.TRANSAK_PRODUCTION_API_KEY,
defaultCurrency: process.env.TRANSAK_PRODUCTION_DEFAULT_CURRENCY || 'USD',
environment: process.env.TRANSAK_PRODUCTION_ENVIRONMENT || 'PRODUCTION'
},
currencies: ['USD', 'NZD', 'AUD', 'EUR', 'GBP', 'CHF', 'SEK', 'PLN', 'NOK', 'MXN', 'DKK', 'CAD', 'ARS', 'BRL', 'CLP', 'CRC', 'DOP', 'IDR', 'ILS', 'JPY', 'KRW', 'MYR', 'PYG', 'PEN', 'PHP', 'SGD', 'ZAR', 'TZS', 'THB', 'TRY', 'BBD', 'BMD', 'BGN', 'HRK', 'CZK', 'FKP', 'FJD', 'GIP', 'HUF', 'ISK', 'JMD', 'KES', 'MDL', 'RON'],
countries: [
Expand Down
4 changes: 4 additions & 0 deletions demo/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DEBUG=1
SERVER_URL=https://localhost:8343
CLIENT_URL=https://localhost:3100
ASSET_MANAGER_GANACHE=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0
70 changes: 70 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Server Configuration

## Configurable (set these values to change default behaviour for local testing)
#RELAYER_DEBUG=1
#VERBOSE=1
#SAFE_NONCE=1

## Mandatory
CORS=*
SECRET='none'

# Datastore
GCP_PROJECT='sms-wallet-server'
GCP_CRED_PATH='./credentials/sms-wallet-server-credentials.json'
GCP_NAMESPACE='sms-wallet-server'

# Twilio
TWILIO_ACCOUNT_SID=ACab1ab1ab1ab1ab1ab1ab1ab1ab1ab1ab
TWILIO_AUTH_TOKEN=1ab11ab11ab1ab1ab1ab1ab1ab1ab1ab
TWILIO_FROM=+11231231234

# OTP
OTP_SALT='local-salt'

# Client
CLIENT_ROOT='https://localhost:3100/'

## Netork Information

# eth-ganache
GANACHE_RPC='http://localhost:8545'
GANACHE_WSS='ws://localhost:8545'
ETH_GANACHE_KEY='<<OPERATORS PRIVATE KEY>>'
ETH_GANACHE_MNEMONIC='test test test test test test test test test test test junk'
#SKIP_GANACHE=0
GANACHE_NUM_ACCOUNTS=1
GANACHE_ASSET_MANAGER=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0

# Harmony Testnet
HARMONY_TESTNET_KEY='<<OPERATORS PRIVATE KEY>>'
TESTNET_RPC='https://api.s0.b.hmny.io'
TESTNET_WSS='wss://ws.s0.pga.hmny.io/'
HARMONY_TESTNET_MNEMONIC='NOT USED'
SKIP_TESTNET=1
TESTNET_NUM_ACCOUNTS=1
TESTNET_ASSET_MANAGER=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0

# Harmony Mainnet
HARMONY_MAINNET_KEY='<<OPERATORS PRIVATE KEY>>'
BEACON_MAINNET_RPC='https://api.s0.t.hmny.io/'
MAINNET_RPC='https://api.s0.t.hmny.io/'
MAINNET_WSS='wss://ws.s0.t.hmny.io/'
HARMONY_MAINNET_MNEMONIC='NOT USED'
SKIP_MAINNET=1
MAINNET_NUM_ACCOUNTS=1
MAINNET_ASSET_MANAGER=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0



## Default Values (can be ommitted)
RELAYER_ID='unknown'
pollingInterval=1000
DEFAULT_NETWORK='harmony-mainnet'
GAS_LIMIT='12345678'
GAS_PRICE='200'
CACHE='cache'
STATS_PATH='../data/stats.json'
OTP_INTERVAL=60000


12 changes: 12 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ const https = require('https')
const http = require('http')
const env = process.env.NODE_ENV || 'development'
const fs = require('fs')
const blockchain = require('./blockchain')

Error.stackTraceLimit = 100
app.locals.ENV = env
app.locals.ENV_DEVELOPMENT = env === 'development'

app.set('trust proxy', true)

try {
blockchain.init().catch(ex => {
console.error('Blockchain initialization failed')
console.error(ex)
process.exit(2)
})
} catch (ex) {
console.error(ex)
process.exit(1)
}

// console.log('config', config)
let httpServer, httpsServer

Expand Down
45 changes: 45 additions & 0 deletions server/blockchain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const config = require('./config')
const { ethers } = require('ethers')
const { Logger } = require('./logger')
const AssetManager = require('../miniwallet/build/contracts/AssetManager.sol/AssetManager.json')

const networks = []
const providers = {}
const signers = {}
const assetManagers = {}

const init = async () => {
Logger.log('Initializing blockchain for server')
Object.keys(config.networks).forEach(k => {
if (config.networks[k].skip) {
Logger.log(`[${k}] Skipped initialization`)
return
}
const n = config.networks[k]
if (n.key) {
try {
Logger.log(`n.url: ${n.url}`)
providers[k] = ethers.getDefaultProvider(n.url)
Logger.log(`n.key: ${n.key}`)
signers[k] = new ethers.Wallet(n.key, providers[k])
Logger.log(`am address; ${config.networks[k].assetManagerAddress}`)
// console.log(`AssetManager.abi: ${JSON.stringify(AssetManager.abi)}`)
assetManagers[k] = new ethers.Contract(config.networks[k].assetManagerAddress, AssetManager.abi, signers[k])
Logger.log('AssetManager deployed to:', assetManagers[k].address)
networks.push(k)
} catch (ex) {
console.error(ex)
console.trace(ex)
}
}
})
console.log(`networks: ${networks}`)
}

module.exports = {
init,
getNetworks: () => networks,
getProvider: (network) => providers[network],
getSigner: (network) => signers[network],
getAssetManager: (network) => assetManagers[network],
}
7 changes: 5 additions & 2 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const config = {
skip: process.env.SKIP_TESTNET,
numAccounts: process.env.TESTNET_NUM_ACCOUNTS || 1,
blockTime: 2,
assetManagerAddress: process.env.TESTNET_ASSET_MANAGER,
},
'harmony-mainnet': {
key: process.env.HARMONY_MAINNET_KEY || '',
Expand All @@ -35,14 +36,16 @@ const config = {
skip: process.env.SKIP_MAINNET,
numAccounts: process.env.MAINNET_NUM_ACCOUNTS || 1,
blockTime: 2,
assetManagerAddress: process.env.MAINNET_ASSET_MANAGER,
},
'eth-ganache': {
url: process.env.GANACHE_RPC || 'http://127.0.0.1:7545',
url: process.env.GANACHE_RPC || 'http://localhost:8545',
wss: process.env.GANACHE_WSS,
key: process.env.ETH_GANACHE_KEY,
mnemonic: process.env.ETH_GANACHE_MNEMONIC,
skip: process.env.SKIP_GANACHE,
numAccounts: process.env.GANACHE_NUM_ACCOUNTS || 1,
assetManagerAddress: process.env.GANACHE_ASSET_MANAGER,
},
},
gasLimit: parseInt(process.env.GAS_LIMIT || '12345678'),
Expand All @@ -58,7 +61,7 @@ const config = {
cred: !process.env.GCP_CRED_PATH ? {} : require(process.env.GCP_CRED_PATH),
mock: !process.env.GCP_CRED_PATH,
mockPort: 9000,
namespace: 'sms-wallet-server'
namespace: process.env.GCP_NAMESPACE || 'sms-wallet-server'
},

twilio: {
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-standard": "^5.0.0",
"ethers": "^5.6.9",
"standard": "^17.0.0"
},
"resolutions": {
Expand Down
20 changes: 20 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ const NodeCache = require('node-cache')
const Cache = new NodeCache()
const { phone } = require('phone')
const Twilio = require('twilio')(config.twilio.sid, config.twilio.token)
const TwilioV = require('twilio')
const utils = require('../utils')
const { User } = require('../src/data/user')
const { isEqual, pick } = require('lodash')
const w3utils = require('../w3utils')
const stringify = require('json-stable-stringify')
const { Setting } = require('../src/data/setting')
const { Request } = require('../src/data/request')
const blockchain = require('../blockchain')
const { ethers } = require('ethers')

router.get('/health', async (req, res) => {
Logger.log('[/health]', req.fingerprint)
Expand Down Expand Up @@ -338,4 +341,21 @@ router.post('/request-complete', async (req, res) => {
}
})

// Process sms requests sent to the twilio operator Number
router.post('/sms', async (req, res) => {
// check valid Twilio Request
if (!TwilioV.validateRequest(config.twilio.token, req.headers['x-twilio-signature'], ('https://' + req.get('host') + '/sms'), req.body)) {
return res.status(StatusCodes.BAD_REQUEST).json({ error: 'request not from twilio' })
}
// Look up the from Phone Number to get the address
const { From: phoneNumber } = req.body
const u = await User.findByPhone({ phone: phoneNumber })
if (!u) {
return res.status(StatusCodes.BAD_REQUEST).json({ error: 'phone number does not exist' })
}
const assetManager = blockchain.getAssetManager('eth-ganache')
const balance = ethers.utils.formatEther(await assetManager.userBalances(u.address))
return res.json({ userAddress: u.address, balance: balance })
})

module.exports = router
2 changes: 1 addition & 1 deletion server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3592,7 +3592,7 @@ ethers@^4.0.32:
uuid "2.0.1"
xmlhttprequest "1.8.0"

ethers@^5.0.13:
ethers@^5.0.13, ethers@^5.6.9:
version "5.6.9"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.6.9.tgz#4e12f8dfcb67b88ae7a78a9519b384c23c576a4d"
integrity sha512-lMGC2zv9HC5EC+8r429WaWu3uWJUCgUCt8xxKCFqkrFuBDZXDYIdzDUECxzjf2BMF8IVBByY1EBoGSL3RTm8RA==
Expand Down