From e5922fae8219d1a604999b673aa48e54ef9a726f Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Wed, 18 Mar 2020 08:02:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=8A=A8=E6=80=81require?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Action.js | 31 +++++++++++++++++-------------- indexActionTypes.js | 4 ++++ 2 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 indexActionTypes.js diff --git a/Action.js b/Action.js index ad8df60..5ddcec5 100644 --- a/Action.js +++ b/Action.js @@ -1,5 +1,6 @@ const Ling = require('so.ling') const ticCrypto = require('tic.crypto') +const my = {} // 私有数据 /** ****************** Public of instance ********************/ @@ -34,12 +35,12 @@ MOM._model = { json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构 } -MOM.packMe = async function (keypair) { // 由前端调用,后台不创建 - this.actorPubkey = keypair.pubkey - this.actorAddress = ticCrypto.pubkey2address(keypair.pubkey) +MOM.packMe = async function ({seckey, pubkey}={}) { // 由前端调用,后台不创建 + this.actorPubkey = pubkey + this.actorAddress = ticCrypto.pubkey2address(pubkey) this.timestamp = new Date() - await this.signMe(keypair.seckey) + await this.signMe(seckey) this.hashMe() return this } @@ -119,17 +120,20 @@ DAD.execute = async function (action) { // For chain server. // } DAD.getActionType = (type)=>{ - return require(`./${type}`) + if (!my.ActionTypeDict) { + my.ActionTypeDict = require('./indexActionTypes.js') // 避免动态require + } + return my.ActionTypeDict[type] } DAD.createTypedAction = function(action){ return new (DAD.getActionType(action.type))(action) } -DAD.buildUserAction = async function (action, keypair) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。 - if (action && action.type && keypair && keypair.seckey && keypair.pubkey && ticCrypto.seckey2pubkey(keypair.seckey)===keypair.pubkey) { +DAD.buildUserAction = async function (action, {seckey, pubkey}={}) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。 + if (action && action.type && seckey && pubkey && ticCrypto.seckey2pubkey(seckey)===pubkey) { let typedAction = new (DAD.getActionType(action.type))(action) - typedAction.actorPubkey = keypair.pubkey + typedAction.actorPubkey = pubkey if (typedAction.validateMe()) { - await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。 + await typedAction.packMe({seckey, pubkey}) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。 return typedAction } } @@ -160,15 +164,14 @@ DAD.api = {} DAD.api.getAction = async function (option) { return await DAD.getOne(option) } - DAD.api.getActionList = async function (option) { return await DAD.getAll(option) } - -DAD.api.getMyActionList = async function ({myAddress, config}={}) { +DAD.api.getMyActionList = async function ({Action={}, config}={}) { let list = {} - list.fromMe = await DAD.getAll({Action:{actorAddress:myAddress}, config:config}) - list.toMe = await DAD.getAll({Action:{toAddress: myAddress}, config:config}) + let {myAddress, ...action} = Action + list.fromMe = await DAD.getAll({Action:{actorAddress:myAddress, ...action}, config}) + list.toMe = await DAD.getAll({Action:{toAddress:myAddress, ...action}, config}) return list } diff --git a/indexActionTypes.js b/indexActionTypes.js new file mode 100644 index 0000000..4857619 --- /dev/null +++ b/indexActionTypes.js @@ -0,0 +1,4 @@ +module.exports = { + ActionTransfer: require('./ActionTransfer.js'), + ActionStore: require('./ActionStore.js') +} \ No newline at end of file