Commit b8c7046d authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] #1037 Migrating ex duniter-common JS files to TS (3/3)

parent 375fef37
"use strict";
const constants = require('../../../../app/lib/common-libs/constants').CommonConstants
module.exports = class Certification {
constructor(
version,
currency,
issuer,
blockstamp,
idty_issuer,
idty_uid,
idty_buid,
idty_sig,
signature) {
this.version = version
this.currency = currency
this.issuer = issuer
this.blockstamp = blockstamp
this.idty_issuer = idty_issuer
this.idty_uid = idty_uid
this.idty_buid = idty_buid
this.idty_sig = idty_sig
this.signature = signature
}
/**
* Aliases
*/
get buid() {
return this.blockstamp
}
set buid(buid) {
this.blockstamp = buid
}
get sig() {
return this.signature
}
set sig(sig) {
this.signature = sig
}
get pubkey() {
return this.issuer
}
set pubkey(pubkey) {
this.issuer = pubkey
}
get to() {
return this.idty_issuer
}
set to(to) {
this.idty_issuer = to
}
get blockNumber() {
if (!this.blockstamp) {
return null
}
return parseInt(this.blockstamp.split('-')[0])
}
get block_number() {
return this.blockNumber
}
get blockHash() {
if (!this.blockstamp) {
return null
}
return this.blockstamp.split('-')[1]
}
get block_hash() {
return this.blockHash
}
/**
* Methods
*/
getRaw() {
return Certification.toRAW(this);
}
inline() {
return [this.pubkey, this.to, this.block_number, this.sig].join(':')
}
/**
* Statics
*/
static fromJSON(json) {
const buid = [json.block_number, json.block_hash].join('-')
return new Certification(
json.version || constants.DOCUMENTS_VERSION,
json.currency,
json.issuer || json.pubkey || json.from,
json.blockstamp || json.buid || buid,
json.idty_issuer || json.to,
json.idty_uid,
json.idty_buid,
json.idty_sig,
json.signature || json.sig
)
}
static toRAW(json, withSig = true) {
const cert = Certification.fromJSON(json)
let raw = "";
raw += "Version: " + cert.version + "\n";
raw += "Type: Certification\n";
raw += "Currency: " + cert.currency + "\n";
raw += "Issuer: " + cert.issuer + "\n";
raw += "IdtyIssuer: " + cert.idty_issuer + '\n';
raw += "IdtyUniqueID: " + cert.idty_uid + '\n';
raw += "IdtyTimestamp: " + cert.idty_buid + '\n';
raw += "IdtySignature: " + cert.idty_sig + '\n';
raw += "CertTimestamp: " + cert.buid + '\n';
if (cert.sig && withSig) {
raw += cert.sig + '\n'
}
return raw
}
static fromInline(inline) {
const sp = inline.split(':')
return Certification.fromJSON({
pubkey: sp[0],
to: sp[1],
block_number: parseInt(sp[2]),
sig: sp[3]
})
}
}
"use strict";
const constants = require('../../../../app/lib/common-libs/constants').CommonConstants
// Constants
const SIGNED = true
const UNSIGNED = !SIGNED
module.exports = class Identity {
constructor(
version,
currency,
pubkey,
uid,
blockstamp,
signature,
revoked,
revoked_on,
revocation_sig) {
this.version = version || constants.DOCUMENTS_VERSION
this.currency = currency
this.pubkey = pubkey
this.uid = uid
this.blockstamp = blockstamp
this.signature = signature
this.revoked = revoked
this.revoked_on = revoked_on
this.revocation_sig = revocation_sig
this.certs = []
}
/**
* Aliases
*/
get buid() {
return this.blockstamp
}
set buid(buid) {
this.blockstamp = buid
}
get sig() {
return this.signature
}
set sig(sig) {
this.signature = sig
}
get blockNumber() {
if (!this.blockstamp) {
return null
}
return parseInt(this.blockstamp.split('-')[0])
}
get blockHash() {
if (!this.blockstamp) {
return null
}
return this.blockstamp.split('-')[1]
}
get hash() {
return this.getTargetHash()
}
/**
* Methods
*/
getTargetHash() {
const hashf = require('../../../lib/common-libs').hashf
return hashf(this.uid + this.buid + this.pubkey).toUpperCase();
}
inline() {
return [this.pubkey, this.sig, this.buid, this.uid].join(':');
}
rawWithoutSig() {
return Identity.toRAW(this, UNSIGNED)
}
json() {
const others = [];
this.certs.forEach((cert) => {
others.push({
"pubkey": cert.from,
"meta": {
"block_number": cert.block_number,
"block_hash": cert.block_hash
},
"uids": cert.uids,
"isMember": cert.isMember,
"wasMember": cert.wasMember,
"signature": cert.sig
});
});
const uids = [{
"uid": this.uid,
"meta": {
"timestamp": this.buid
},
"revoked": this.revoked,
"revoked_on": this.revoked_on,
"revocation_sig": this.revocation_sig,
"self": this.sig,
"others": others
}];
const signed = [];
this.signed.forEach((cert) => {
signed.push({
"uid": cert.idty.uid,
"pubkey": cert.idty.pubkey,
"meta": {
"timestamp": cert.idty.buid
},
"cert_time": {
"block": cert.block_number,
"block_hash": cert.block_hash
},
"isMember": cert.idty.member,
"wasMember": cert.idty.wasMember,
"signature": cert.sig
});
});
return {
"pubkey": this.pubkey,
"uids": uids,
"signed": signed
}
}
/**
* Statics
*/
static fromJSON(json) {
return new Identity(
json.version || constants.DOCUMENTS_VERSION,
json.currency,
json.pubkey || json.issuer,
json.uid,
json.blockstamp || json.buid,
json.signature || json.sig,
json.revoked,
json.revoked_on,
json.revocation_sig)
}
static toRAW(json, withSig) {
const idty = Identity.fromJSON(json)
let raw = ""
raw += "Version: " + idty.version + "\n"
raw += "Type: Identity\n"
raw += "Currency: " + idty.currency + "\n"
raw += "Issuer: " + (idty.issuer || idty.pubkey) + "\n"
raw += "UniqueID: " + idty.uid + '\n'
raw += "Timestamp: " + idty.buid + '\n'
if (idty.sig && withSig) {
raw += idty.sig + '\n'
}
return raw
}
static fromInline(inline) {
const sp = inline.split(':')
return Identity.fromJSON({
pubkey: sp[0],
sig: sp[1],
buid: sp[2],
uid: sp[3]
})
}
static revocationFromInline(inline) {
const sp = inline.split(':')
return {
pubkey: sp[0],
sig: sp[1]
}
}
}
"use strict";
module.exports = {
Peer: require('./peer'),
Block: require('./block'),
Membership: require('./membership'),
Transaction: require('./transaction'),
Identity: require('./identity'),
Certification: require('./certification')
Transaction: require('./transaction')
}
\ No newline at end of file
"use strict";
const constants = require('../../../../app/lib/common-libs/constants').CommonConstants
module.exports = class Membership {
constructor(
version,
currency,
issuer,
membership,
userid,
blockstamp,
certts,
signature) {
this.version = version
this.currency = currency
this.issuer = issuer
this.membership = membership
this.userid = userid
this.blockstamp = blockstamp
this.certts = certts
this.signature = signature
}
/**
* Aliases
*/
get number() {
return this.blockNumber
}
get fpr() {
return this.blockHash
}
get block() {
return this.blockstamp
}
get blockNumber() {
if (!this.blockstamp) {
return null
}
return parseInt(this.blockstamp.split('-')[0])
}
get blockHash() {
if (!this.blockstamp) {
return null
}
return this.blockstamp.split('-')[1]
}
/**
* Methods
*/
getRaw() {
return Membership.toRAW(this)
}
inline() {
return [
this.issuer,
this.signature,
this.blockstamp,
this.certts,
this.userid
].join(':')
}
/**
* Statics
*/
static toRAW(json) {
const ms = Membership.fromJSON(json)
let raw = ""
raw += "Version: " + ms.version + "\n"
raw += "Type: Membership\n"
raw += "Currency: " + ms.currency + "\n"
raw += "Issuer: " + ms.issuer + "\n"
raw += "Block: " + ms.block + "\n"
raw += "Membership: " + ms.membership + "\n"
if (ms.userid)
raw += "UserID: " + ms.userid + "\n"
if (ms.certts)
raw += "CertTS: " + ms.certts + "\n"
return raw
}
static fromJSON(json) {
return new Membership(
json.version || constants.DOCUMENTS_VERSION,
json.currency,
json.issuer,
json.membership,
json.userid,
json.blockstamp || json.block,
json.certts,
json.signature)
}
static fromInline(inline, type, currency) {
const sp = inline.split(':')
return Membership.fromJSON({
version: constants.DOCUMENTS_VERSION,
currency: currency,
issuer: sp[0],
membership: type,
userid: sp[4],
blockstamp: sp[2],
certts: sp[3],
signature: sp[1]
})
}
}
"use strict";
const constants = require('../../../../app/lib/common-libs/constants').CommonConstants
// Constants
const SIGNED = false
const UNSIGNED = !SIGNED
module.exports = class Peer {
constructor(version, currency, pubkey, endpoints, blockstamp, signature) {
this.version = version
this.currency = currency
this.pubkey = pubkey
this.endpoints = endpoints || [];
this.blockstamp = blockstamp;
this.signature = signature;
}
/**
* Aliases
*/
get pub() {
return this.pubkey
}
set pub(pub) {
this.pubkey = pub
}
get block() {
return this.blockstamp
}
get statics() {
return {
peerize: (json) => Peer.fromJSON(json),
fromJSON: (json) => Peer.fromJSON(json)
}
}
/**
* Methods
*/
getDns() {
const bma = this.getBMA();
return bma.dns ? bma.dns : null;
}
getIPv4() {
const bma = this.getBMA();
return bma.ipv4 ? bma.ipv4 : null;
}
getIPv6() {
const bma = this.getBMA();
return bma.ipv6 ? bma.ipv6 : null;
}
getPort() {
const bma = this.getBMA();
return bma.port ? bma.port : null;
}
getBMA() {
let bma = null;
this.endpoints.forEach((ep) => {
const matches = !bma && ep.match(constants.BMA_REGEXP);
if (matches) {
bma = {
"dns": matches[2] || '',
"ipv4": matches[4] || '',
"ipv6": matches[6] || '',
"port": matches[8] || 9101
};
}
});
return bma || {};
}
getURL() {
const bma = this.getBMA();
let base = this.getHostPreferDNS();
if(bma.port)
base += ':' + bma.port;
return base;
}
getHostPreferDNS() {
const bma = this.getBMA();
return (bma.dns ? bma.dns :
(bma.ipv4 ? bma.ipv4 :
(bma.ipv6 ? bma.ipv6 : '')));
}
getRaw() {
return Peer.toRAW(this, SIGNED)
}
getRawUnsigned() {
return Peer.toRAW(this, UNSIGNED)
}
endpointSum() {
return this.endpoints.join('_')
}
blockNumber() {
return this.blockstamp.match(/^(\d+)-/)[1]
}
static fromJSON(json) {
// Returns a new Peer only if `json` is defined and not null
if (!json) return null
return new Peer(
json.version || constants.DOCUMENTS_VERSION,
json.currency,
json.pubkey || json.pub || "",
json.endpoints,
json.blockstamp || json.block,
json.signature
)
}
static toRAW(json, unsigned) {
const p = Peer.fromJSON(json)
let raw = ""
raw += "Version: " + p.version + "\n"
raw += "Type: Peer\n"
raw += "Currency: " + p.currency + "\n"
raw += "PublicKey: " + p.pubkey + "\n"
raw += "Block: " + p.blockstamp + "\n"
raw += "Endpoints:" + "\n"
for(const ep of p.endpoints) {
raw += ep + "\n"
}
if (!unsigned) {
raw += json.signature + '\n'
}
return raw
}