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

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

parent 483efdce
"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) {
......@@ -20,7 +18,7 @@ module.exports = {
format: {
hashf: (value) => hashf(String(value)).toUpperCase(),
hashf: (value) => require('../../lib/common-libs').hashf(String(value)),
isBuid: (value) => {
return (typeof value === 'string') && value.match(BLOCK_UID) ? true : false;
......
"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;
}));
......@@ -2,7 +2,6 @@
const _ = require('underscore')
const constants = require('../constants');
const regex = require('../regex');
const hashf = require('../hashf');
const Transaction = require('./transaction');
// Constants
......@@ -151,6 +150,7 @@ module.exports = class Block {
static getHash(json) {
const raw = Block.toRAWHashAndNonce(json)
const hashf = require('../../../lib/common-libs').hashf
return hashf(raw).toUpperCase()
}
......
"use strict";
const constants = require('../constants');
const regex = require('../regex');
const hashf = require('../hashf');
// Constants
const SIGNED = true
......
"use strict";
const constants = require('../constants');
const regex = require('../regex');
const hashf = require('../hashf');
// Constants
const SIGNED = true
......@@ -74,6 +73,7 @@ module.exports = class Identity {
*/
getTargetHash() {
const hashf = require('../../../lib/common-libs').hashf
return hashf(this.uid + this.buid + this.pubkey).toUpperCase();
}
......
"use strict";
const constants = require('../constants');
const regex = require('../regex');
const hashf = require('../hashf');
const hashf = require('../../../lib/common-libs').hashf
// Constants
const SIGNED = false
......
"use strict";
const util = require('util');
const stream = require('stream');
module.exports = function (str) {
if (str)
return dos2unix(str);
else
return new Dos2UnixStream();
};
const dos2unix = (str) => str.replace(/\r\n/g, '\n');
function Dos2UnixStream () {
stream.Transform.apply(this);
this._write = function (str, enc, done) {
this.push(dos2unix(str.toString()));
done();
}
}
util.inherits(Dos2UnixStream, stream.Transform);
"use strict";
module.exports = function (str){
return require("crypto")
.createHash("sha256")
.update(str)
.digest("hex");
};
"use strict";
const util = require('util');
const stream = require('stream');
const hashf = require('../hashf');
const hashf = require('../../../lib/common-libs').hashf
const constants = require('../constants');
module.exports = GenericParser;
......
......@@ -2,8 +2,8 @@
const util = require('util');
const GenericParser = require('./GenericParser');
const Block = require('../../../../app/common/lib/document/block');
const hashf = require('../../../../app/common/lib/hashf');
const rawer = require('../../../../app/common/lib/rawer');
const hashf = require('../../../../app/lib/common-libs').hashf
const rawer = require('../../../../app/lib/common-libs').rawer
const constants = require('../../../../app/common/lib/constants');
module.exports = BlockParser;
......
"use strict";
const GenericParser = require('./GenericParser');
const util = require('util');
const rawer = require('../../../../app/common/lib/rawer');
const rawer = require('../../../../app/lib/common-libs').rawer
const constants = require('../../../../app/common/lib/constants');
module.exports = CertificationParser;
......
"use strict";
const GenericParser = require('./GenericParser');
const util = require('util');
const rawer = require('../../../../app/common/lib/rawer');
const hashf = require('../../../../app/common/lib/hashf');
const rawer = require('../../../../app/lib/common-libs').rawer
const hashf = require('../../../../app/lib/common-libs').hashf
const constants = require('../../../../app/common/lib/constants');
module.exports = IdentityParser;
......
"use strict";
const GenericParser = require('./GenericParser');
const ucp = require('../buid');
const rawer = require('../rawer');
const rawer = require('../../../lib/common-libs/index').rawer
const util = require('util');
const constants = require('../constants');
......
"use strict";
const GenericParser = require('./GenericParser');
const rawer = require('../rawer');
const rawer = require('../../../lib/common-libs/index').rawer
const util = require('util');
const constants = require('../constants');
......
"use strict";
const GenericParser = require('./GenericParser');
const util = require('util');
const rawer = require('../rawer');
const hashf = require('../hashf');
const rawer = require('../../../lib/common-libs').rawer
const hashf = require('../../../lib/common-libs').hashf
const constants = require('../constants');
module.exports = RevocationParser;
......
"use strict";
const GenericParser = require('./GenericParser');
const rawer = require('../rawer');
const rawer = require('../../../lib/common-libs/index').rawer
const constants = require('../constants');
const util = require('util');
......
"use strict";
const dos2unix = require('./dos2unix');
const document = require('./document');
const DOCUMENTS_VERSION = 10;
const SIGNED = false
const UNSIGNED = true
module.exports = new function() {
this.getOfficialIdentity = (json, withSig) => {
return document.Identity.toRAW(json, withSig !== false) // Defaut with sig
};
this.getOfficialCertification = (json) => {
let raw = getNormalHeader('Certification', json);
raw += "IdtyIssuer: " + json.idty_issuer + '\n';
raw += "IdtyUniqueID: " + json.idty_uid + '\n';
raw += "IdtyTimestamp: " + json.idty_buid + '\n';
raw += "IdtySignature: " + json.idty_sig + '\n';
raw += "CertTimestamp: " + json.buid + '\n';
if (json.sig) {
raw += json.sig + '\n';
}
return dos2unix(raw);
};
this.getOfficialRevocation = (json) => {
let raw = getNormalHeader('Revocation', json);
raw += "IdtyUniqueID: " + json.uid + '\n';
raw += "IdtyTimestamp: " + json.buid + '\n';
raw += "IdtySignature: " + json.sig + '\n';
if (json.revocation) {
raw += json.revocation + '\n';
}
return dos2unix(raw);
};
this.getPeerWithoutSignature = (json) => document.Peer.fromJSON(json).getRawUnsigned()
this.getPeer = (json) => document.Peer.fromJSON(json).getRaw()
this.getMembershipWithoutSignature = (json) => {
return document.Membership.toRAW(json)
};
this.getMembership = (json) => {
return dos2unix(signed(this.getMembershipWithoutSignature(json), json));
};
this.getBlockInnerPart = (json) => {
return document.Block.toRAWInnerPart(json)
};
this.getBlockWithInnerHashAndNonce = (json) => {
return document.Block.toRAWinnerPartWithHashAndNonce(json)
};
this.getBlockInnerHashAndNonce = (json) => {
return document.Block.toRAWHashAndNonce(json, UNSIGNED)
};
this.getBlockInnerHashAndNonceWithSignature = (json) => {
return document.Block.toRAWHashAndNonce(json, SIGNED)
};
this.getBlock = (json) => {
return dos2unix(signed(this.getBlockWithInnerHashAndNonce(json), json));
};
this.getTransaction = (json) => {
return document.Transaction.toRAW(json)
};
this.getCompactTransaction = (json) => {
return document.Transaction.getCompactTransaction(json)
};
let getNormalHeader = (doctype, json) => {
let raw = "";
raw += "Version: " + (json.version || DOCUMENTS_VERSION) + "\n";
raw += "Type: " + doctype + "\n";
raw += "Currency: " + json.currency + "\n";
raw += "Issuer: " + json.issuer + "\n";
return raw;
};
let signed = (raw, json) => {
raw += json.signature + '\n';
return raw;
};
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment