Skip to content

Commit

Permalink
feat(GA): enbale GA (#692)
Browse files Browse the repository at this point in the history
* feat(GA): enbale GA
Add ability to use MemoryAccount without keyPair(GA), add another init prop that -> `gaId`
Exm:
```
MemoryAccount({ gaId: 'ak_f23fada...' })
```

* fix(test): Fix GA test

* chore(test): Fix typo in test name

* chore(test): remove duplicated
  • Loading branch information
nduchak committed Oct 1, 2019
1 parent 0dbb2fe commit eded912
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
22 changes: 15 additions & 7 deletions es/account/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { decode } from '../tx/builder/helpers'
const secrets = new WeakMap()

async function sign (data) {
if (this.isGa) throw new Error('You are trying to sign data using GA account without keypair')
return Promise.resolve(Crypto.sign(data, secrets.get(this).secretKey))
}

Expand Down Expand Up @@ -72,15 +73,22 @@ function validateKeyPair (keyPair) {
* @return {Account}
*/
const MemoryAccount = Account.compose({
init ({ keypair }) {
validateKeyPair(keypair)
if (Object.prototype.hasOwnProperty.call(keypair, 'priv') && Object.prototype.hasOwnProperty.call(keypair, 'pub')) {
keypair = { secretKey: keypair.priv, publicKey: keypair.pub }
console.warn('pub/priv naming for accounts has been deprecated, please use secretKey/publicKey')
}
init ({ keypair, gaId }) {
this.isGa = !!gaId
if (gaId) {
if (!Crypto.isAddressValid(gaId)) throw new Error('Invalid GA address')
secrets.set(this, { publicKey: gaId })
} else {
validateKeyPair(keypair)
if (Object.prototype.hasOwnProperty.call(keypair, 'priv') && Object.prototype.hasOwnProperty.call(keypair, 'pub')) {
keypair = { secretKey: keypair.priv, publicKey: keypair.pub }
console.warn('pub/priv naming for accounts has been deprecated, please use secretKey/publicKey')
}

this.setSecret(keypair)
this.setSecret(keypair)
}
},
props: { isGa: false },
methods: { sign, address, setSecret }
})

Expand Down
5 changes: 2 additions & 3 deletions es/ae/aepp.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import Aens from './aens'
import Rpc from '../rpc/client'
import { ContractAPI } from './contract'
import Oracle from './oracle'
// Todo Enable GA
// import GeneralizeAccount from '../contract/ga'
import GeneralizeAccount from '../contract/ga'

/**
* Aepp Stamp
Expand All @@ -42,6 +41,6 @@ import Oracle from './oracle'
* @param {Object} [options={}] - Initializer object
* @return {Object} Aepp instance
*/
const Aepp = Ae.compose(ContractAPI, Aens, Oracle, Rpc)
const Aepp = Ae.compose(ContractAPI, Aens, Oracle, GeneralizeAccount, Rpc)

export default Aepp
17 changes: 7 additions & 10 deletions es/ae/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ import { isNameValid } from '../tx/builder/helpers'
*/
async function send (tx, options = {}) {
const opt = R.merge(this.Ae.defaults, options)
// Todo Enable GA
// const { contractId: gaId, authFun } = await this.getAccount(await this.address(opt))
// const signed = gaId
// ? await this.signUsingGA(tx, { ...opt, authFun })
const signed = await this.signTransaction(tx, opt)
const { contractId: gaId, authFun } = await this.getAccount(await this.address(opt))
const signed = gaId
? await this.signUsingGA(tx, { ...opt, authFun })
: await this.signTransaction(tx, opt)
return this.sendTransaction(signed, opt)
}

// Todo Enable GA
// eslint-disable-next-line no-unused-vars
async function signUsingGA (tx, options = {}) {
const { authData, authFun } = options
Expand Down Expand Up @@ -157,10 +155,9 @@ function destroyInstance () {
* @return {Object} Ae instance
*/
const Ae = stampit(Tx, Account, Chain, {
methods: { send, spend, transferFunds, destroyInstance, resolveRecipientName },
deepProps: { Ae: { defaults: {} } }
// Todo Enable GA
// deepConfiguration: { Ae: { methods: ['signUsingGA'] } }
methods: { send, spend, transferFunds, destroyInstance, resolveRecipientName, signUsingGA },
deepProps: { Ae: { defaults: {} } },
deepConfiguration: { Ae: { methods: ['signUsingGA'] } }
})

export default Ae
5 changes: 2 additions & 3 deletions es/ae/universal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import Chain from '../chain/node'
import Aens from './aens'
import Transaction from '../tx/tx'
import Oracle from './oracle'
// Todo Enable GA
// import GeneralizeAccount from '../contract/ga'
import GeneralizeAccount from '../contract/ga'
import Accounts from '../accounts'
import Contract from './contract'
import NodePool from '../node-pool'
Expand All @@ -44,7 +43,7 @@ import NodePool from '../node-pool'
* @param {Object} [options={}] - Initializer object
* @return {Object} Universal instance
*/
export const Universal = Ae.compose(Accounts, Chain, NodePool, Transaction, Aens, Contract, Oracle, {
export const Universal = Ae.compose(Accounts, Chain, NodePool, Transaction, Aens, Contract, Oracle, GeneralizeAccount, {
init () {},
props: { process: {} }
})
Expand Down
5 changes: 2 additions & 3 deletions es/ae/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import * as R from 'ramda'
import Tx from '../tx/tx'
import Contract from './contract'
import NodePool from '../node-pool'
// Todo Enable GA
// import GeneralizeAccount from '../contract/ga'
import GeneralizeAccount from '../contract/ga'

const contains = R.flip(R.contains)
const isTxMethod = contains(Tx.compose.deepConfiguration.Ae.methods)
Expand Down Expand Up @@ -132,7 +131,7 @@ async function rpcAddress ({ params, session }) {
onContract: confirm
})
*/
const Wallet = Ae.compose(Accounts, Chain, NodePool, Tx, Contract, Rpc, {
const Wallet = Ae.compose(Accounts, Chain, NodePool, Tx, Contract, GeneralizeAccount, Rpc, {
init ({ onTx = this.onTx, onChain = this.onChain, onAccount = this.onAccount, onContract = this.onContract }, { stamp }) {
this.onTx = onTx
this.onChain = onChain
Expand Down
6 changes: 2 additions & 4 deletions es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import Accounts from './accounts'
import MemoryAccount from './account/memory'
import Aens from './ae/aens'
import Contract from './ae/contract'
// Todo Enable GA
// import GeneralizeAccount from './contract/ga'
import GeneralizeAccount from './contract/ga'
import ContractCompilerAPI from './contract/compiler'
import Wallet from './ae/wallet'
import Aepp from './ae/aepp'
Expand All @@ -60,8 +59,7 @@ export {
Channel,
Crypto,
Chain,
// Todo Enable GA
// GeneralizeAccount,
GeneralizeAccount,
HdWallet,
MemoryAccount,
Node,
Expand Down
3 changes: 1 addition & 2 deletions es/tx/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ const Transaction = ChainNode.compose(Tx, {
channelSlashTx,
channelSettleTx,
channelSnapshotSoloTx,
// Todo Enable GA
// gaAttachTx,
gaAttachTx,
getAccountNonce,
getVmVersion
}
Expand Down
9 changes: 5 additions & 4 deletions test/integration/ga.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const authContract = `contract BlindAuth =
None => abort("Not in Auth context")
Some(tx_hash) => true
`
// Todo Enable GA
describe.skip('Generalize Account', function () {
describe('Generalize Account', function () {
configure(this)

let client
Expand All @@ -54,15 +53,17 @@ describe.skip('Generalize Account', function () {
e.message.should.be.equal(`Account ${gaAccount.publicKey} is already GA`)
}
})
it('Spend Using Meta Tx', async () => {
it('Init MemoryAccount for GA and Spend using GA', async () => {
const r = Math.floor(Math.random() * 20)
const r2 = Math.floor(Math.random() * 20)
const callData = await client.contractEncodeCall(authContract, 'authorize', [`${r}`])

const { publicKey } = generateKeyPair()
client.removeAccount(gaAccount.publicKey)
await client.addAccount(MemoryAccount({ gaId: gaAccount.publicKey }))
await client.spend(10000, publicKey, { authData: { callData }, onAccount: gaAccount.publicKey })
await client.spend(10000, publicKey, { authData: { source: authContract, args: [`${r2}`] }, onAccount: gaAccount.publicKey })
const balanceAfter = await client.balance(publicKey)
balanceAfter.should.be.equal(`20000`)
balanceAfter.should.be.equal('20000')
})
})

0 comments on commit eded912

Please sign in to comment.