This commit is contained in:
陆柯 2022-07-03 16:02:58 +08:00
parent e3920911b0
commit f70b97cbb2
4 changed files with 143 additions and 70 deletions

View File

@ -1,6 +1,5 @@
const Ling = require('so.ling') const Ling = require('so.ling')
const ticrypto = require('tic-crypto') const ticc = require('tic-crypto')
const wo = {}
/** ****************** Public of instance ********************/ /** ****************** Public of instance ********************/
@ -42,7 +41,7 @@ MOM._model = {
MOM.packMe = async function (keypair) { MOM.packMe = async function (keypair) {
// 由前端调用,后台不创建 // 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey this.actorPubkey = keypair.pubkey
this.actorAddress = ticrypto.pubkey2address(keypair.pubkey) this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date() this.timestamp = new Date()
await this.signMe(keypair.seckey) await this.signMe(keypair.seckey)
@ -53,22 +52,18 @@ MOM.packMe = async function (keypair) {
MOM.signMe = async function (seckey) { 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 ticrypto.sign(json, seckey) this.actorSignature = await ticc.sign(json, seckey)
return this return this
} }
MOM.hashMe = function () { MOM.hashMe = function () {
this.hash = ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了 this.hash = ticc.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 ticrypto.verify( let result = await ticc.verify(json, this.actorSignature, this.actorPubkey)
json,
this.actorSignature,
this.actorPubkey
)
return result return result
} }
DAD.verifySig = async function (actionData) { DAD.verifySig = async function (actionData) {
@ -77,7 +72,9 @@ DAD.verifySig = async function (actionData) {
} }
MOM.verifyAddress = function () { MOM.verifyAddress = function () {
return this.actorAddress === ticrypto.pubkey2address(this.actorPubkey) return (
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 wo[actionData.type](actionData)
@ -86,8 +83,7 @@ DAD.verifyAddress = function (actionData) {
MOM.verifyHash = function () { MOM.verifyHash = function () {
return ( return (
this.hash === this.hash === ticc.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
) )
} }
DAD.verifyHash = function (actionData) { DAD.verifyHash = function (actionData) {
@ -157,7 +153,7 @@ DAD.buildUserAction = async function (action, keypair) {
keypair && keypair &&
keypair.seckey && keypair.seckey &&
keypair.pubkey && keypair.pubkey &&
ticrypto.seckey2pubkey(keypair.seckey) === keypair.pubkey ticc.seckey_to_pubkey({ seckey: keypair.seckey }) === keypair.pubkey
) { ) {
let typedAction = new wo[action.type](action) let typedAction = new wo[action.type](action)
typedAction.actorPubkey = keypair.pubkey typedAction.actorPubkey = keypair.pubkey

View File

@ -1,46 +1,64 @@
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 (seckey) { // 由前端调用,后台不该进行签名 MOM.signMe = async function (seckey) {
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash // 由前端调用,后台不该进行签名
this.actorSignature = await wo.Crypto.sign(json, seckey) let json = this.getJson({
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
}) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await ticc.sign(json, seckey)
return this return this
} }
MOM.verifySig = async function () { MOM.verifySig = async function () {
let json = this.getJson(({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] })) let json = this.getJson({
let res = await wo.Crypto.verify(json, this.actorSignature, this.actorPubkey) exclude: ['hash', 'blockHash', 'actorSignature', 'json']
})
let res = await ticc.verify({
data: json,
signature: this.actorSignature,
pubkey: this.actorPubkey
})
return res return res
} }
MOM.verifyAddress = function () { MOM.verifyAddress = function () {
return this.actorAddress === wo.Crypto.pubkey2address(this.actorPubkey) return (
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
} }
MOM.hashMe = function () { MOM.hashMe = function () {
this.hash = wo.Crypto.hash(this.getJson({ exclude: ['hash', 'blockHash', 'json'] })) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了 this.hash = ticc.hash(
this.getJson({ exclude: ['hash', 'blockHash', 'json'] })
) // block.hash 受到所包含的actionList影响所以action不能受blockHash影响否则循环了
return this return this
} }
MOM.verifyHash = function () { MOM.verifyHash = function () {
return this.hash === wo.Crypto.hash(this.getJson({ exclude: ['hash', 'blockHash', 'json'] })) return (
this.hash ===
ticc.hash(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 = wo.Crypto.pubkey2address(keypair.pubkey) this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date() this.timestamp = new Date()
await this.signMe(keypair.seckey) await this.signMe(keypair.seckey)
@ -49,7 +67,9 @@ MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
} }
MOM.checkMultiSig = async function (account) { MOM.checkMultiSig = async function (account) {
let json = this.getJson(({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] })) let json = this.getJson({
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) {
@ -57,9 +77,12 @@ 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 (account.multiSignatures.keysgroup.indexOf(i) !== -1 && await wo.Crypto.verify(json, this.json[i], i)) { if (
account.multiSignatures.keysgroup.indexOf(i) !== -1 &&
(await ticc.verify(json, this.json[i], i))
) {
M++ M++
} }
} }
@ -140,16 +163,20 @@ step3:发起人申请执行
*/ */
MOM.validateMe = async function () { MOM.validateMe = async function () {
return wo.Crypto.isAddress(this.toAddress) && return (
ticc.is_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
} }
@ -160,27 +187,48 @@ MOM.executeMe = async function () {
switch (this.json.act) { switch (this.json.act) {
// 多重签名账户注册 // 多重签名账户注册
case 'sign': { case 'sign': {
let actor = await wo.Account.getOne({ Account: { address: this.actorAddress } }) let actor = await wo.Account.getOne({
Account: { address: this.actorAddress }
})
if (actor && actor.type !== 'multisig') { if (actor && actor.type !== 'multisig') {
// 检查账户类型,只有不是多重签名账户的才可以执行 // 检查账户类型,只有不是多重签名账户的才可以执行
// todo:类型检查,安全操作 // todo:类型检查,安全操作
await actor.setMe({ Account: { multiSignatures: { await actor.setMe({
Account: {
multiSignatures: {
min: this.json.min, min: this.json.min,
ttl: this.json.ttl, // 该账户交易的最大挂起时间 ttl: this.json.ttl, // 该账户交易的最大挂起时间
keysgroup: this.json.keysgroup 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({ Account: { address: this.actorAddress } }) let sender = await wo.Account.getOne({
if (sender && await this.checkMultiSig(sender) && this.toAddress != this.actorAddress && sender.balance >= this.amount + this.fee) { Account: { address: this.actorAddress }
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 (
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({ Account: { balance: getter.balance + this.amount }, cond: { address: getter.address } }) await getter.setMe({
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,15 +1,30 @@
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 (action.data.name && action.data.symbol && action.data.decimals && if (
action.data.name &&
action.data.symbol &&
action.data.decimals &&
Number.isSafeInteger(Number(action.data.decimals)) && Number.isSafeInteger(Number(action.data.decimals)) &&
!await wo.Tac.getOne({ Tac: { name: action.data.name, symbol: action.data.symbol } }) !(await wo.Tac.getOne({
) { return true } Tac: { name: action.data.name, symbol: action.data.symbol }
}))
) {
return true
}
return false return false
case 'transfer': case 'transfer':
return action.amount && action.amount > 0 && action.actorAddress && action.toAddress && action.actorAddress !== action.toAddress return (
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':
@ -38,27 +53,41 @@ class ActionTac extends Action {
writable: false writable: false
}) })
} }
static async validate (action) { // todo 20190409: MOM.validateMe static async validate (action) {
return Methods.includes(action.data.method) && await actValidator(action) // todo 20190409: MOM.validateMe
return Methods.includes(action.data.method) && (await actValidator(action))
} }
static async execute (action) { // todo 20190409: MOM.executeMe static async execute (action) {
// 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(action.data, Object.assign(
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(action.actorSignature, action.hash)
})
return await tac.addMe() return await tac.addMe()
case 'transfer': case 'transfer':
// 内部交易,转发到应用链进程来处理 // 内部交易,转发到应用链进程来处理
await wo.Store.decrease(action.actorAddress, 0 - action.amount, action.address) await wo.Store.decrease(
await wo.Store.increase(action.toAddress, action.amount, action.address) action.actorAddress,
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,5 +1,5 @@
const Action = require('./Action.js') const Action = require('./Action.js')
const ticrypto = require('tic-crypto') const ticc = 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
@ -16,7 +16,7 @@ MOM.validateMe = function () {
return ( return (
this.actorPubkey && this.actorPubkey &&
this.toAddress && this.toAddress &&
ticrypto.pubkey2address(this.actorPubkey) !== this.toAddress && // 不能转帐给自己。 ticc.pubkey_to_address({ pubkey: this.actorPubkey }) !== this.toAddress && // 不能转帐给自己。
this.amount && this.amount &&
this.amount > 0 && this.amount > 0 &&
this.fee >= 0 this.fee >= 0