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 ticrypto = require('tic-crypto')
const wo = {}
const ticc = require('tic-crypto')
/** ****************** Public of instance ********************/
@ -42,7 +41,7 @@ MOM._model = {
MOM.packMe = async function (keypair) {
// 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey
this.actorAddress = ticrypto.pubkey2address(keypair.pubkey)
this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date()
await this.signMe(keypair.seckey)
@ -53,22 +52,18 @@ MOM.packMe = async function (keypair) {
MOM.signMe = async function (seckey) {
// 由前端调用,后台不该进行签名
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
}
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
}
MOM.verifySig = async function () {
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] })
let result = await ticrypto.verify(
json,
this.actorSignature,
this.actorPubkey
)
let result = await ticc.verify(json, this.actorSignature, this.actorPubkey)
return result
}
DAD.verifySig = async function (actionData) {
@ -77,7 +72,9 @@ DAD.verifySig = async function (actionData) {
}
MOM.verifyAddress = function () {
return this.actorAddress === ticrypto.pubkey2address(this.actorPubkey)
return (
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
}
DAD.verifyAddress = function (actionData) {
let typedAction = new wo[actionData.type](actionData)
@ -86,8 +83,7 @@ DAD.verifyAddress = function (actionData) {
MOM.verifyHash = function () {
return (
this.hash ===
ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
this.hash === ticc.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
)
}
DAD.verifyHash = function (actionData) {
@ -157,7 +153,7 @@ DAD.buildUserAction = async function (action, keypair) {
keypair &&
keypair.seckey &&
keypair.pubkey &&
ticrypto.seckey2pubkey(keypair.seckey) === keypair.pubkey
ticc.seckey_to_pubkey({ seckey: keypair.seckey }) === keypair.pubkey
) {
let typedAction = new wo[action.type](action)
typedAction.actorPubkey = keypair.pubkey

View File

@ -1,46 +1,64 @@
const Action = require('./Action.js')
const ticc = require('tic-crypto')
/** ****************** Public of instance ********************/
const DAD = module.exports = function ActionMultisig (prop) {
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 (seckey) { // 由前端调用,后台不该进行签名
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] }) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await wo.Crypto.sign(json, seckey)
MOM.signMe = async function (seckey) {
// 由前端调用,后台不该进行签名
let json = this.getJson({
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
}) // 是前端用户发起事务时签字这时候还不知道进入哪个区块所以不能计入blockHash
this.actorSignature = await ticc.sign(json, seckey)
return this
}
MOM.verifySig = async function () {
let json = this.getJson(({ exclude: ['hash', 'blockHash', 'actorSignature', 'json'] }))
let res = await wo.Crypto.verify(json, this.actorSignature, this.actorPubkey)
let json = this.getJson({
exclude: ['hash', 'blockHash', 'actorSignature', 'json']
})
let res = await ticc.verify({
data: json,
signature: this.actorSignature,
pubkey: this.actorPubkey
})
return res
}
MOM.verifyAddress = function () {
return this.actorAddress === wo.Crypto.pubkey2address(this.actorPubkey)
return (
this.actorAddress === ticc.pubkey_to_address({ pubkey: this.actorPubkey })
)
}
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
}
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.actorAddress = wo.Crypto.pubkey2address(keypair.pubkey)
this.actorAddress = ticc.pubkey_to_address({ pubkey: keypair.pubkey })
this.timestamp = new Date()
await this.signMe(keypair.seckey)
@ -49,7 +67,9 @@ MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
}
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) // 公钥列表
// 交易发起人的签名在prepare的verifySig里已经检查过合法性
if (account.multiSignatures.keysgroup.indexOf(this.actorPubkey) === -1) {
@ -57,9 +77,12 @@ MOM.checkMultiSig = async function (account) {
} else {
let M = 0 // 如果发起人已经在keysgroup里则从0算起
}
for (let i of sigers) // 该交易内已签名的每一个公钥
{
if (account.multiSignatures.keysgroup.indexOf(i) !== -1 && await wo.Crypto.verify(json, this.json[i], i)) {
for (let i of sigers) {
// 该交易内已签名的每一个公钥
if (
account.multiSignatures.keysgroup.indexOf(i) !== -1 &&
(await ticc.verify(json, this.json[i], i))
) {
M++
}
}
@ -140,16 +163,20 @@ step3:发起人申请执行
*/
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.toAddress != this.actorAddress
)
}
MOM.executableMe = async function () {
if (this.json.act === 'createTransfer') { // 创建挂起的多重签名事务
if (this.json.act === 'createTransfer') {
// 创建挂起的多重签名事务
DAD.pendingPool[this.hash] = this
return false
} else if (this.json.act === 'addSig') { // 签名者签名
} else if (this.json.act === 'addSig') {
// 签名者签名
DAD.pendingPool[this.hash].json[this.actorPubkey] = this.json.signature
return false
}
@ -160,27 +187,48 @@ MOM.executeMe = async function () {
switch (this.json.act) {
// 多重签名账户注册
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') {
// 检查账户类型,只有不是多重签名账户的才可以执行
// todo:类型检查,安全操作
await actor.setMe({ Account: { multiSignatures: {
min: this.json.min,
ttl: this.json.ttl, // 该账户交易的最大挂起时间
keysgroup: this.json.keysgroup
} },
cond: { address: actor.address } })
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 } })
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 } })
await getter.setMe({
Account: { balance: getter.balance + this.amount },
cond: { address: getter.address }
})
} else {
await wo.Account.addOne({ Account: { address: this.toAddress } })
}

View File

@ -1,15 +1,30 @@
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 }
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
return (
action.amount &&
action.amount > 0 &&
action.actorAddress &&
action.toAddress &&
action.actorAddress !== action.toAddress
)
case 'exchange':
return true
case 'mount':
@ -27,38 +42,52 @@ class ActionTac extends Action {
enumerable: true,
writable: false
}),
Object.defineProperty(this, 'type', {
value: 'ActionTac',
enumerable: true,
writable: false
}),
Object.defineProperty(this, 'data', {
value: prop,
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 validate (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) {
switch (action.data.method) {
case 'create':
delete action._class
let tac = new wo.Tac(
Object.assign(action.data,
Object.assign(
action.data,
action.actorAddress,
action.actorPubkey,
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()
case 'transfer':
// 内部交易,转发到应用链进程来处理
await wo.Store.decrease(action.actorAddress, 0 - action.amount, action.address)
await wo.Store.increase(action.toAddress, action.amount, action.address)
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类型

View File

@ -1,5 +1,5 @@
const Action = require('./Action.js')
const ticrypto = require('tic-crypto')
const ticc = require('tic-crypto')
const DAD = (module.exports = function ActionTransfer (prop) {
this._class = this.constructor.name
@ -16,7 +16,7 @@ MOM.validateMe = function () {
return (
this.actorPubkey &&
this.toAddress &&
ticrypto.pubkey2address(this.actorPubkey) !== this.toAddress && // 不能转帐给自己。
ticc.pubkey_to_address({ pubkey: this.actorPubkey }) !== this.toAddress && // 不能转帐给自己。
this.amount &&
this.amount > 0 &&
this.fee >= 0