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

Change per as review from taniguchi san #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module.exports = {
parser: 'babel-eslint',
extends: 'standard',
plugins: ['mocha'],
plugins: ['mocha', 'chai-friendly'],
parserOptions: {
'ecmaVersion':2017
},
env: {'mocha': true},
rules: {
'space-before-function-paren': ['error', 'never'],
'no-underscore-dangle': 0,
'mocha/no-exclusive-tests': 'error'
'mocha/no-exclusive-tests': 'error',
"no-unused-expressions": 0,
"chai-friendly/no-unused-expressions": 2
}
}
24 changes: 24 additions & 0 deletions lib/community.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Transaction from './transaction'

/**
* Community managing class
*/
class Community {
constractor(wallet) {
this.wallet = wallet
}
/**
* create new Community.
* @param name (unique) as communityId.
* @return {Promise<Transaction>}
*/
async create(name) {
return new Transaction()
}

get [Symbol.toStringTag]() {
return 'Community'
}
}

export default Community
27 changes: 25 additions & 2 deletions lib/masa.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
function masa(provider) {
console.log('masa.js')
import MasaPoint from './masa_point'
import MasaCoin from './masa_coin'

/**
* Masa system client library class.
*/
class Masa {
/**
* @constructor
*/
constructor() {
console.log('masa.js')
const provider = {}
this.point = new class extends MasaPoint {
feedback(to, communityId, feelingsId, msg) {
throw new TypeError('Not support function. Please use MasaTx.')
}
}(provider, null)
this.coin = new class extends MasaCoin {
transfer(address) {
throw new TypeError('Not support function. Please use MasaTx.')
}
}(provider, null)
}
}

const masa = new Masa()
export default masa
33 changes: 33 additions & 0 deletions lib/masa_coin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Transaction from './transaction'

/**
* MasaCoin managing class
*/
class MasaCoin {
constructor(provider, wallet) {
this.provider = provider
this.wallet = wallet
}
/**
* Get the balance of coin.
* @param address to query the balance of.
* @returns {Promise<number>} amount of coin.
*/
async balanceOf(address) {
// TODO: implement
return 100
}

/**
* Send coin to other user.
* @param address receiver address
* @param amount sending amount
* @return {Promise<Transaction>}
*/
async transfer(address, amount) {
// TODO: implement
return new Transaction()
}
}

export default MasaCoin
62 changes: 62 additions & 0 deletions lib/masa_point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Transaction from './transaction'

/**
* MasaPoint managing class.
* This class provide functions for access to MasaPoint Smart Contract on Ethereum.
*/
class MasaPoint {
/**
* @constructor
* @param provider require for explorer functions
* @param wallet require for signed functions
*/
constructor(provider, wallet) {
this.provider = provider
this.wallet = wallet
}
/**
* Get the amount of valid points.
* @param address the owner of masa point
* @returns {Promise<number>} point of the user
*/
async amountOf(address) {
// TODO: implement
if (address === '0x6861766520636f696e20697300000000000000b8') {
return 200
}
return 500
}

/**
* Get the amount of valid points in a specific community.
* @param address the owner of masa point
* @param communityId community id
* @returns {Promise<number>} point of the user in the community
*/
async communityAmountOf(address, communityId) {
// TODO: implement
return 200
}

/**
* Create feedback raw transaction.
* @param to feedback received user address
* @param communityId the community regarding of feedback
* @param feelingsId Gratitude: 1 / Empathy: 2 /Support: 3
* @param msg reason for feelings
* @returns {Promise<Transaction>}
*/
async feedback(to, communityId, feelingsId, msg) {
// TODO: implement
if (this.wallet === undefined) {
throw new TypeError('Wallet is NULL! need for sign.')
}
return new Transaction()
}

get [Symbol.toStringTag]() {
return 'MasaPoint'
}
}

export default MasaPoint
40 changes: 40 additions & 0 deletions lib/masa_tx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Wallet from './wallet'
import MasaPoint from './masa_point'
import MasaCoin from './masa_coin'
import Community from './community'

/**
* MasaTx class provide functions that create signed transaction.
* This class needs wallet for pick up private key.
*
* Usage:
* const masaTx = new MasaTx() // generate new wallet.
* or
* const masaTx = new MasaTx(mnemonic) // restore wallet from specify mnemonic
*
* masaTx.point.feedback(~~~~) // return signed transaction for Masa Point Feedback.
* masaTx.coin.transfer(~~~) // return signed transaction for Masa Coin Transfer.
* masaTx.community.create(~~~) // return signed transaction for Community creation.
*/
class MasaTx {
constructor(mnemonic = '') {
mnemonic = mnemonic === undefined || mnemonic === '' ? Wallet.generateMnemonic() : mnemonic
this.wallet = new Wallet(mnemonic)
this.point = new class extends MasaPoint {
amountOf(address) {
throw new TypeError('Not support function. Please use Masa.')
}
communityAmountOf(address, communityId) {
throw new TypeError('Not support function. Please use Masa.')
}
}(null, this.wallet)
this.coin = new class extends MasaCoin {
async balanceOf() {
throw new TypeError('Not support function. Please use Masa.')
}
}(null, this.wallet)
this.community = new Community(this.wallet)
}
}

export default MasaTx
37 changes: 37 additions & 0 deletions lib/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Class for Provide convenience access to Ethereum Transaction.
*/
class Transaction {
/**
* sign this transaction
* @param privateKey
*/
signature(privateKey) {
// TODO: implement
}

/**
* serialize to json format
* @return {JSON} json formatted transaction
*/
toJSON() {
// TODO: implement
return {
to: '',
nonce: 2,
gas: 0x01,
gasPrice: 20 * 10 ** 9,
data: '0x0000000001',
chainId: 4447,
v: '',
s: '',
r: ''
}
}

get [Symbol.toStringTag]() {
return 'Transaction'
}
}

export default Transaction
48 changes: 48 additions & 0 deletions lib/wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import bip39 from 'bip39'
import hdkey from 'ethereumjs-wallet/hdkey'

/**
* Masa Wallet class.
* Provide simple wallet functions.
* Usage:
* const mnemonic = Wallet.generateMnemonic()
* const wallet = new Wallet(mnemonic)
* const privateKey = wallet.privateKey // return Buffer
* const address = wallet.address // return string
*/
class Wallet {
get HD_PATH() {
return "m/44'/60'/0'/0/0"
}

/**
* generate mnemonic of master seed.
* @return {string} mnemonic
*/
static generateMnemonic() {
return bip39.generateMnemonic()
}

/**
* create HDWallet from master seed mnemonic.
* @constructor
* @param mnemonic {string}
*/
constructor(mnemonic) {
if (!bip39.validateMnemonic(mnemonic)) {
throw new TypeError('invalid mnemonic!')
}
this.mnemonic = mnemonic
this._hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic))
this._wallet = this._hdwallet.derivePath(this.HD_PATH).getWallet()
this.address = '0x' + this._wallet.getAddress().toString('hex')
this.privateKey = this._wallet.getPrivateKey()
this.publicKey = this._wallet.getPublicKey()
}

get [Symbol.toStringTag]() {
return 'Wallet'
}
}

export default Wallet
Loading