Commit 483efdce authored by Cédric Moreau's avatar Cédric Moreau

[fix] #1037 Migrate duniter-common back into duniter core

parent e2fd7c8a
app/cli.js
app/common/*.js
app/common/lib/*.js
app/common/lib/document/*.js
app/lib/blockchain/*.js
app/lib/blockchain/interfaces/*.js
app/lib/computation/*.js
......
"use strict";
module.exports = {
hashf: require('./lib/hashf'),
dos2unix: require('./lib/dos2unix'),
keyring: require('./lib/crypto/keyring'),
base58: require('./lib/crypto/base58'),
rawer: require('./lib/rawer'),
buid: require('./lib/buid'),
document: require('./lib/document'),
constants: require('./lib/constants'),
txunlock: require('./lib/txunlock'),
parsers: require('./lib/parsers')
}
"use strict";
const hashf = require('./hashf');
const BLOCK_UID = /^(0|[1-9]\d{0,18})-[A-F0-9]{64}$/;
const buidFunctions = function(number, hash) {
if (arguments.length === 2) {
return [number, hash].join('-');
}
if (arguments[0]) {
return [arguments[0].number, arguments[0].hash].join('-');
}
return '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855';
};
buidFunctions.fromTS = (line) => line.match(/TS:(.*)/)[1];
buidFunctions.fromIdty = (idty) => this(idty.ts_number, idty.ts_hash);
module.exports = {
format: {
hashf: (value) => hashf(String(value)).toUpperCase(),
isBuid: (value) => {
return (typeof value === 'string') && value.match(BLOCK_UID) ? true : false;
},
buid: buidFunctions,
obuid: (line) => {
let sp = this.buid.fromTS(line).split('-');
return {
number: sp[0],
hash: sp[1]
};
}
}
};
This diff is collapsed.
"use strict"
const assert = require('assert')
const bs58 = require('bs58')
module.exports = {
encode: (bytes) => bs58.encode(bytes),
decode: (data) => new Uint8Array(bs58.decode(data))
};
"use strict";
const nacl = require('tweetnacl');
const base58 = require('./base58');
const seedrandom = require('seedrandom');
const naclBinding = require('naclb');
nacl.util = require('./nacl-util');
const crypto_sign_BYTES = 64;
/**
* Verify a signature against data & public key.
* Return true of false as callback argument.
*/
function verify(rawMsg, rawSig, rawPub) {
const msg = nacl.util.decodeUTF8(rawMsg);
const sig = nacl.util.decodeBase64(rawSig);
const pub = base58.decode(rawPub);
const m = new Uint8Array(crypto_sign_BYTES + msg.length);
const sm = new Uint8Array(crypto_sign_BYTES + msg.length);
let i;
for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
// Call to verification lib...
return naclBinding.verify(m, sm, pub);
}
function Key(pub, sec) {
/*****************************
*
* GENERAL CRYPTO
*
*****************************/
this.publicKey = pub;
this.secretKey = sec;
const rawSec = () => base58.decode(this.secretKey);
this.json = () => { return {
pub: this.publicKey,
sec: this.secretKey
}};
this.sign = (msg) => Promise.resolve(this.signSync(msg));
this.signSync = (msg) => {
const m = nacl.util.decodeUTF8(msg);
const signedMsg = naclBinding.sign(m, rawSec());
const sig = new Uint8Array(crypto_sign_BYTES);
for (let i = 0; i < sig.length; i++) {
sig[i] = signedMsg[i];
}
return nacl.util.encodeBase64(sig);
};
}
function randomKey() {
const byteseed = new Uint8Array(32)
for (let i = 0; i < 32; i++) {
byteseed[i] = Math.floor(seedrandom()() * 255) + 1
}
const keypair = nacl.sign.keyPair.fromSeed(byteseed)
return new Key(
base58.encode(keypair.publicKey),
base58.encode(keypair.secretKey)
)
}
module.exports ={
randomKey,
Key: (pub, sec) => new Key(pub, sec),
verify: verify
};
// Written in 2014-2016 by Dmitry Chestnykh and Devi Mandiri.
// Public domain.
(function(root, f) {
'use strict';
if (typeof module !== 'undefined' && module.exports) module.exports = f();
else if (root.nacl) root.nacl.util = f();
else {
root.nacl = {};
root.nacl.util = f();
}
}(this, function() {
'use strict';
let util = {};
util.decodeUTF8 = function(s) {
let i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
};
util.encodeUTF8 = function(arr) {
let i, s = [];
for (i = 0; i < arr.length; i++) s.push(String.fromCharCode(arr[i]));
return decodeURIComponent(escape(s.join('')));
};
util.encodeBase64 = function(arr) {
if (typeof btoa === 'undefined' || !window) {
return (new Buffer(arr)).toString('base64');
} else {
let i, s = [], len = arr.length;
for (i = 0; i < len; i++) s.push(String.fromCharCode(arr[i]));
return btoa(s.join(''));
}
};
util.decodeBase64 = function(s) {
if (typeof atob === 'undefined' || !window) {
return new Uint8Array(Array.prototype.slice.call(new Buffer(s, 'base64'), 0));
} else {
let i, d = atob(s), b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
}
};
return util;
}));
This diff is collapsed.
"use strict";
const constants = require('../constants');
const regex = require('../regex');
const hashf = require('../hashf');
// Constants
const SIGNED = true
const UNSIGNED = !SIGNED
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('../constants');
const regex = require('../regex');
const hashf = require('../hashf');
// 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() {
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')
}
\ No newline at end of file
"use strict";
const constants = require('../constants');
const regex = require('../regex');
// Constants
const SIGNED = false
const UNSIGNED = !SIGNED
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