Compare commits

..

6 Commits
main ... kiss

9 changed files with 143 additions and 447 deletions

118
.gitignore vendored
View File

@ -1,113 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # 以'#'开始的行,被视为注释.
# how to include another gitignore? node_modules
# https://stackoverflow.com/questions/7005142/can-i-include-other-gitignore-file-in-a-gitignore-file-like-include-in-c-li
# https://github.com/github/gitignore
# https://github.com/SlideWave/gitignore-include?tab=readme-ov-file#examples
# https://gitignore.io
### .gitignore.global.txt ###
# Self defined pattern to ignore
?*.gitignore
?*.gitignore/
?*.gitignore.*
?*.gitignore.*/
*.gitomit
*.gitomit.*
*.gitomit/
*.gitomit.*/
*.nogit
*.nogit.*
*.nogit/
*.nogit.*/
# 保留
!.gitignore
!.gitignore.*
!.gitkeep
# 通用
.svn/
.deploy_git/
.idea/
.sass-cache/
.wrangler
/test/unit/coverage/
/test/e2e/reports/
node_modules/
*.aab
*.apk
*.ipa
*.min.js
*.min.css
*.min.html
*.iml
*.njsproj
*.ntvs*
*.sw*
*.sln
*.suo
.gitattributes
.umi
.umi-production
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock
selenium-debug.log
Thumbs.db
thumbs.db
_desktop.ini
# vue-cli 项目
/dist/
# 来自 vue-cli 创建项目的 .gitignore
.project
# hexo
/public/
# Hardhat
/artifacts/
/cache/
# seafile 临时文件
._*
.$*
# office 暂存文件
~$*
# 用户shell配置脚本
.bashrc_custom
# 苹果系统临时文件
.DS_Store
# 安卓缓存文件夹
.thumbnails
# local env files
.env.local
.env.*.local
# hexo
/db.json
# wo
# 服务端
/_archive/*
/_datastore/*
/_filestore/*
/_logstore/*
/_webroot/*
/_ssl/*
# uniapp 客户端
/unpackage/*
!/unpackage/res/
package-lock.json package-lock.json
pages4loader.json5 .vscode
.svn
### .gitignore.local.txt ### ~*
.gitattributes

152
Action.js
View File

@ -1,13 +1,14 @@
const Ling = require('so.ling') const Ling = require('so.ling')
const ticc = require('tic-crypto') const ticCrypto = require('tic.crypto')
const my = {} // 私有数据
/** ****************** Public of instance ********************/ /** ****************** Public of instance ********************/
const DAD = (module.exports = function Action (prop) { const DAD = module.exports = function Action (prop) {
this._class = this.constructor.name this._class = this.constructor.name
this.setProp(prop) this.setProp(prop)
this.type = this.constructor.name this.type = this.constructor.name
}) }
DAD.__proto__ = Ling DAD.__proto__ = Ling
const MOM = DAD.prototype const MOM = DAD.prototype
@ -17,11 +18,7 @@ MOM.__proto__ = Ling.prototype
MOM._table = DAD.name MOM._table = DAD.name
MOM._tablekey = 'hash' MOM._tablekey = 'hash'
MOM._model = { MOM._model = {
hash: { hash: { default: undefined, sqlite: 'TEXT UNIQUE', mysql: 'VARCHAR(64) PRIMARY KEY' }, // 不纳入签名和哈希
default: undefined,
sqlite: 'TEXT UNIQUE',
mysql: 'VARCHAR(64) PRIMARY KEY'
}, // 不纳入签名和哈希
version: { default: 0, sqlite: 'INTEGER' }, version: { default: 0, sqlite: 'INTEGER' },
type: { default: 'Action', sqlite: 'TEXT', mysql: 'VARCHAR(100)' }, // 是否放在 assets里更好这里该放action自己的version type: { default: 'Action', sqlite: 'TEXT', mysql: 'VARCHAR(100)' }, // 是否放在 assets里更好这里该放action自己的version
blockHash: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(64)' }, // 不纳入签名和哈希。只为了方便查找 blockHash: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(64)' }, // 不纳入签名和哈希。只为了方便查找
@ -38,132 +35,105 @@ MOM._model = {
json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构 json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构
} }
MOM.packMe = async function (keypair) { MOM.packMe = async function ({seckey, pubkey}={}) { // 由前端调用,后台不创建
// 由前端调用,后台不创建 this.actorPubkey = pubkey
this.actorPubkey = keypair.pubkey this.actorAddress = ticCrypto.pubkey2address(pubkey)
this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date() this.timestamp = new Date()
await this.signMe(keypair.prikey) await this.signMe(seckey)
this.hashMe() this.hashMe()
return this return this
} }
MOM.signMe = async function (prikey) { MOM.signMe = async function (seckey) { // 由前端调用,后台不该进行签名
// 由前端调用,后台不该进行签名
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await ticc.sign_easy({ data: json, prikey }) this.actorSignature = await ticCrypto.sign(json, seckey)
return this return this
} }
MOM.hashMe = function () { MOM.hashMe = function () {
this.hash = ticc.hash_easy(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了 this.hash = ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
return this return this
} }
MOM.verifySig = async function () { MOM.verifySig = async function() {
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] })
let result = await ticc.verify_easy({ let result = await ticCrypto.verify(json, this.actorSignature, this.actorPubkey)
data: json,
signature: this.actorSignature,
pubkey: this.actorPubkey
})
return result return result
} }
DAD.verifySig = async function (actionData) { DAD.verifySig = async function (actionData) {
let typedAction = new wo[actionData.type](actionData) let typedAction = new (DAD.getActionType(actionData.type))(actionData)
return await typedAction.verifySig() return await typedAction.verifySig()
} }
MOM.verifyAddress = function () { MOM.verifyAddress = function () {
return ( return this.actorAddress === ticCrypto.pubkey2address(this.actorPubkey)
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
} }
DAD.verifyAddress = function (actionData) { DAD.verifyAddress = function (actionData) {
let typedAction = new wo[actionData.type](actionData) let typedAction = new (DAD.getActionType(actionData.type))(actionData)
return typedAction.verifyAddress() return typedAction.verifyAddress()
} }
MOM.verifyHash = function () { MOM.verifyHash = function () {
return ( return this.hash === ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
this.hash ===
ticc.hash_easy(this.getJson({ exclude: ['hash', 'blockHash'] }))
)
} }
DAD.verifyHash = function (actionData) { DAD.verifyHash = function (actionData) {
let typedAction = new wo[actionData.type](actionData) let typedAction = new (DAD.getActionType(actionData.type))(actionData)
return typedAction.verifyHash() return typedAction.verifyHash()
} }
MOM.validateMe = function () { MOM.validateMe = function() { // Applicable on both client and chain server. 子类应当覆盖本方法,静态的检查事务内容的格式。不能检查 balance 等需要全链数据库的东西,因为本方法也要用在前端检查。
// Applicable on both client and chain server. 子类应当覆盖本方法,静态的检查事务内容的格式。不能检查 balance 等需要全链数据库的东西,因为本方法也要用在前端检查。
// to implement in subclasses: 检查子类事务内容的格式 // to implement in subclasses: 检查子类事务内容的格式
let typedAction = new wo[this.type](this) let typedAction = new (DAD.getActionType(this.type))(this)
return typedAction.validateMe() return typedAction.validateMe()
} }
DAD.validate = function (action) { DAD.validate = function (action) { // Allicable on both client and chain server.
// Allicable on both client and chain server.
mylog.info(`Validating action type=${action.type} of hash=${action.hash}`) mylog.info(`Validating action type=${action.type} of hash=${action.hash}`)
let typedAction = new wo[action.type](action) let typedAction = new (DAD.getActionType(action.type))(action)
return typedAction.validateMe() return typedAction.validateMe()
} }
MOM.executableMe = async function () { MOM.executableMe = async function() { // Applicable on chain server. 子类应当覆盖本方法动态的检查事务内容在当前链状态下是否能执行。To check if an action is executableMe given the current chain status.
// Applicable on chain server. 子类应当覆盖本方法动态的检查事务内容在当前链状态下是否能执行。To check if an action is executableMe given the current chain status. let typedAction = new (DAD.getActionType(this.type))(this)
let typedAction = new wo[this.type](this)
return await typedAction.executableMe() return await typedAction.executableMe()
} }
DAD.executable = async function (action) { DAD.executable = async function(action) { // For chain server.
// For chain server. let typedAction = new (DAD.getActionType(action.type))(action)
let typedAction = new wo[action.type](action) if (typedAction.hasOwnProperty('executableMe')) { // 防止子类忘了定义自己的 executableMe
if (typedAction.hasOwnProperty('executableMe')) {
// 防止子类忘了定义自己的 executableMe
return await typedAction.executableMe() return await typedAction.executableMe()
} else { }else {
return true return true
} }
} }
MOM.executeMe = async function () { MOM.executeMe = async function() { // For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
// For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
// to implement in subclasses: 把action的影响汇总登记到其他表格用于辅助的、索引的表格方便快速索引、处理。每种事务类型都要重定义这个方法。 // to implement in subclasses: 把action的影响汇总登记到其他表格用于辅助的、索引的表格方便快速索引、处理。每种事务类型都要重定义这个方法。
let typedAction = new wo[this.type](this) let typedAction = new (DAD.getActionType(this.type))(this)
return await typedAction.executeMe() return await typedAction.executeMe()
} }
DAD.execute = async function (action) { DAD.execute = async function (action) { // For chain server.
// For chain server.
mylog.info(`Excecuting action type=${action.type} of hash=${action.hash}`) mylog.info(`Excecuting action type=${action.type} of hash=${action.hash}`)
let typedAction = new wo[action.type](action) let typedAction = new (DAD.getActionType(action.type))(action)
return await typedAction.executeMe() return await typedAction.executeMe()
} }
// [todo 20190411] 执行事务池中的所有事务 // [todo 20190411] 执行事务池中的所有事务
// DAD.executePool = async function() { // DAD.executePool = async function() {
// } // }
DAD._initTypeDict = function (typedActionDict) { DAD.getActionType = (type)=>{
Object.assign(wo, typedActionDict) if (!my.ActionTypeDict) {
my.ActionTypeDict = require('./indexActionTypes.js') // 避免动态require
}
return my.ActionTypeDict[type]
} }
DAD.getTypedAction = function (type) { DAD.createTypedAction = function(action){
return wo[type] return new (DAD.getActionType(action.type))(action)
} }
DAD.createTypedAction = function (action) { DAD.buildUserAction = async function (action, {seckey, pubkey}={}) { // Applicable on client. 客户端调用 Action.build即可新建、并打包成一个完整的子事务不需要亲自调用 constructor, packMe 等方法。
return new wo[action.type](action) if (action && action.type && seckey && pubkey && ticCrypto.seckey2pubkey(seckey)===pubkey) {
} let typedAction = new (DAD.getActionType(action.type))(action)
typedAction.actorPubkey = pubkey
DAD.buildUserAction = async function (action, keypair) {
// Applicable on client. 客户端调用 Action.build即可新建、并打包成一个完整的子事务不需要亲自调用 constructor, packMe 等方法。
if (
action &&
action.type &&
keypair &&
keypair.prikey &&
keypair.pubkey &&
ticc.prikey_to_pubkey({ prikey: keypair.prikey }) === keypair.pubkey
) {
let typedAction = new wo[action.type](action)
typedAction.actorPubkey = keypair.pubkey
if (typedAction.validateMe()) { if (typedAction.validateMe()) {
await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。 await typedAction.packMe({seckey, pubkey}) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
return typedAction return typedAction
} }
} }
@ -194,10 +164,16 @@ DAD.api = {}
DAD.api.getAction = async function (option) { DAD.api.getAction = async function (option) {
return await DAD.getOne(option) return await DAD.getOne(option)
} }
DAD.api.getActionList = async function (option) { DAD.api.getActionList = async function (option) {
return await DAD.getAll(option) return await DAD.getAll(option)
} }
DAD.api.getMyActionList = async function ({Action={}, config}={}) {
let list = {}
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
}
DAD.api.prepare = async function (option) { DAD.api.prepare = async function (option) {
if (typeof option === 'string') { if (typeof option === 'string') {
@ -206,26 +182,18 @@ DAD.api.prepare = async function (option) {
} catch (error) {} } catch (error) {}
} }
// 前端发来action数据进行格式检查不检查是否可执行--这和事务类型、执行顺序有关)后放入缓冲池。 // 前端发来action数据进行格式检查不检查是否可执行--这和事务类型、执行顺序有关)后放入缓冲池。
if ( if (option && option.Action && option.Action.type && DAD.getActionType(option.Action.type) && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
option && let typedAction = new (DAD.getActionType(option.Action.type))(option.Action)
option.Action && if (typedAction.verifyAddress() && // 只检查所有事务通用的格式
option.Action.type && await typedAction.verifySig() &&
wo[option.Action.type] && typedAction.verifyHash() &&
option.Action.hash && typedAction.validateMe() && // 检查事务的内容是否符合该子类事务的格式
!DAD.actionPool[option.Action.hash] (await typedAction.executableMe()) // 检查事务是否可执行,在当前链的状态下。
) {
let typedAction = new wo[option.Action.type](option.Action)
if (
typedAction.verifyAddress() && // 只检查所有事务通用的格式
(await typedAction.verifySig()) &&
typedAction.verifyHash() &&
typedAction.validateMe() && // 检查事务的内容是否符合该子类事务的格式
(await typedAction.executableMe()) // 检查事务是否可执行,在当前链的状态下。
) { ) {
DAD.actionPool[option.Action.hash] = typedAction DAD.actionPool[option.Action.hash] = typedAction
DAD.actionPoolInfo.totalAmount += option.Action.amount || 0 DAD.actionPoolInfo.totalAmount += option.Action.amount || 0
DAD.actionPoolInfo.totalFee += option.Action.fee || 0 DAD.actionPoolInfo.totalFee += option.Action.fee || 0
// wo.Netnode.broadcast({ Action: option.Action }) // 即使对 master 分支的node.server 也报错Cannot read property 'broadcast' of undefined // wo.Netnode.broadcast({ Action: option.Action }) // 即使对 master 分支的node.server 也报错Cannot read property 'broadcast' of undefined
return option.Action return option.Action
} }
} }

View File

@ -1,75 +1,55 @@
const Action = require('./Action.js') const Action = require('./Action.js')
const ticc = require('tic-crypto')
/** ****************** Public of instance ********************/ /** ****************** Public of instance ********************/
const DAD = (module.exports = function ActionMultisig (prop) { const DAD = module.exports = function ActionMultisig (prop) {
this._class = this.constructor.name this._class = this.constructor.name
this.setProp(prop) // 没有定义 DAD.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop) this.setProp(prop) // 没有定义 DAD.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = this.constructor.name this.type = this.constructor.name
}) }
DAD.__proto__ = Action DAD.__proto__ = Action
const MOM = DAD.prototype const MOM = DAD.prototype
MOM.__proto__ = Action.prototype MOM.__proto__ = Action.prototype
// MOM._table=DAD.name // 注释掉从而继承父类Action的数据库表格名 // MOM._table=DAD.name // 注释掉从而继承父类Action的数据库表格名
MOM.signMe = async function (prikey) { MOM.signMe = async function (seckey) { // 由前端调用,后台不该进行签名
// 由前端调用,后台不该进行签名 let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
let json = this.getJson({ this.actorSignature = await wo.Crypto.sign(json, seckey)
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
}) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await ticc.sign_easy({ data: json, prikey })
return this return this
} }
MOM.verifySig = async function () { MOM.verifySig = async function () {
let json = this.getJson({ let json = this.getJson(({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] }))
exclude: ['hash', 'blockHash', 'actorSignature', 'json'] let res = await wo.Crypto.verify(json, this.actorSignature, this.actorPubkey)
})
let res = await ticc.verify_easy({
data: json,
signature: this.actorSignature,
pubkey: this.actorPubkey
})
return res return res
} }
MOM.verifyAddress = function () { MOM.verifyAddress = function () {
return ( return this.actorAddress === wo.Crypto.pubkey2address(this.actorPubkey)
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
} }
MOM.hashMe = function () { MOM.hashMe = function () {
this.hash = ticc.hash_easy( this.hash = wo.Crypto.hash(this.getJson({ exclude: ['hash', 'blockHash', 'json'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
this.getJson({ exclude: ['hash', 'blockHash', 'json'] })
) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
return this return this
} }
MOM.verifyHash = function () { MOM.verifyHash = function () {
return ( return this.hash === wo.Crypto.hash(this.getJson({ exclude: ['hash', 'blockHash', 'json'] }))
this.hash ===
ticc.hash_easy(this.getJson({ exclude: ['hash', 'blockHash', 'json'] }))
)
} }
MOM.packMe = async function (keypair) { MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
// 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey this.actorPubkey = keypair.pubkey
this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey }) this.actorAddress = wo.Crypto.pubkey2address(keypair.pubkey)
this.timestamp = new Date() this.timestamp = new Date()
await this.signMe(keypair.prikey) await this.signMe(keypair.seckey)
this.hashMe() this.hashMe()
return this return this
} }
MOM.checkMultiSig = async function (account) { MOM.checkMultiSig = async function (account) {
let json = this.getJson({ let json = this.getJson(({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] }))
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
})
let sigers = Object.keys(this.json) // 公钥列表 let sigers = Object.keys(this.json) // 公钥列表
// 交易发起人的签名在prepare的verifySig里已经检查过合法性 // 交易发起人的签名在prepare的verifySig里已经检查过合法性
if (account.multiSignatures.keysgroup.indexOf(this.actorPubkey) === -1) { if (account.multiSignatures.keysgroup.indexOf(this.actorPubkey) === -1) {
@ -77,16 +57,9 @@ MOM.checkMultiSig = async function (account) {
} else { } else {
let M = 0 // 如果发起人已经在keysgroup里则从0算起 let M = 0 // 如果发起人已经在keysgroup里则从0算起
} }
for (let i of sigers) { for (let i of sigers) // 该交易内已签名的每一个公钥
// 该交易内已签名的每一个公钥 {
if ( if (account.multiSignatures.keysgroup.indexOf(i) !== -1 && await wo.Crypto.verify(json, this.json[i], i)) {
account.multiSignatures.keysgroup.indexOf(i) !== -1 &&
(await ticc.verify_easy({
data: json,
signature: this.json[i],
pubkey: i
}))
) {
M++ M++
} }
} }
@ -167,20 +140,16 @@ step3:发起人申请执行
*/ */
MOM.validateMe = async function () { MOM.validateMe = async function () {
return ( return wo.Crypto.isAddress(this.toAddress) &&
ticc.which_chain_address({ address: this.toAddress }) &&
this.fee >= wo.Config.MIN_FEE_ActionTransfer && this.fee >= wo.Config.MIN_FEE_ActionTransfer &&
this.toAddress != this.actorAddress this.toAddress != this.actorAddress
)
} }
MOM.executableMe = async function () { MOM.executableMe = async function () {
if (this.json.act === 'createTransfer') { if (this.json.act === 'createTransfer') { // 创建挂起的多重签名事务
// 创建挂起的多重签名事务
DAD.pendingPool[this.hash] = this DAD.pendingPool[this.hash] = this
return false return false
} else if (this.json.act === 'addSig') { } else if (this.json.act === 'addSig') { // 签名者签名
// 签名者签名
DAD.pendingPool[this.hash].json[this.actorPubkey] = this.json.signature DAD.pendingPool[this.hash].json[this.actorPubkey] = this.json.signature
return false return false
} }
@ -191,48 +160,27 @@ MOM.executeMe = async function () {
switch (this.json.act) { switch (this.json.act) {
// 多重签名账户注册 // 多重签名账户注册
case 'sign': { case 'sign': {
let actor = await wo.Account.getOne({ let actor = await wo.Account.getOne({ Account: { address: this.actorAddress } })
Account: { address: this.actorAddress }
})
if (actor && actor.type !== 'multisig') { if (actor && actor.type !== 'multisig') {
// 检查账户类型,只有不是多重签名账户的才可以执行 // 检查账户类型,只有不是多重签名账户的才可以执行
// todo:类型检查,安全操作 // todo:类型检查,安全操作
await actor.setMe({ await actor.setMe({ Account: { multiSignatures: {
Account: { min: this.json.min,
multiSignatures: { ttl: this.json.ttl, // 该账户交易的最大挂起时间
min: this.json.min, keysgroup: this.json.keysgroup
ttl: this.json.ttl, // 该账户交易的最大挂起时间 } },
keysgroup: this.json.keysgroup cond: { address: actor.address } })
}
},
cond: { address: actor.address }
})
} }
return this return this
} }
// 多重签名账户执行转账 // 多重签名账户执行转账
case 'emitTransfer': { case 'emitTransfer': {
let sender = await wo.Account.getOne({ let sender = await wo.Account.getOne({ Account: { address: this.actorAddress } })
Account: { address: this.actorAddress } if (sender && await this.checkMultiSig(sender) && this.toAddress != this.actorAddress && sender.balance >= this.amount + this.fee) {
}) await sender.setMe({ Account: { balance: sender.balance - this.amount - this.fee }, cond: { address: sender.address } })
if ( let getter = await wo.Account.getOne({ Account: { address: this.toAddress } })
sender &&
(await this.checkMultiSig(sender)) &&
this.toAddress != this.actorAddress &&
sender.balance >= this.amount + this.fee
) {
await sender.setMe({
Account: { balance: sender.balance - this.amount - this.fee },
cond: { address: sender.address }
})
let getter = await wo.Account.getOne({
Account: { address: this.toAddress }
})
if (getter) { if (getter) {
await getter.setMe({ await getter.setMe({ Account: { balance: getter.balance + this.amount }, cond: { address: getter.address } })
Account: { balance: getter.balance + this.amount },
cond: { address: getter.address }
})
} else { } else {
await wo.Account.addOne({ Account: { address: this.toAddress } }) await wo.Account.addOne({ Account: { address: this.toAddress } })
} }

View File

@ -1,30 +1,15 @@
const Action = require('./Action.js') const Action = require('./Action.js')
const ticc = require('tic-crypto')
const Methods = ['create', 'transfer', 'exchange', 'mount'] const Methods = ['create', 'transfer', 'exchange', 'mount']
async function actValidator (action) { async function actValidator (action) {
switch (action.data.method) { switch (action.data.method) {
case 'create': case 'create':
if ( if (action.data.name && action.data.symbol && action.data.decimals &&
action.data.name && Number.isSafeInteger(Number(action.data.decimals)) &&
action.data.symbol && !await wo.Tac.getOne({ Tac: { name: action.data.name, symbol: action.data.symbol } })
action.data.decimals && ) { return true }
Number.isSafeInteger(Number(action.data.decimals)) &&
!(await wo.Tac.getOne({
Tac: { name: action.data.name, symbol: action.data.symbol }
}))
) {
return true
}
return false return false
case 'transfer': case 'transfer':
return ( return action.amount && action.amount > 0 && action.actorAddress && action.toAddress && action.actorAddress !== action.toAddress
action.amount &&
action.amount > 0 &&
action.actorAddress &&
action.toAddress &&
action.actorAddress !== action.toAddress
)
case 'exchange': case 'exchange':
return true return true
case 'mount': case 'mount':
@ -42,52 +27,38 @@ class ActionTac extends Action {
enumerable: true, enumerable: true,
writable: false writable: false
}), }),
Object.defineProperty(this, 'type', { Object.defineProperty(this, 'type', {
value: 'ActionTac', value: 'ActionTac',
enumerable: true, enumerable: true,
writable: false writable: false
}), }),
Object.defineProperty(this, 'data', { Object.defineProperty(this, 'data', {
value: prop, value: prop,
enumerable: true, enumerable: true,
writable: false writable: false
}) })
} }
static async validate (action) { static async validate (action) { // todo 20190409: MOM.validateMe
// todo 20190409: MOM.validateMe return Methods.includes(action.data.method) && await actValidator(action)
return Methods.includes(action.data.method) && (await actValidator(action))
} }
static async execute (action) { static async execute (action) { // todo 20190409: MOM.executeMe
// todo 20190409: MOM.executeMe
if (action && action.data.method) { if (action && action.data.method) {
switch (action.data.method) { switch (action.data.method) {
case 'create': case 'create':
delete action._class delete action._class
let tac = new wo.Tac( let tac = new wo.Tac(
Object.assign( Object.assign(action.data,
action.data,
action.actorAddress, action.actorAddress,
action.actorPubkey, action.actorPubkey,
action.actorSignature action.actorSignature
) ))
) tac.address = wo.Crypto.pubkey2address(wo.Crypto.hash(action.actorSignature, action.hash))
tac.address = ticc.pubkey_to_address({
pubkey: ticc.hash_easy(action.actorSignature, action.hash)
})
return await tac.addMe() return await tac.addMe()
case 'transfer': case 'transfer':
// 内部交易,转发到应用链进程来处理 // 内部交易,转发到应用链进程来处理
await wo.Store.decrease( await wo.Store.decrease(action.actorAddress, 0 - action.amount, action.address)
action.actorAddress, await wo.Store.increase(action.toAddress, action.amount, action.address)
0 - action.amount,
action.address
)
await wo.Store.increase(
action.toAddress,
action.amount,
action.address
)
return true return true
case 'exchange': case 'exchange':
// Bancor类型 // Bancor类型

View File

@ -1,11 +1,11 @@
const Action = require('./Action.js') const Action = require('./Action.js')
const ticc = require('tic-crypto') const ticCrypto = require('tic.crypto')
const DAD = (module.exports = function ActionTransfer (prop) { const DAD = module.exports = function ActionTransfer (prop) {
this._class = this.constructor.name this._class = this.constructor.name
this.setProp(prop) // 没有定义 ActionTransfer.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop) this.setProp(prop) // 没有定义 ActionTransfer.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = this.constructor.name this.type = this.constructor.name
}) }
DAD.__proto__ = Action DAD.__proto__ = Action
const MOM = DAD.prototype const MOM = DAD.prototype
@ -13,28 +13,25 @@ MOM.__proto__ = Action.prototype
MOM.validateMe = function () { MOM.validateMe = function () {
// if (sender && sender.type !== 'multisig' && action.toAddress != action.actorAddress && sender.balance >= action.amount + action.fee){ // if (sender && sender.type !== 'multisig' && action.toAddress != action.actorAddress && sender.balance >= action.amount + action.fee){
return ( return this.actorPubkey && this.toAddress && ticCrypto.pubkey2address(this.actorPubkey)!== this.toAddress // 不能转帐给自己。
this.actorPubkey && && this.amount && this.amount > 0 && (this.fee >= 0)
this.toAddress &&
ticc.pubkey_to_address({ pubkey: this.actorPubkey }) !== this.toAddress && // 不能转帐给自己。
this.amount &&
this.amount > 0 &&
this.fee >= 0
)
} }
MOM.executableMe = async function () { MOM.executableMe = async function() {
let balance = await wo.Store.getBalance(this.actorAddress) let balance = await wo.Account.getBalance(this.actorAddress)
return balance >= this.amount + this.fee return balance >= this.amount + this.fee
} }
MOM.executeMe = async function () { MOM.executeMe = async function () {
let balance = await wo.Store.getBalance(this.actorAddress) let sender= await wo.Account.getOne({Account: { address: this.actorAddress }})
if (balance >= this.amount + this.fee) { if (sender && sender.type !== 'multisig' && this.toAddress != this.actorAddress && sender.balance >= this.amount + this.fee){
await wo.Store.decrease(this.actorAddress, this.amount + this.fee) await sender.setMe({Account:{ balance: Number(sender.balance)-Number(this.amount)-Number(this.fee), countAction: sender.countAction+1 }, cond:{ address:sender.address}})
await wo.Store.increase(this.toAddress, this.amount) let getter= await wo.Account.getOne({Account: { address: this.toAddress }}) || await wo.Account.addOne({Account: { address: this.toAddress }})
await getter.setMe({Account:{ balance: Number(getter.balance)+Number(this.amount), countAction: getter.countAction+1 }, cond:{ address:getter.address}})
mylog.info('Excecuted action='+JSON.stringify(this))
return this return this
} }
// mylog.info('balance('+sender.address+')='+sender.balance+' is less than '+this.amount+', 无法转账')
return null return null
} }

View File

@ -1,15 +1 @@
// const Action = require('./Action.js') // 不要在 index 里引入 Action避免循环无限引入。 module.exports = require('./Action.js')
const ActionTransfer = require('./ActionTransfer.js')
const ActionStore = require('./ActionStore.js')
const ActionMultisig = require('./ActionMultisig.js')
const ActionLockProof = require('./ActionLockProof.js')
// const ActionTac = require('./ActionTac.js')
module.exports = {
// Action,
ActionTransfer,
ActionStore,
ActionMultisig,
ActionLockProof,
// ActionTac
}

4
indexActionTypes.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
ActionTransfer: require('./ActionTransfer.js'),
ActionStore: require('./ActionStore.js')
}

View File

@ -1,9 +1,9 @@
{ {
"name": "tic-traction", "name": "tic.action",
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"so.ling": "git+https://git.faronear.org/so/so.ling", "so.ling": "git+https://git.faronear.org/so/so.ling",
"tic-crypto": "git+https://git.faronear.org/tic/tic-crypto" "tic.crypto": "git+https://git.faronear.org/tic/tic.crypto"
}, },
"devDependencies": {}, "devDependencies": {},
"scripts": {}, "scripts": {},

View File

@ -1,72 +0,0 @@
# https://help.seafile.com/syncing_client/excluding_files/
# 注释。通配符:* 匹配0到若干个字符包括代表目录的/。? 匹配1个字符包括/。
# seafile-ignore.txt 只能控制在客户端需要忽略哪些文件。你依然可以在 seahub 的 web 界面创建这些被客户端忽略的文件。
# 在这种情况下,
# 这些文件会被同步到客户端,但是用户在客户端对这些文件的后续修改会被忽略,不会被同步回服务器。
# 文件在服务器端的后续更改会被同步到客户端,如果客户端也同时修改了这些文件,系统会生成冲突文件。
# seafile-ignore.txt 只能忽略还没有被同步的文件。对于已经被同步的文件,如果后来把它添加到 seafile-ignore.txt 中,系统只会忽略后续更改,已经上传的版本不会受影响。
### seafile-ignore.global.txt ###
# 自定义的后缀名,凡有 sfignore 后缀的都不进行同步
*.sfignore
*.sfignore/
*.sfignore.*
*.sfignore.*/
*.sfomit
*.sfomit.*
*.sfomit/
*.sfomit.*/
*.nosf
*.nosf.*
*.nosf/
*.nosf.*/
.DS_Store
*/.DS_Store
.thumbnails
*/.thumbnails
Thumbs.db
*/Thumbs.db
thumbs.db
*/thumbs.db
_desktop.ini
*/_desktop.ini
._*
*/._*
.$*
*/.$*
~$*
*/~$*
node_modules/
*/node_modules/
package-lock.json
pages4loader.json5
.deploy_git/
*/.deploy_git/
# HBuilder 目录
unpackage/
*/unpackage/
Icon
OneDrive/Icon
# wrangler project
.dev.vars*
*/.dev.vars*
.wrangler/
*/.wrangler/
### seafile-ignore.local.txt ###