Compare commits

...

48 Commits

Author SHA1 Message Date
Luk
5d23f2efa1 ignore *nogit* and *nosf* 2024-09-22 15:50:40 +08:00
Luk
d8e35affca improve gitignore and seafile-ignore 2024-04-24 14:17:29 +08:00
Luk
19c233e473 updated .gitignore and seafile-ignore.txt using npm/sysconfig/*-ignore-find2merge.sh 2024-01-28 12:16:46 +08:00
eceafbb5f6 update seafile-ignore.txt 2023-09-12 14:20:49 +08:00
5246988a6f update .gitignore to ignore *.gitignore.* instead of envar-*.gitignore.js 2023-03-11 08:27:22 +08:00
7f81d65a6d 'add seafile-ignore.txt to every git repo' 2023-01-12 20:10:06 +08:00
51df83f884 update to standard .gitignore 2022-12-10 19:12:17 +08:00
874f18ee25 update [.gitignore] to ignore /unpackage/* except for !/unpackage/res/ 2022-10-06 20:06:10 +08:00
9a4978f6fd use standard [.gitignore] file 2022-08-20 10:24:59 +08:00
e9542cb32c rename is_chain_address to which_chain_address 2022-08-16 15:30:35 +08:00
76deb199a5 rename seckey to prikey 2022-08-14 15:51:24 +08:00
8cf67bf90c rename ticc.sign/verify/encrypt/decrypt/hash to ticc.xxx_easy 2022-07-23 16:03:24 +08:00
f70b97cbb2 u 2022-07-03 16:02:58 +08:00
e3920911b0 u 2022-06-05 11:30:32 +08:00
cfad03864a 添加 getTypedAction和createTypedAction方法,为了修正错误:node.server的强化版的BlockPot.js执行action.executeMe()失败,因为Action.getBatchAction()进行了JSON.stringify;注释掉 wo.Netnode.broadcast 因为报错说不存在。 2020-02-28 17:09:26 +08:00
9099da4ab9 上个commit不够对,actorAddress 是在 packMe 中被赋值的,不需要提前检测。 2020-02-28 12:24:35 +08:00
61318e8940 ActionTransfer: 必须检查发起人地址和公钥是匹配的,否则客户端能够造假 2020-02-27 20:02:35 +08:00
bbb573c3a8 u 2020-02-27 19:26:00 +08:00
8769cd3abc 增加 _initTypes 方法和 TypedActionList,来插入子类型。 2020-02-27 18:49:26 +08:00
e6ae650f7a 去掉 Action.js 里对 ActionXxx 的引入,这样还是会造成循环引入的。 2020-02-27 16:08:11 +08:00
be8fe145a5 刷新 package.json 里的库 2020-02-27 15:29:28 +08:00
a2c7592847 不在 index.js 里引入 Action.js,避免循环引入。 2020-02-27 15:27:04 +08:00
e59f2d92f5 add 'async' to checkMultiSig 2020-02-21 15:44:14 +08:00
c9f04b24eb 随着 tic.crypto 升级到 secp256k1 的公私钥和签名,把 signMe, packMe, verifySig 改成 async 的 2020-02-20 18:53:00 +08:00
4e2a4b35db DAD._table 改为 MOM._table 2020-02-10 16:04:07 +08:00
2e1bbe2904 更新 'README.md' 2019-08-27 07:31:32 +00:00
75d8fcb260 整理 GIT 项目库导航 结构 2019-08-26 15:19:09 +08:00
a3c5cc2dd9 update packages to 20190714_release_passtoken 2019-07-14 11:46:19 +08:00
f41338f95f [package.json] so.ling 指向 20190701_release_initDb 2019-07-01 20:11:44 +08:00
2055080854 Merge branch 'luk.lu_initDatabaseFromOutside' of tic/tic.action into master 2019-07-01 12:09:47 +00:00
f7632ce6c1 [package.json] so.ling指向 #luk.lu_initDatabaseFromOutside 2019-06-17 11:23:47 +08:00
7408ed43cc 文档改进:latest_stable => RELEASE_OR_BRANCH 2019-06-17 11:21:23 +08:00
ca15dc3eeb 改进文档。 2019-05-18 16:38:13 +08:00
c0f03840e0 改进文档。 2019-05-18 11:49:13 +08:00
2e591f6bb0 [README.md] 添加说明文档。 2019-05-18 10:57:38 +08:00
57df61a2b3 更新绑定到 so.ling#20190427_fon2so 2019-04-27 10:00:30 +08:00
6465159122 迁移 fon/fon.xxx 库到 so/so.xxx 2019-04-22 00:57:02 +08:00
f527f942b6 把会在 require('Action.js') 时就执行的 wo.Netnode.on('broadcast', DAD.api.prepare) 删除(迁移到 node.server/server.js 里),解除 Action 对 wo.Netnode 的依赖,否则前端引入 Action 也需要依赖后台的网络而无法启动。 2019-04-18 11:48:38 +08:00
48b1cb4bd6 Rename wo.NodeNet to wo.Netnode 2019-04-13 14:15:52 +08:00
a733f000d5 删除多余代码,这个测试已经在上一层 if (...) 里出现过了。 2019-04-13 13:46:09 +08:00
64f095304b Merge branch 'master' of https://git.faronear.org/tic/tic.action
# Conflicts:
#	Action.js
2019-04-13 13:41:09 +08:00
luk.lu
a289a4b2ad 1. 把 validateMe 分解成 静态数据检查(给客户端调用)validateMe 和 动态可执行性检查(给链节点调用)executableMe.
2. 在 Action.api.prepare() 里,生成可运行的对象 typedAction 存入 ActionPool,而不是仅仅存数据 option.Action进去。
3. 删除 Action.getJson(),把 DAD.verifyXxx(action) 都改为 MOM.verifyXxx().
4. 添加了 ActionRegisterChain.js 作为 应用链注册事务。
2019-04-13 13:37:38 +08:00
luk.lu
3cf673af6f action.packMe(keypair)调用有个无关紧要的小错:输入参数是 {seckey, pubkey} 即可,不需要传入 address,因为 address 是 packMe 内部自己根据 pubkey 生成的。 2019-04-11 15:59:26 +08:00
luk.lu
044c2175d6 纠错:完善 index.js 返回所有 ActionXxx 子类 2019-04-09 20:34:13 +08:00
luk.lu
4fe26a0b09 采用新结构:Action*.js 从 node.server 中删除,集中存放于本库。
这样消除了同一份代码出现在两处的不良结构,避免了同步的困难。
当 node.server 需要临时修改 ActionXxx.js 时,只要在 server.js 里临时 require('../tic.action').ActionXxx 即可。一处修改,到处可用。
2019-04-09 20:16:58 +08:00
luk.lu
e2a0738a80 把 this._class = 'ActionTransfer' 改为 this._class = this.constructor.name,这才是标准写法,不知道为何ActionTranfer用了字符串。 2019-04-09 09:25:58 +08:00
luk.lu
73ca4df3db 重命名 ActTransfer 为 ActionTransfer;
删除 ling 目录,把 Action*.js 迁移到上级根目录。
2019-04-09 09:06:33 +08:00
luk.lu
c18b412c1b [Action.js] prepare方法里的wo.Peer 改为 wo.Node。但这其实不影响使用,因为 prepare 方法只在 node.server 里被调用,而在前端只用到 sign, hash 等方法。 2019-02-18 13:36:26 +08:00
14 changed files with 990 additions and 185 deletions

118
.gitignore vendored
View File

@ -1,7 +1,113 @@
# 以'#'开始的行,被视为注释.
node_modules
package-lock.json
.vscode
.svn
~*
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# how to include another gitignore?
# 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
pages4loader.json5
### .gitignore.local.txt ###

241
Action.js Normal file
View File

@ -0,0 +1,241 @@
const Ling = require('so.ling')
const ticc = require('tic-crypto')
/** ****************** Public of instance ********************/
const DAD = (module.exports = function Action (prop) {
this._class = this.constructor.name
this.setProp(prop)
this.type = this.constructor.name
})
DAD.__proto__ = Ling
const MOM = DAD.prototype
MOM.__proto__ = Ling.prototype
/** ****************** Shared by instances ********************/
MOM._table = DAD.name
MOM._tablekey = 'hash'
MOM._model = {
hash: {
default: undefined,
sqlite: 'TEXT UNIQUE',
mysql: 'VARCHAR(64) PRIMARY KEY'
}, // 不纳入签名和哈希
version: { default: 0, sqlite: 'INTEGER' },
type: { default: 'Action', sqlite: 'TEXT', mysql: 'VARCHAR(100)' }, // 是否放在 assets里更好这里该放action自己的version
blockHash: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(64)' }, // 不纳入签名和哈希。只为了方便查找
timestamp: { default: undefined, sqlite: 'TEXT', mysql: 'CHAR(24)' },
actorPubkey: { default: undefined, sqlite: 'TEXT', mysql: 'BINARY(32)' },
actorAddress: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(50)' },
actorSignature: { default: undefined, sqlite: 'TEXT', mysql: 'BINARY(64)' }, // 不纳入签名,纳入哈希
toAddress: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(50)' },
amount: { default: 0, sqlite: 'NUMERIC', mysql: 'BIGINT' },
fee: { default: 0, sqlite: 'NUMERIC', mysql: 'BIGINT' },
message: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(256)' },
dataIndex: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(50)' }, // 用于索引json中存储数据
method: { default: undefined, sqlite: 'TEXT' },
json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构
}
MOM.packMe = async function (keypair) {
// 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey
this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date()
await this.signMe(keypair.prikey)
this.hashMe()
return this
}
MOM.signMe = async function (prikey) {
// 由前端调用,后台不该进行签名
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await ticc.sign_easy({ data: json, prikey })
return this
}
MOM.hashMe = function () {
this.hash = ticc.hash_easy(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
return this
}
MOM.verifySig = async function () {
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] })
let result = await ticc.verify_easy({
data: json,
signature: this.actorSignature,
pubkey: this.actorPubkey
})
return result
}
DAD.verifySig = async function (actionData) {
let typedAction = new wo[actionData.type](actionData)
return await typedAction.verifySig()
}
MOM.verifyAddress = function () {
return (
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
}
DAD.verifyAddress = function (actionData) {
let typedAction = new wo[actionData.type](actionData)
return typedAction.verifyAddress()
}
MOM.verifyHash = function () {
return (
this.hash ===
ticc.hash_easy(this.getJson({ exclude: ['hash', 'blockHash'] }))
)
}
DAD.verifyHash = function (actionData) {
let typedAction = new wo[actionData.type](actionData)
return typedAction.verifyHash()
}
MOM.validateMe = function () {
// Applicable on both client and chain server. 子类应当覆盖本方法,静态的检查事务内容的格式。不能检查 balance 等需要全链数据库的东西,因为本方法也要用在前端检查。
// to implement in subclasses: 检查子类事务内容的格式
let typedAction = new wo[this.type](this)
return typedAction.validateMe()
}
DAD.validate = function (action) {
// Allicable on both client and chain server.
mylog.info(`Validating action type=${action.type} of hash=${action.hash}`)
let typedAction = new wo[action.type](action)
return typedAction.validateMe()
}
MOM.executableMe = async function () {
// Applicable on chain server. 子类应当覆盖本方法动态的检查事务内容在当前链状态下是否能执行。To check if an action is executableMe given the current chain status.
let typedAction = new wo[this.type](this)
return await typedAction.executableMe()
}
DAD.executable = async function (action) {
// For chain server.
let typedAction = new wo[action.type](action)
if (typedAction.hasOwnProperty('executableMe')) {
// 防止子类忘了定义自己的 executableMe
return await typedAction.executableMe()
} else {
return true
}
}
MOM.executeMe = async function () {
// For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
// to implement in subclasses: 把action的影响汇总登记到其他表格用于辅助的、索引的表格方便快速索引、处理。每种事务类型都要重定义这个方法。
let typedAction = new wo[this.type](this)
return await typedAction.executeMe()
}
DAD.execute = async function (action) {
// For chain server.
mylog.info(`Excecuting action type=${action.type} of hash=${action.hash}`)
let typedAction = new wo[action.type](action)
return await typedAction.executeMe()
}
// [todo 20190411] 执行事务池中的所有事务
// DAD.executePool = async function() {
// }
DAD._initTypeDict = function (typedActionDict) {
Object.assign(wo, typedActionDict)
}
DAD.getTypedAction = function (type) {
return wo[type]
}
DAD.createTypedAction = function (action) {
return new wo[action.type](action)
}
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()) {
await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
return typedAction
}
}
return null
}
/**
* 获取一批交易在出块时调用调用actionPool的内容被深拷贝到currentActionPool后自动清空
* 所以在一次出块期间只能调用一次
*/
DAD.getActionBatch = function () {
let actionBatch = {
actionPool: JSON.parse(JSON.stringify(DAD.actionPool)), // deep copy
totalAmount: DAD.actionPoolInfo.totalAmount,
totalFee: DAD.actionPoolInfo.totalFee
}
DAD.actionPool = {}
DAD.actionPoolInfo = {
totalAmount: 0,
totalFee: 0
}
return actionBatch
}
/** ********************* Public of class *******************/
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.prepare = async function (option) {
if (typeof option === 'string') {
try {
option = JSON.parse(option)
} catch (error) {}
}
// 前端发来action数据进行格式检查不检查是否可执行--这和事务类型、执行顺序有关)后放入缓冲池。
if (
option &&
option.Action &&
option.Action.type &&
wo[option.Action.type] &&
option.Action.hash &&
!DAD.actionPool[option.Action.hash]
) {
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.actionPoolInfo.totalAmount += option.Action.amount || 0
DAD.actionPoolInfo.totalFee += option.Action.fee || 0
// wo.Netnode.broadcast({ Action: option.Action }) // 即使对 master 分支的node.server 也报错Cannot read property 'broadcast' of undefined
return option.Action
}
}
return null // 非法的交易数据
}
/** ******************** Private in class *******************/
DAD.actionPool = {} // 交易池在执行getActionBatch时被清空
// DAD.currentActionPool = {} // 仅包含0~40秒的交易,40~59秒的交易将被堆积到actionPool。
DAD.actionPoolInfo = {
totalAmount: 0,
totalFee: 0
}

26
ActionLockProof.js Normal file
View File

@ -0,0 +1,26 @@
'use strict'
/**
Layer 2
1.从链上申请取得余额锁定证明或者额度锁定证明Proof of Locked(POL)
LockProof
{
hash: '0x12324tgw423fa23hrio2uhiut32gt823i4uth243i9',
amount: 100,
blockHeight: 10086,
blockHash: '0xweg34ga243yt54yh543hj65tbxfdgbxdfbx',
actorAddress: 'TARbCVuy5g2u2uUYGVIUYyvuIUvhj3',
actorPubkey: '8912rbiu2341t98o43bguierg98374h7tou34trnh32io87g7ayub',
actorSignature: 'iu12b3ru32tqoknhiaubwefvbuwfibw',
packerPubkey: 'aopiwhgawe9igawonibirugnbiuwehnsjc'
packerSignature: '0xuihg87324hfuiweaoigwnhjriaerwogireabhwg',
expire: 100 //100块后作废
}
2.应用层验证POL检查是否标准是否过期
*/
/*
聚合交易
{
actorAddress:[],
toAddress:[]
}
*/

257
ActionMultisig.js Normal file
View File

@ -0,0 +1,257 @@
const Action = require('./Action.js')
const ticc = require('tic-crypto')
/** ****************** Public of instance ********************/
const DAD = (module.exports = function ActionMultisig (prop) {
this._class = this.constructor.name
this.setProp(prop) // 没有定义 DAD.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = this.constructor.name
})
DAD.__proto__ = Action
const MOM = DAD.prototype
MOM.__proto__ = Action.prototype
// MOM._table=DAD.name // 注释掉从而继承父类Action的数据库表格名
MOM.signMe = async function (prikey) {
// 由前端调用,后台不该进行签名
let json = this.getJson({
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
}) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await ticc.sign_easy({ data: json, prikey })
return this
}
MOM.verifySig = async function () {
let json = this.getJson({
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
})
let res = await ticc.verify_easy({
data: json,
signature: this.actorSignature,
pubkey: this.actorPubkey
})
return res
}
MOM.verifyAddress = function () {
return (
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
}
MOM.hashMe = function () {
this.hash = ticc.hash_easy(
this.getJson({ exclude: ['hash', 'blockHash', 'json'] })
) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
return this
}
MOM.verifyHash = function () {
return (
this.hash ===
ticc.hash_easy(this.getJson({ exclude: ['hash', 'blockHash', 'json'] }))
)
}
MOM.packMe = async function (keypair) {
// 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey
this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date()
await this.signMe(keypair.prikey)
this.hashMe()
return this
}
MOM.checkMultiSig = async function (account) {
let json = this.getJson({
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
})
let sigers = Object.keys(this.json) // 公钥列表
// 交易发起人的签名在prepare的verifySig里已经检查过合法性
if (account.multiSignatures.keysgroup.indexOf(this.actorPubkey) === -1) {
let M = 1 // 如果不在keysgroup里可以把交易发起人算一个有效的签名因此M从1算起
} else {
let M = 0 // 如果发起人已经在keysgroup里则从0算起
}
for (let i of sigers) {
// 该交易内已签名的每一个公钥
if (
account.multiSignatures.keysgroup.indexOf(i) !== -1 &&
(await ticc.verify_easy({
data: json,
signature: this.json[i],
pubkey: i
}))
) {
M++
}
}
return M >= account.multiSignatures.min
}
/** ****************** Shared by instances ********************/
/*
1.创建多重签名账户
{
"ActionMultisig":{
"actorPubkey": "actorPubkey",
"actorAddress": "actorAddress",
"actorSignature":"actorSignature",
"fee":1,
"json":{
act: "create",
min: 2,
lifetime: 10, //暂时无用,因为每个交易的挂起时间需求可能不同
keysgroup:[
pubkey_a,
pubkey_b,
...
pubkey_n
]
}
}
}
2.多重签名账户交易
step1:发起一个多重签名账户的交易该交易只是在缓存里为了给多重签名账户的控制者们提供真正要写入区块链的源Action数据
{
"ActionMultisig":{
"amount": 100,
"fee": 1,
"actorPubkey": "actorPubkey",
"actorAddress": "actorAddress",
"actorSignature":"actorSignature",
"toAddress": "toAddress",
"json":{
act: "createTransfer",
lifetime: 10,
}
}
}
step2:所有人签名
{
"ActionMultisig":{
"amount": 100,
"fee": 1,
"actorPubkey": "actorPubkey",
"actorAddress": "actorAddress",
"actorSignature":"actorSignature",
"toAddress": "toAddress",
"json":{
act: 'addSig',
signature: 'signature',
}
}
}
step3:发起人申请执行
{
"ActionMultisig":{
"amount": 100,
"fee": 1,
"actorPubkey": "actorPubkey",
"actorAddress": "actorAddress",
"actorSignature":"actorSignature",
"toAddress": "toAddress",
"json":{
act:'emitTransfer'
'pubkey1':'sig1',
'pubkey2':'sig2',
......
'pubkeyn':'sign',
}
}
}
*/
MOM.validateMe = async function () {
return (
ticc.which_chain_address({ address: this.toAddress }) &&
this.fee >= wo.Config.MIN_FEE_ActionTransfer &&
this.toAddress != this.actorAddress
)
}
MOM.executableMe = async function () {
if (this.json.act === 'createTransfer') {
// 创建挂起的多重签名事务
DAD.pendingPool[this.hash] = this
return false
} else if (this.json.act === 'addSig') {
// 签名者签名
DAD.pendingPool[this.hash].json[this.actorPubkey] = this.json.signature
return false
}
return true
}
MOM.executeMe = async function () {
switch (this.json.act) {
// 多重签名账户注册
case 'sign': {
let actor = await wo.Account.getOne({
Account: { address: this.actorAddress }
})
if (actor && actor.type !== 'multisig') {
// 检查账户类型,只有不是多重签名账户的才可以执行
// todo:类型检查,安全操作
await actor.setMe({
Account: {
multiSignatures: {
min: this.json.min,
ttl: this.json.ttl, // 该账户交易的最大挂起时间
keysgroup: this.json.keysgroup
}
},
cond: { address: actor.address }
})
}
return this
}
// 多重签名账户执行转账
case 'emitTransfer': {
let sender = await wo.Account.getOne({
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 }
})
let getter = await wo.Account.getOne({
Account: { address: this.toAddress }
})
if (getter) {
await getter.setMe({
Account: { balance: getter.balance + this.amount },
cond: { address: getter.address }
})
} else {
await wo.Account.addOne({ Account: { address: this.toAddress } })
}
// mylog.info('Excecuted action='+JSON.stringify(this))
delete DAD.pendingPool[this.hash]
return this
}
// mylog.info('balance('+sender.address+')='+sender.balance+' is less than '+this.amount+', 无法转账')
return null
}
}
}
DAD.pendingPool = {} // 存放所有待签名的多重签名账户交易
DAD.api = {}
/* 为挂起状态的多重签名交易提供查询服务 */
DAD.api.pendingAction = function (option) {
return DAD.pendingPool[option.id]
}

37
ActionRegisterChain.js Normal file
View File

@ -0,0 +1,37 @@
const Action = require('./Action.js')
const DAD = module.exports = function ActionRegisterChain (prop) {
this._class = this.constructor.name
this.setProp(prop) // 没有定义 ActionTransfer.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = this.constructor.name
}
DAD.__proto__ = Action
const MOM = DAD.prototype
MOM.__proto__ = Action.prototype
MOM.validateMe = function () {
let result = (this.fee >= wo.Config.MIN_FEE_ActionRegisterChain || 0) &&
// 检查事务内容数据,是否符合本类事务的格式和语义要求:
this.json &&
this.json.consensus && // 共识机制
this.json.network && // 网络机制
this.json.chain && // 区块机制
this.json.chain.period && // 出块周期
this.json.chain.size // 区块大小
return result
}
MOM.executableMe = async function() {
// todo: 检查是否已有同名的应用链?以及其他检查。
let balance = await wo.Store.getBalance(this.actorAddress)
return balance >= this.fee
}
MOM.executeMe = async function () {
return this
}
/** ****************** Public of class ********************/
DAD.api = {}

40
ActionStore.js Normal file
View File

@ -0,0 +1,40 @@
const Action = require('./Action.js')
/** ****************** Public of instance ********************/
const DAD = module.exports = function ActionStore (prop) {
this._class = this.constructor.name
this.setProp(prop) // 没有定义 DAD.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = this.constructor.name
}
DAD.__proto__ = Action
const MOM = DAD.prototype
MOM.__proto__ = Action.prototype
// MOM._table=DAD.name // 注释掉从而继承父类Action的数据库表格名
/** ****************** Shared by instances ********************/
MOM.validateMe = function () {
// check size, account balance >= fee, fee>wo.Config.MIN_FEE_ActionStore
return (this.fee >= wo.Config.MIN_FEE_ActionStore || 0)
}
MOM.executableMe = async function (){
let balance = await wo.Store.getBalance(this.actorAddress)
return balance >= this.fee &&
true // todo: 检查服务器上的存储容量是否还够用?
}
MOM.executeMe = async function () {
let actor = await wo.Account.getOne({ Account: { address: this.actorAddress } })
if (actor && actor.balance >= this.fee) {
await actor.setMe({ Account: { balance: actor.balance - this.fee }, cond: { address: actor.address } }) // todo 20190409: change to use wo.Store
return this
}
return null
}
/** ****************** Public of class ********************/
DAD.api = {}

105
ActionTac.js Normal file
View File

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

43
ActionTransfer.js Normal file
View File

@ -0,0 +1,43 @@
const Action = require('./Action.js')
const ticc = require('tic-crypto')
const DAD = (module.exports = function ActionTransfer (prop) {
this._class = this.constructor.name
this.setProp(prop) // 没有定义 ActionTransfer.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = this.constructor.name
})
DAD.__proto__ = Action
const MOM = DAD.prototype
MOM.__proto__ = Action.prototype
MOM.validateMe = function () {
// if (sender && sender.type !== 'multisig' && action.toAddress != action.actorAddress && sender.balance >= action.amount + action.fee){
return (
this.actorPubkey &&
this.toAddress &&
ticc.pubkey_to_address({ pubkey: this.actorPubkey }) !== this.toAddress && // 不能转帐给自己。
this.amount &&
this.amount > 0 &&
this.fee >= 0
)
}
MOM.executableMe = async function () {
let balance = await wo.Store.getBalance(this.actorAddress)
return balance >= this.amount + this.fee
}
MOM.executeMe = async function () {
let balance = await wo.Store.getBalance(this.actorAddress)
if (balance >= this.amount + this.fee) {
await wo.Store.decrease(this.actorAddress, this.amount + this.fee)
await wo.Store.increase(this.toAddress, this.amount)
return this
}
return null
}
/** ****************** Public of class ********************/
DAD.api = {}

41
README.md Normal file
View File

@ -0,0 +1,41 @@
# tic.action
时光链事务库:前后端通用的事务库。前端用来生成用户指定的事务并签名,后台用来验证、准备、执行用户提交的事务。
## Table of Contents
1. [Installation Guide](#1-installation-guide)
2. [Usage](#2-usage)
3. [API Specification](#3-api-specification)
4. [References](#4-references)
## 1. Installation Guide
在前后端软件的 package.json 的依赖清单中引入本库:
```
npm install git+https://git.faronear.org/tic/tic.action#RELEASE_OR_BRANCH --save
```
## 2. Usage
首先在前端JS里创建事务
```
let ActTransfer=require('tic.action/ActTransfer') // 引用
let action=new ActTransfer({ amount: 金额, toAddress: 对方地址}) // 组装事务对象
action.packMe(keypair) // 用当前用户的公私钥,对该事务进行签名
```
然后把事务发送到时光链,例如通过 jQuery
```
$.post(
'https://test.bittic.net/api/Action/prepare',
{ Action:action },
'json'
)
```
## 3. API Specification
## 4. References
+ 返回根文档: <https://git.faronear.org/tic/_tic/>

View File

@ -1,6 +1,15 @@
const Action = require('./ling/Action.js')
const ActTransfer = require('./ling/ActTransfer.js')
// const Action = require('./Action.js') // 不要在 index 里引入 Action避免循环无限引入。
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,
ActTransfer
// Action,
ActionTransfer,
ActionStore,
ActionMultisig,
ActionLockProof,
// ActionTac
}

View File

@ -1,28 +0,0 @@
const Action = require('./Action.js')
const DAD = module.exports = function ActTransfer (prop) {
this._class = 'ActTransfer'
this.setProp(prop) // 没有定义 ActTransfer.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type = 'ActTransfer'
}
DAD.__proto__ = Action
const MOM = DAD.prototype
MOM.__proto__ = Action.prototype
DAD.validate = async function (action) {
// if (sender && sender.type !== 'multisig' && action.toAddress != action.actorAddress && sender.balance >= action.amount + action.fee){
let sender = await wo.Store.getBalance(action.actorAddress)
return action.actorAddress && action.toAddress && action.toAddress != action.actorAddress && action.amount && action.amount > 0 && sender >= action.amount + action.fee && action.fee >= wo.Config.MIN_FEE_ActTransfer
}
DAD.execute = async function (action) {
let sender = await wo.Store.getBalance(action.actorAddress)
if (sender >= action.amount + action.fee) {
await wo.Store.decrease(action.actorAddress, action.amount + action.fee)
await wo.Store.increase(action.toAddress, action.amount)
return true
}
return null
}
DAD.api = {}

View File

@ -1,144 +0,0 @@
var Ling = require('fon.ling')
var Ticrypto = require('tic.crypto')
/** ****************** Public of instance ********************/
const DAD = module.exports = function Action (prop) {
this._class = this.constructor.name
this.setProp(prop)
}
DAD.__proto__ = Ling
DAD._table = DAD.name
const MOM = DAD.prototype
MOM.__proto__ = Ling.prototype
/** ****************** Shared by instances ********************/
MOM._tablekey = 'hash'
MOM._model = {
hash: { default: undefined, sqlite: 'TEXT UNIQUE', mysql: 'VARCHAR(64) PRIMARY KEY' }, // 不纳入签名和哈希
version: { default: 0, sqlite: 'INTEGER' },
type: { default: 'Action', sqlite: 'TEXT', mysql: 'VARCHAR(100)' }, // 是否放在 assets里更好这里该放action自己的version
blockHash: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(64)' }, // 不纳入签名和哈希。只为了方便查找
timestamp: { default: undefined, sqlite: 'TEXT', mysql: 'CHAR(24)' },
actorPubkey: { default: undefined, sqlite: 'TEXT', mysql: 'BINARY(32)' },
actorAddress: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(50)' },
actorSignature: { default: undefined, sqlite: 'TEXT', mysql: 'BINARY(64)' }, // 不纳入签名,纳入哈希
toAddress: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(50)' },
amount: { default: 0, sqlite: 'NUMERIC', mysql: 'BIGINT' },
fee: { default: 0, sqlite: 'NUMERIC', mysql: 'BIGINT' },
message: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(256)' },
dataIndex: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(50)' }, // 用于索引json中存储数据
method: { default: undefined, sqlite: 'TEXT' },
data: { default: undefined, sqlite: 'TEXT' }
}
MOM.packMe = function (keypair) { // 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey
this.actorAddress = Ticrypto.pubkey2address(keypair.pubkey)
this.timestamp = new Date()
this.signMe(keypair.seckey)
this.hashMe()
return this
}
MOM.signMe = function (seckey) { // 由前端调用,后台不该进行签名
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = Ticrypto.sign(json, seckey)
return this
}
MOM.hashMe = function () {
this.hash = Ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
return this
}
DAD.getJson = function (action, option = {}) {
let data = {}
let sortedKey = Object.keys(DAD.prototype._model).sort()
for (let exkey of option.exclude) { sortedKey.splice(sortedKey.indexOf(exkey), 1) }
for (let key of sortedKey) { // 忽略一些不需要签名的属性
data[key] = action[key]
}
let json = JSON.stringify(data)
return json
}
DAD.verifySig = function (action) {
let json = DAD.getJson(action, { exclude: ['hash', 'blockHash', 'actorSignature'] })
let res = Ticrypto.verify(json, action.actorSignature, action.actorPubkey)
return res
}
DAD.verifyAddress = function (action) {
return action.actorAddress === Ticrypto.pubkey2address(action.actorPubkey)
}
DAD.verifyHash = function (action) {
return action.hash === Ticrypto.hash(DAD.getJson(action, { exclude: ['hash', 'blockHash'] }))
}
DAD.execute = function () { // 子类应当覆盖本方法。把action的影响汇总登记到其他表格用于辅助的、索引的表格方便快速索引、处理。每种事务类型都要重定义这个方法。
// save to account or other tables
return this
}
DAD.calculateFee = function () {
return 1000
}
/**
* 获取一批交易在出块时调用调用actionPool的内容被深拷贝到currentActionPool后自动清空
* 所以在一次出块期间只能调用一次
*/
DAD.getActionBatch = function () {
let actionBatch = {
actionPool: JSON.parse(JSON.stringify(DAD.actionPool)), // deep copy
totalAmount: DAD.actionPoolInfo.totalAmount,
totalFee: DAD.actionPoolInfo.totalFee
}
DAD.actionPool = {}
DAD.actionPoolInfo = {
totalAmount: 0,
totalFee: 0
}
return actionBatch
}
/** ********************* Public of class *******************/
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.prepare = async function (option) {
// 前端发来action数据进行初步检查不检查是否可执行--这和事务类型、执行顺序有关,只检查格式是否有效--这是所有事务通用的规范)后放入缓冲池。
if (option && option.Action && option.Action.type && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
if (DAD.verifyAddress(option.Action) &&
DAD.verifySig(option.Action) &&
DAD.verifyHash(option.Action) &&
!DAD.actionPool[option.Action.hash] &&
(await wo[option.Action.type].validate(option.Action))
) {
DAD.actionPool[option.Action.hash] = option.Action
DAD.actionPoolInfo.totalAmount += option.Action.amount || 0
DAD.actionPoolInfo.totalFee += option.Action.fee || 0
wo.Peer.broadcast('/Action/prepare', option)
return option.Action
}
}
return null // 非法的交易数据
}
/** ******************** Private in class *******************/
DAD.actionPool = {} // 交易池在执行getActionBatch时被清空
// DAD.currentActionPool = {} // 仅包含0~40秒的交易,40~59秒的交易将被堆积到actionPool。
DAD.actionPoolInfo = {
totalAmount: 0,
totalFee: 0
}

View File

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

72
seafile-ignore.txt Normal file
View File

@ -0,0 +1,72 @@
# 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 ###