diff --git a/es/account/memory.js b/es/account/memory.js index 894f54a20e..3873fe3fdd 100644 --- a/es/account/memory.js +++ b/es/account/memory.js @@ -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)) } @@ -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 } }) diff --git a/es/ae/aepp.js b/es/ae/aepp.js index 6adac71790..557b4725ef 100644 --- a/es/ae/aepp.js +++ b/es/ae/aepp.js @@ -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 @@ -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 diff --git a/es/ae/index.js b/es/ae/index.js index 3b12da9dba..179a17f88c 100644 --- a/es/ae/index.js +++ b/es/ae/index.js @@ -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 @@ -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 diff --git a/es/ae/universal.js b/es/ae/universal.js index a18ba042b9..c925f7189a 100644 --- a/es/ae/universal.js +++ b/es/ae/universal.js @@ -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' @@ -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: {} } }) diff --git a/es/ae/wallet.js b/es/ae/wallet.js index c0e08742a0..03644ad3b3 100644 --- a/es/ae/wallet.js +++ b/es/ae/wallet.js @@ -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) @@ -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 diff --git a/es/index.js b/es/index.js index db1135fc9c..1f953aa53c 100644 --- a/es/index.js +++ b/es/index.js @@ -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' @@ -60,8 +59,7 @@ export { Channel, Crypto, Chain, - // Todo Enable GA - // GeneralizeAccount, + GeneralizeAccount, HdWallet, MemoryAccount, Node, diff --git a/es/tx/tx.js b/es/tx/tx.js index 6a8fea314b..37cb1dcf8c 100644 --- a/es/tx/tx.js +++ b/es/tx/tx.js @@ -474,8 +474,7 @@ const Transaction = ChainNode.compose(Tx, { channelSlashTx, channelSettleTx, channelSnapshotSoloTx, - // Todo Enable GA - // gaAttachTx, + gaAttachTx, getAccountNonce, getVmVersion } diff --git a/test/integration/ga.js b/test/integration/ga.js index 8dbc6d8026..a741084ece 100644 --- a/test/integration/ga.js +++ b/test/integration/ga.js @@ -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 @@ -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') }) })