2017-10-31 18:05:15 +01:00
|
|
|
const logger = require('winston');
|
2018-04-27 04:19:11 +02:00
|
|
|
const returnShortId = require('./utils/returnShortId.js');
|
2017-10-31 18:05:15 +01:00
|
|
|
|
2018-04-29 21:17:23 +02:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
|
|
|
|
|
|
|
function isLongChannelId (channelId) {
|
|
|
|
return (channelId && (channelId.length === 40));
|
|
|
|
}
|
|
|
|
|
|
|
|
function isShortChannelId (channelId) {
|
|
|
|
return (channelId && (channelId.length < 40));
|
|
|
|
}
|
|
|
|
|
2017-11-08 16:25:21 +01:00
|
|
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
|
2017-08-21 22:27:13 +02:00
|
|
|
const Certificate = sequelize.define(
|
|
|
|
'Certificate',
|
|
|
|
{
|
|
|
|
address: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
amount: {
|
2017-11-08 16:25:21 +01:00
|
|
|
type : DECIMAL(19, 8),
|
2017-08-21 22:27:13 +02:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
claimId: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
claimSequence: {
|
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
decodedClaim: {
|
|
|
|
type : BOOLEAN,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
depth: {
|
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
effectiveAmount: {
|
2017-11-08 16:25:21 +01:00
|
|
|
type : DECIMAL(19, 8),
|
2017-08-21 22:27:13 +02:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
hasSignature: {
|
|
|
|
type : BOOLEAN,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
height: {
|
2017-11-08 00:42:48 +01:00
|
|
|
type : INTEGER,
|
2017-08-21 22:27:13 +02:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
hex: {
|
|
|
|
type : TEXT('long'),
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
nout: {
|
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
txid: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
validAtHeight: {
|
2017-11-08 00:42:48 +01:00
|
|
|
type : INTEGER,
|
2017-08-21 22:27:13 +02:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
outpoint: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
valueVersion: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
claimType: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
certificateVersion: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
keyType: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
publicKey: {
|
|
|
|
type : TEXT('long'),
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
|
|
|
);
|
2017-09-15 23:41:47 +02:00
|
|
|
|
|
|
|
Certificate.associate = db => {
|
2017-09-26 06:03:43 +02:00
|
|
|
Certificate.belongsTo(db.Channel, {
|
2017-09-15 23:41:47 +02:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-31 18:05:15 +01:00
|
|
|
Certificate.getShortChannelIdFromLongChannelId = function (longChannelId, channelName) {
|
2018-01-30 20:46:22 +01:00
|
|
|
logger.debug(`getShortChannelIdFromLongChannelId ${channelName}:${longChannelId}`);
|
2017-10-31 18:05:15 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: {name: channelName},
|
|
|
|
order: [['height', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2018-05-01 00:58:04 +02:00
|
|
|
return reject(NO_CHANNEL);
|
2017-10-31 18:05:15 +01:00
|
|
|
default:
|
2017-11-04 01:10:08 +01:00
|
|
|
return resolve(returnShortId(result, longChannelId));
|
2017-10-31 18:05:15 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-29 21:17:23 +02:00
|
|
|
Certificate.validateLongChannelId = function (name, claimId) {
|
|
|
|
logger.debug(`validateLongChannelId(${name}, ${claimId})`);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.findOne({
|
|
|
|
where: {name, claimId},
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
if (!result) {
|
|
|
|
return reject(NO_CHANNEL);
|
|
|
|
}
|
|
|
|
resolve(claimId);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error(error);
|
|
|
|
reject(NO_CHANNEL);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-06 00:11:16 +01:00
|
|
|
Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelClaimId) {
|
2018-01-30 20:46:22 +01:00
|
|
|
logger.debug(`getLongChannelIdFromShortChannelId(${channelName}, ${channelClaimId})`);
|
2017-11-01 00:23:11 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: {
|
|
|
|
name : channelName,
|
|
|
|
claimId: {
|
2018-05-15 00:23:50 +02:00
|
|
|
[sequelize.Op.like]: `${channelClaimId}%`,
|
2017-11-01 00:23:11 +01:00
|
|
|
},
|
|
|
|
},
|
2017-11-01 04:35:33 +01:00
|
|
|
order: [['height', 'ASC']],
|
2017-11-01 00:23:11 +01:00
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2018-04-29 21:17:23 +02:00
|
|
|
return reject(NO_CHANNEL);
|
|
|
|
default:
|
2017-11-01 00:23:11 +01:00
|
|
|
return resolve(result[0].claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-04-29 21:17:23 +02:00
|
|
|
logger.error(error);
|
|
|
|
reject(NO_CHANNEL);
|
2017-11-01 00:23:11 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Certificate.getLongChannelIdFromChannelName = function (channelName) {
|
|
|
|
logger.debug(`getLongChannelIdFromChannelName(${channelName})`);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: { name: channelName },
|
|
|
|
order: [['effectiveAmount', 'DESC'], ['height', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2018-04-29 21:17:23 +02:00
|
|
|
return reject(NO_CHANNEL);
|
2017-11-01 00:23:11 +01:00
|
|
|
default:
|
|
|
|
return resolve(result[0].claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-04-29 21:17:23 +02:00
|
|
|
logger.error(error);
|
|
|
|
reject(NO_CHANNEL);
|
2018-02-22 02:02:57 +01:00
|
|
|
});
|
2017-11-21 21:53:43 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-06 00:11:16 +01:00
|
|
|
Certificate.getLongChannelId = function (channelName, channelClaimId) {
|
|
|
|
logger.debug(`getLongChannelId(${channelName}, ${channelClaimId})`);
|
2018-04-29 21:17:23 +02:00
|
|
|
if (isLongChannelId(channelClaimId)) {
|
2017-12-06 00:11:16 +01:00
|
|
|
return this.validateLongChannelId(channelName, channelClaimId);
|
2018-04-29 21:17:23 +02:00
|
|
|
} else if (isShortChannelId(channelClaimId)) {
|
2017-12-06 00:11:16 +01:00
|
|
|
return this.getLongChannelIdFromShortChannelId(channelName, channelClaimId);
|
2017-11-01 00:23:11 +01:00
|
|
|
} else {
|
2018-04-29 21:17:23 +02:00
|
|
|
return this.getLongChannelIdFromChannelName(channelName);
|
2017-11-01 00:23:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-21 22:27:13 +02:00
|
|
|
return Certificate;
|
|
|
|
};
|