spee.ch/models/certificate.js

202 lines
4.9 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
2017-11-04 01:10:08 +01:00
const { returnShortId } = require('../helpers/sequelizeHelpers.js');
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: {
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: {
type : DECIMAL(19, 8),
2017-08-21 22:27:13 +02:00
default: null,
},
hasSignature: {
type : BOOLEAN,
default: null,
},
height: {
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: {
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,
},
});
};
Certificate.getShortChannelIdFromLongChannelId = function (longChannelId, channelName) {
logger.debug(`finding short channel id for ${channelName}:${longChannelId}`);
return new Promise((resolve, reject) => {
this
.findAll({
where: {name: channelName},
order: [['height', 'ASC']],
})
.then(result => {
switch (result.length) {
case 0:
2017-11-14 23:52:20 +01:00
throw new Error('No channel(s) found with that channel name');
default:
2017-11-04 01:10:08 +01:00
return resolve(returnShortId(result, longChannelId));
}
})
.catch(error => {
reject(error);
});
});
};
2017-12-06 00:11:16 +01:00
Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelClaimId) {
2017-11-01 00:23:11 +01:00
return new Promise((resolve, reject) => {
this
.findAll({
where: {
name : channelName,
claimId: {
2017-12-06 00:11:16 +01:00
$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:
2017-12-06 02:08:34 +01:00
return resolve(null);
2017-11-01 00:23:11 +01:00
default: // note results must be sorted
return resolve(result[0].claimId);
}
})
.catch(error => {
reject(error);
});
});
};
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:
2017-12-06 02:08:34 +01:00
return resolve(null);
2017-11-01 00:23:11 +01:00
default:
return resolve(result[0].claimId);
}
})
.catch(error => {
reject(error);
});
});
};
2017-11-21 21:53:43 +01:00
Certificate.validateLongChannelId = function (name, claimId) {
return new Promise((resolve, reject) => {
this.findOne({
where: {name, claimId},
})
.then(result => {
2017-11-28 03:21:50 +01:00
if (!result) {
2017-12-06 02:08:34 +01:00
return resolve(null);
2017-11-28 03:21:50 +01:00
};
resolve(claimId);
2017-11-21 21:53:43 +01:00
})
.catch(error => {
reject(error);
});
});
};
2017-12-06 00:11:16 +01:00
Certificate.getLongChannelId = function (channelName, channelClaimId) {
logger.debug(`getLongChannelId(${channelName}, ${channelClaimId})`);
if (channelClaimId && (channelClaimId.length === 40)) { // if a full channel id is provided
return this.validateLongChannelId(channelName, channelClaimId);
} else if (channelClaimId && channelClaimId.length < 40) { // if a short channel id is provided
return this.getLongChannelIdFromShortChannelId(channelName, channelClaimId);
2017-11-01 00:23:11 +01:00
} else {
return this.getLongChannelIdFromChannelName(channelName); // if no channel id provided
}
};
2017-08-21 22:27:13 +02:00
return Certificate;
};