Initial commit

This commit is contained in:
AlphaX-Projects
2021-12-25 14:39:47 +01:00
commit 2f823927a5
3687 changed files with 37381 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
// Generated by CoffeeScript 1.8.0
// == Changed for ES6 modules == //
//(function() {
//var ALPHABET, ALPHABET_MAP, Base58, i;
//const Base58 = (typeof module !== "undefined" && module !== null ? module.exports : void 0) || (window.Base58 = {});
const Base58 = {};
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const ALPHABET_MAP = {};
let i = 0;
while (i < ALPHABET.length) {
ALPHABET_MAP[ALPHABET.charAt(i)] = i;
i++;
}
Base58.encode = function(buffer) {
buffer = new Uint8Array(buffer);
var carry, digits, j;
if (buffer.length === 0) {
return "";
}
i = void 0;
j = void 0;
digits = [0];
i = 0;
while (i < buffer.length) {
j = 0;
while (j < digits.length) {
digits[j] <<= 8;
j++;
}
digits[0] += buffer[i];
carry = 0;
j = 0;
while (j < digits.length) {
digits[j] += carry;
carry = (digits[j] / 58) | 0;
digits[j] %= 58;
++j;
}
while (carry) {
digits.push(carry % 58);
carry = (carry / 58) | 0;
}
i++;
}
i = 0;
while (buffer[i] === 0 && i < buffer.length - 1) {
digits.push(0);
i++;
}
return digits.reverse().map(function(digit) {
return ALPHABET[digit];
}).join("");
};
Base58.decode = function(string) {
var bytes, c, carry, j;
if (string.length === 0) {
return new (typeof Uint8Array !== "undefined" && Uint8Array !== null ? Uint8Array : Buffer)(0);
}
i = void 0;
j = void 0;
bytes = [0];
i = 0;
while (i < string.length) {
c = string[i];
if (!(c in ALPHABET_MAP)) {
throw "Base58.decode received unacceptable input. Character '" + c + "' is not in the Base58 alphabet.";
}
j = 0;
while (j < bytes.length) {
bytes[j] *= 58;
j++;
}
bytes[0] += ALPHABET_MAP[c];
carry = 0;
j = 0;
while (j < bytes.length) {
bytes[j] += carry;
carry = bytes[j] >> 8;
bytes[j] &= 0xff;
++j;
}
while (carry) {
bytes.push(carry & 0xff);
carry >>= 8;
}
i++;
}
i = 0;
while (string[i] === "1" && i < string.length - 1) {
bytes.push(0);
i++;
}
return new (typeof Uint8Array !== "undefined" && Uint8Array !== null ? Uint8Array : Buffer)(bytes.reverse());
};
// == Changed for ES6 modules == //
//}).call(this);
export default Base58;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,180 @@
// "Generated from Java with JSweet 1.0.0 - http://www.jsweet.org";
// BAD IMPLEMENTATION. BROKEN, BUT MUST KEEP CAUSE OF NETWORK
//const RIPEMD160 = (function () {
// == Convert to ES6 module for export == //
const RIPEMD160 = (function () {
function RIPEMD160() {
this.MDbuf = [];
this.MDbuf[0] = 1732584193;
this.MDbuf[1] = -271733879;
this.MDbuf[2] = -1732584194;
this.MDbuf[3] = 271733878;
this.MDbuf[4] = -1009589776;
this.working = new Int32Array(16);
this.working_ptr = 0;
this.msglen = 0;
}
RIPEMD160.prototype.reset = function () {
this.MDbuf = [];
this.MDbuf[0] = 1732584193;
this.MDbuf[1] = -271733879;
this.MDbuf[2] = -1732584194;
this.MDbuf[3] = 271733878;
this.MDbuf[4] = -1009589776;
this.working = new Int32Array(16);
this.working_ptr = 0;
this.msglen = 0;
};
RIPEMD160.prototype.compress = function (X) {
var index = 0;
var a;
var b;
var c;
var d;
var e;
var A;
var B;
var C;
var D;
var E;
var temp;
var s;
A = a = this.MDbuf[0];
B = b = this.MDbuf[1];
C = c = this.MDbuf[2];
D = d = this.MDbuf[3];
E = e = this.MDbuf[4];
for (; index < 16; index++) {
temp = a + (b ^ c ^ d) + X[RIPEMD160.IndexArray[0][index]];
a = e;
e = d;
d = (c << 10) | (c >>> 22);
c = b;
s = RIPEMD160.ArgArray[0][index];
b = ((temp << s) | (temp >>> (32 - s))) + a;
temp = A + (B ^ (C | ~D)) + X[RIPEMD160.IndexArray[1][index]] + 1352829926;
A = E;
E = D;
D = (C << 10) | (C >>> 22);
C = B;
s = RIPEMD160.ArgArray[1][index];
B = ((temp << s) | (temp >>> (32 - s))) + A;
}
for (; index < 32; index++) {
temp = a + ((b & c) | (~b & d)) + X[RIPEMD160.IndexArray[0][index]] + 1518500249;
a = e;
e = d;
d = (c << 10) | (c >>> 22);
c = b;
s = RIPEMD160.ArgArray[0][index];
b = ((temp << s) | (temp >>> (32 - s))) + a;
temp = A + ((B & D) | (C & ~D)) + X[RIPEMD160.IndexArray[1][index]] + 1548603684;
A = E;
E = D;
D = (C << 10) | (C >>> 22);
C = B;
s = RIPEMD160.ArgArray[1][index];
B = ((temp << s) | (temp >>> (32 - s))) + A;
}
for (; index < 48; index++) {
temp = a + ((b | ~c) ^ d) + X[RIPEMD160.IndexArray[0][index]] + 1859775393;
a = e;
e = d;
d = (c << 10) | (c >>> 22);
c = b;
s = RIPEMD160.ArgArray[0][index];
b = ((temp << s) | (temp >>> (32 - s))) + a;
temp = A + ((B | ~C) ^ D) + X[RIPEMD160.IndexArray[1][index]] + 1836072691;
A = E;
E = D;
D = (C << 10) | (C >>> 22);
C = B;
s = RIPEMD160.ArgArray[1][index];
B = ((temp << s) | (temp >>> (32 - s))) + A;
}
for (; index < 64; index++) {
temp = a + ((b & d) | (c & ~d)) + X[RIPEMD160.IndexArray[0][index]] + -1894007588;
a = e;
e = d;
d = (c << 10) | (c >>> 22);
c = b;
s = RIPEMD160.ArgArray[0][index];
b = ((temp << s) | (temp >>> (32 - s))) + a;
temp = A + ((B & C) | (~B & D)) + X[RIPEMD160.IndexArray[1][index]] + 2053994217;
A = E;
E = D;
D = (C << 10) | (C >>> 22);
C = B;
s = RIPEMD160.ArgArray[1][index];
B = ((temp << s) | (temp >>> (32 - s))) + A;
}
for (; index < 80; index++) {
temp = a + (b ^ (c | ~d)) + X[RIPEMD160.IndexArray[0][index]] + -1454113458;
a = e;
e = d;
d = (c << 10) | (c >>> 22);
c = b;
s = RIPEMD160.ArgArray[0][index];
b = ((temp << s) | (temp >>> (32 - s))) + a;
temp = A + (B ^ C ^ D) + X[RIPEMD160.IndexArray[1][index]];
A = E;
E = D;
D = (C << 10) | (C >>> 22);
C = B;
s = RIPEMD160.ArgArray[1][index];
B = ((temp << s) | (temp >>> (32 - s))) + A;
}
D += c + this.MDbuf[1];
this.MDbuf[1] = this.MDbuf[2] + d + E;
this.MDbuf[2] = this.MDbuf[3] + e + A;
this.MDbuf[3] = this.MDbuf[4] + a + B;
this.MDbuf[4] = this.MDbuf[0] + b + C;
this.MDbuf[0] = D;
};
RIPEMD160.prototype.MDfinish = function (array, lswlen, mswlen) {
var X = array;
X[(lswlen >> 2) & 15] ^= 1 << (((lswlen & 3) << 3) + 7);
if (((lswlen & 63) > 55)) {
this.compress(X);
for (var i = 0; i < 14; i++) {
X[i] = 0;
}
}
X[14] = lswlen << 3;
X[15] = (lswlen >> 29) | (mswlen << 3);
this.compress(X);
};
RIPEMD160.prototype.update = function (input) {
for (var i = 0; i < input.length; i++) {
this.working[this.working_ptr >> 2] ^= input[i] << ((this.working_ptr & 3) << 3);
this.working_ptr++;
if ((this.working_ptr == 64)) {
this.compress(this.working);
for (var j = 0; j < 16; j++) {
this.working[j] = 0;
}
this.working_ptr = 0;
}
}
this.msglen += input.length;
};
RIPEMD160.prototype.digestBin = function () {
this.MDfinish(this.working, this.msglen, 0);
//var res = new Int8Array();
var res = [];
for (var i = 0; i < 20; i++) {
res[i] = ((this.MDbuf[i >> 2] >>> ((i & 3) << 3)) & 255);
}
return new Uint8Array(res);
};
RIPEMD160.prototype.digest = function (input) {
this.update(new Int8Array(input));
return this.digestBin();
};
RIPEMD160.ArgArray = [[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6], [8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11]];
RIPEMD160.IndexArray = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13], [5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]];
return RIPEMD160;
})();
export default RIPEMD160

View File

@@ -0,0 +1,3 @@
export { default as Base58 } from './Base58.js'
export { default as utils } from './utils.js'

View File

@@ -0,0 +1,264 @@
/*
* ed2curve: convert Ed25519 signing key pair into Curve25519
* key pair suitable for Diffie-Hellman key exchange.
*
* Written by Dmitry Chestnykh in 2014. Public domain.
*/
/* jshint newcap: false */
/*
Change to es6 import/export
*/
import nacl from './nacl-fast.js'
// (function(root, f) {
// 'use strict';
// if (typeof module !== 'undefined' && module.exports) module.exports = f(require('tweetnacl'));
// else root.ed2curve = f(root.nacl);
// }(this, function(nacl) {
// 'use strict';
// if (!nacl) throw new Error('tweetnacl not loaded');
// -- Operations copied from TweetNaCl.js. --
var gf = function(init) {
var i, r = new Float64Array(16);
if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
return r;
};
var gf0 = gf(),
gf1 = gf([1]),
D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),
I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
function car25519(o) {
var c;
var i;
for (i = 0; i < 16; i++) {
o[i] += 65536;
c = Math.floor(o[i] / 65536);
o[(i+1)*(i<15?1:0)] += c - 1 + 37 * (c-1) * (i===15?1:0);
o[i] -= (c * 65536);
}
}
function sel25519(p, q, b) {
var t, c = ~(b-1);
for (var i = 0; i < 16; i++) {
t = c & (p[i] ^ q[i]);
p[i] ^= t;
q[i] ^= t;
}
}
function unpack25519(o, n) {
var i;
for (i = 0; i < 16; i++) o[i] = n[2*i] + (n[2*i+1] << 8);
o[15] &= 0x7fff;
}
// addition
function A(o, a, b) {
var i;
for (i = 0; i < 16; i++) o[i] = (a[i] + b[i])|0;
}
// subtraction
function Z(o, a, b) {
var i;
for (i = 0; i < 16; i++) o[i] = (a[i] - b[i])|0;
}
// multiplication
function M(o, a, b) {
var i, j, t = new Float64Array(31);
for (i = 0; i < 31; i++) t[i] = 0;
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
t[i+j] += a[i] * b[j];
}
}
for (i = 0; i < 15; i++) {
t[i] += 38 * t[i+16];
}
for (i = 0; i < 16; i++) o[i] = t[i];
car25519(o);
car25519(o);
}
// squaring
function S(o, a) {
M(o, a, a);
}
// inversion
function inv25519(o, i) {
var c = gf();
var a;
for (a = 0; a < 16; a++) c[a] = i[a];
for (a = 253; a >= 0; a--) {
S(c, c);
if(a !== 2 && a !== 4) M(c, c, i);
}
for (a = 0; a < 16; a++) o[a] = c[a];
}
function pack25519(o, n) {
var i, j, b;
var m = gf(), t = gf();
for (i = 0; i < 16; i++) t[i] = n[i];
car25519(t);
car25519(t);
car25519(t);
for (j = 0; j < 2; j++) {
m[0] = t[0] - 0xffed;
for (i = 1; i < 15; i++) {
m[i] = t[i] - 0xffff - ((m[i-1]>>16) & 1);
m[i-1] &= 0xffff;
}
m[15] = t[15] - 0x7fff - ((m[14]>>16) & 1);
b = (m[15]>>16) & 1;
m[14] &= 0xffff;
sel25519(t, m, 1-b);
}
for (i = 0; i < 16; i++) {
o[2*i] = t[i] & 0xff;
o[2*i+1] = t[i] >> 8;
}
}
function par25519(a) {
var d = new Uint8Array(32);
pack25519(d, a);
return d[0] & 1;
}
function vn(x, xi, y, yi, n) {
var i, d = 0;
for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i];
return (1 & ((d - 1) >>> 8)) - 1;
}
function crypto_verify_32(x, xi, y, yi) {
return vn(x, xi, y, yi, 32);
}
function neq25519(a, b) {
var c = new Uint8Array(32), d = new Uint8Array(32);
pack25519(c, a);
pack25519(d, b);
return crypto_verify_32(c, 0, d, 0);
}
function pow2523(o, i) {
var c = gf();
var a;
for (a = 0; a < 16; a++) c[a] = i[a];
for (a = 250; a >= 0; a--) {
S(c, c);
if (a !== 1) M(c, c, i);
}
for (a = 0; a < 16; a++) o[a] = c[a];
}
function set25519(r, a) {
var i;
for (i = 0; i < 16; i++) r[i] = a[i] | 0;
}
function unpackneg(r, p) {
var t = gf(), chk = gf(), num = gf(),
den = gf(), den2 = gf(), den4 = gf(),
den6 = gf();
set25519(r[2], gf1);
unpack25519(r[1], p);
S(num, r[1]);
M(den, num, D);
Z(num, num, r[2]);
A(den, r[2], den);
S(den2, den);
S(den4, den2);
M(den6, den4, den2);
M(t, den6, num);
M(t, t, den);
pow2523(t, t);
M(t, t, num);
M(t, t, den);
M(t, t, den);
M(r[0], t, den);
S(chk, r[0]);
M(chk, chk, den);
if (neq25519(chk, num)) M(r[0], r[0], I);
S(chk, r[0]);
M(chk, chk, den);
if (neq25519(chk, num)) return -1;
if (par25519(r[0]) === (p[31] >> 7)) Z(r[0], gf0, r[0]);
M(r[3], r[0], r[1]);
return 0;
}
// ----
// Converts Ed25519 public key to Curve25519 public key.
// montgomeryX = (edwardsY + 1)*inverse(1 - edwardsY) mod p
function convertPublicKey(pk) {
var z = new Uint8Array(32),
q = [gf(), gf(), gf(), gf()],
a = gf(), b = gf();
if (unpackneg(q, pk)) return null; // reject invalid key
var y = q[1];
A(a, gf1, y);
Z(b, gf1, y);
inv25519(b, b);
M(a, a, b);
pack25519(z, a);
return z;
}
// Converts Ed25519 secret key to Curve25519 secret key.
function convertSecretKey(sk) {
var d = new Uint8Array(64), o = new Uint8Array(32), i;
nacl.lowlevel.crypto_hash(d, sk, 32);
d[0] &= 248;
d[31] &= 127;
d[31] |= 64;
for (i = 0; i < 32; i++) o[i] = d[i];
for (i = 0; i < 64; i++) d[i] = 0;
return o;
}
function convertKeyPair(edKeyPair) {
var publicKey = convertPublicKey(edKeyPair.publicKey);
if (!publicKey) return null;
return {
publicKey: publicKey,
secretKey: convertSecretKey(edKeyPair.secretKey)
};
}
// return {
// convertPublicKey: convertPublicKey,
// convertSecretKey: convertSecretKey,
// convertKeyPair: convertKeyPair,
// };
export default {
convertPublicKey: convertPublicKey,
convertSecretKey: convertSecretKey,
convertKeyPair: convertKeyPair,
}
// }));

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,336 @@
// QORA
const TYPES = {
GENESIS_TRANSACTION: 1,
PAYMENT_TRANSACTION: 2,
REGISTER_NAME_TRANSACTION: 3,
UPDATE_NAME_TRANSACTION: 4,
SELL_NAME_TRANSACTION: 5,
CANCEL_SELL_NAME_TRANSACTION: 6,
BUY_NAME_TRANSACTION: 7,
CREATE_POLL_TRANSACTION: 8,
VOTE_ON_POLL_TRANSACTION: 9,
ARBITRARY_TRANSACTION: 10,
ISSUE_ASSET_TRANSACTION: 11,
TRANSFER_ASSET_TRANSACTION: 12,
CREATE_ORDER_TRANSACTION: 13,
CANCEL_ORDER_TRANSACTION: 14,
MULTI_PAYMENT_TRANSACTION: 15,
DEPLOY_AT_TRANSACTION: 16,
MESSAGE_TRANSACTION: 17
};
function getKeyPairFromSeed(seed, returnBase58)
{
if(typeof(seed) == "string") {
seed = new Uint8Array(Base58.decode(seed));
}
var keyPair = nacl.sign.keyPair.fromSeed(seed);
var base58privateKey = Base58.encode(keyPair.secretKey);
var base58publicKey = Base58.encode(keyPair.publicKey);
if(returnBase58) {
return {
privateKey: Base58.encode(keyPair.secretKey),
publicKey: Base58.encode(keyPair.publicKey)
};
} else {
return {
privateKey: keyPair.secretKey,
publicKey: keyPair.publicKey
};
}
}
function stringtoUTF8Array(message) {
if (typeof message == 'string') {
var s = unescape(encodeURIComponent(message)); // UTF-8
message = new Uint8Array(s.length);
for (var i = 0; i < s.length; i++) {
message[i] = s.charCodeAt(i) & 0xff;
}
}
return message;
}
function int32ToBytes (word) {
var byteArray = [];
for (var b = 0; b < 32; b += 8) {
byteArray.push((word >>> (24 - b % 32)) & 0xFF);
}
return byteArray;
}
function int64ToBytes (int64) {
// we want to represent the input as a 8-bytes array
var byteArray = [0, 0, 0, 0, 0, 0, 0, 0];
for ( var index = 0; index < byteArray.length; index ++ ) {
var byte = int64 & 0xff;
byteArray [ byteArray.length - index - 1 ] = byte;
int64 = (int64 - byte) / 256 ;
}
return byteArray;
}
function appendBuffer (buffer1, buffer2) {
buffer1 = new Uint8Array(buffer1);
buffer2 = new Uint8Array(buffer2);
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(buffer1, 0);
tmp.set(buffer2, buffer1.byteLength);
return tmp;
}
function equal (buf1, buf2)
{
if (buf1.byteLength != buf2.byteLength) return false;
var dv1 = new Uint8Array(buf1);
var dv2 = new Uint8Array(buf2);
for (var i = 0; i != buf1.byteLength; i++)
{
if (dv1[i] != dv2[i]) return false;
}
return true;
}
function generateAccountSeed(seed, nonce, returnBase58)
{
if(typeof(seed) == "string") {
seed = Base58.decode(seed);
}
nonceBytes = int32ToBytes(nonce);
var resultSeed = new Uint8Array();
resultSeed = appendBuffer(resultSeed, nonceBytes);
resultSeed = appendBuffer(resultSeed, seed);
resultSeed = appendBuffer(resultSeed, nonceBytes);
if(returnBase58) {
return Base58.encode(SHA256.digest(SHA256.digest(resultSeed)));
} else {
return new SHA256.digest(SHA256.digest(resultSeed));
}
}
function getAccountAddressFromPublicKey(publicKey)
{
var ADDRESS_VERSION = 58; // Q
if(typeof(publicKey) == "string") {
publicKey = Base58.decode(publicKey);
}
var publicKeyHashSHA256 = SHA256.digest(publicKey);
var ripemd160 = new RIPEMD160();
var publicKeyHash = ripemd160.digest(publicKeyHashSHA256);
var addressArray = new Uint8Array();
addressArray = appendBuffer(addressArray, [ADDRESS_VERSION]);
addressArray = appendBuffer(addressArray, publicKeyHash);
var checkSum = SHA256.digest(SHA256.digest(addressArray));
addressArray = appendBuffer(addressArray, checkSum.subarray(0, 4));
return Base58.encode(addressArray);
}
function getAccountAddressType(address)
{
try {
var ADDRESS_VERSION = 58; // Q
var AT_ADDRESS_VERSION = 23; // A
if(typeof(address) == "string") {
address = Base58.decode(address);
}
var checkSum = address.subarray(address.length - 4, address.length)
var addressWitoutChecksum = address.subarray(0, address.length - 4);
var checkSumTwo = SHA256.digest(SHA256.digest(addressWitoutChecksum));
checkSumTwo = checkSumTwo.subarray(0, 4);
if (equal(checkSum, checkSumTwo))
{
if(address[0] == ADDRESS_VERSION)
{
return "standard";
}
if(address[0] == AT_ADDRESS_VERSION)
{
return "at";
}
}
return "invalid";
} catch (e) {
return "invalid";
}
}
function isValidAddress(address)
{
return (getAccountAddressType(address) != "invalid");
}
function generateSignaturePaymentTransaction(keyPair, lastReference, recipient, amount, fee, timestamp) {
const data = generatePaymentTransactionBase(keyPair.publicKey, lastReference, recipient, amount, fee, timestamp);
return nacl.sign.detached(data, keyPair.privateKey);
}
function generatePaymentTransaction(keyPair, lastReference, recipient, amount, fee, timestamp, signature) {
return appendBuffer(generatePaymentTransactionBase(keyPair.publicKey, lastReference, recipient, amount, fee, timestamp),
signature);
}
function generatePaymentTransactionBase(publicKey, lastReference, recipient, amount, fee, timestamp) {
const txType = TYPES.PAYMENT_TRANSACTION;
const typeBytes = int32ToBytes(txType);
const timestampBytes = int64ToBytes(timestamp);
const amountBytes = int64ToBytes(amount * 100000000);
const feeBytes = int64ToBytes(fee * 100000000);
var data = new Uint8Array();
data = appendBuffer(data, typeBytes);
data = appendBuffer(data, timestampBytes);
data = appendBuffer(data, lastReference);
data = appendBuffer(data, publicKey);
data = appendBuffer(data, recipient);
data = appendBuffer(data, amountBytes);
data = appendBuffer(data, feeBytes);
return data;
}
function generateSignatureMessageTransaction(keyPair, lastReference, recipient, amount, fee, timestamp, message, isText, isEncrypted) {
const data = generateMessageTransactionBase(keyPair.publicKey, lastReference, recipient, amount, fee, timestamp, message, isText, isEncrypted);
return nacl.sign.detached(data, keyPair.privateKey);
}
function generateMessageTransaction(keyPair, lastReference, recipient, amount, fee, timestamp, message, isText, isEncrypted, signature) {
return appendBuffer(generateMessageTransactionBase(keyPair.publicKey, lastReference, recipient, amount, fee, timestamp, message, isText, isEncrypted),
signature);
}
function generateMessageTransactionBase(publicKey, lastReference, recipient, amount, fee, timestamp, message, isText, isEncrypted) {
txType = TYPES.MESSAGE_TRANSACTION;
const typeBytes = int32ToBytes(txType);
const timestampBytes = int64ToBytes(timestamp);
const amountBytes = int64ToBytes(amount * 100000000);
const feeBytes = int64ToBytes(fee * 100000000);
const messageLength = int32ToBytes(message.length);
const key = int64ToBytes(0);
isTextB = new Uint8Array(1);
isTextB[0] = isText;
isEncryptedB = new Uint8Array(1);
isEncryptedB[0] = isEncrypted;
var data = new Uint8Array();
data = appendBuffer(data, typeBytes);
data = appendBuffer(data, timestampBytes);
data = appendBuffer(data, lastReference);
data = appendBuffer(data, publicKey);
data = appendBuffer(data, recipient);
data = appendBuffer(data, key);
data = appendBuffer(data, amountBytes);
data = appendBuffer(data, messageLength);
data = appendBuffer(data, message);
data = appendBuffer(data, isEncryptedB);
data = appendBuffer(data, isTextB);
data = appendBuffer(data, feeBytes);
return data;
}
function generateSignatureArbitraryTransactionV3(keyPair, lastReference, service, arbitraryData, fee, timestamp) {
const data = generateArbitraryTransactionV3Base(keyPair.publicKey, lastReference, service, arbitraryData, fee, timestamp);
return nacl.sign.detached(data, keyPair.privateKey);
}
function generateArbitraryTransactionV3(keyPair, lastReference, service, arbitraryData, fee, timestamp, signature) {
return appendBuffer(generateArbitraryTransactionV3Base(keyPair.publicKey, lastReference, service, arbitraryData, fee, timestamp),
signature);
}
function generateArbitraryTransactionV3Base(publicKey, lastReference, service, arbitraryData, fee, timestamp) {
const txType = TYPES.ARBITRARY_TRANSACTION;
const typeBytes = int32ToBytes(txType);
const timestampBytes = int64ToBytes(timestamp);
const feeBytes = int64ToBytes(fee * 100000000);
const serviceBytes = int32ToBytes(service);
const dataSizeBytes = int32ToBytes(arbitraryData.length);
const paymentsLengthBytes = int32ToBytes(0); // Support payments - not yet.
var data = new Uint8Array();
data = appendBuffer(data, typeBytes);
data = appendBuffer(data, timestampBytes);
data = appendBuffer(data, lastReference);
data = appendBuffer(data, publicKey);
data = appendBuffer(data, paymentsLengthBytes);
// Here it is necessary to insert the payments, if there are
data = appendBuffer(data, serviceBytes);
data = appendBuffer(data, dataSizeBytes);
data = appendBuffer(data, arbitraryData);
data = appendBuffer(data, feeBytes);
return data;
}
function generateSignatureRegisterNameTransaction(keyPair, lastReference, owner, name, value, fee, timestamp) {
const data = generateRegisterNameTransactionBase(keyPair.publicKey, lastReference, owner, name, value, fee, timestamp);
return nacl.sign.detached(data, keyPair.privateKey);
}
function generateRegisterNameTransaction(keyPair, lastReference, owner, name, value, fee, timestamp, signature) {
return appendBuffer( generateRegisterNameTransactionBase(keyPair.publicKey, lastReference, owner, name, value, fee, timestamp),
signature );
}
function generateRegisterNameTransactionBase(publicKey, lastReference, owner, name, value, fee, timestamp) {
const txType = TYPES.REGISTER_NAME_TRANSACTION;
const typeBytes = int32ToBytes(txType);
const timestampBytes = int64ToBytes(timestamp);
const feeBytes = int64ToBytes(fee * 100000000);
const nameSizeBytes = int32ToBytes(name.length);
const valueSizeBytes = int32ToBytes(value.length);
var data = new Uint8Array();
data = appendBuffer(data, typeBytes);
data = appendBuffer(data, timestampBytes);
data = appendBuffer(data, lastReference);
data = appendBuffer(data, publicKey);
data = appendBuffer(data, owner);
data = appendBuffer(data, nameSizeBytes);
data = appendBuffer(data, name);
data = appendBuffer(data, valueSizeBytes);
data = appendBuffer(data, value);
data = appendBuffer(data, feeBytes);
return data;
}

View File

@@ -0,0 +1,226 @@
// Extracted from https://github.com/crypto-browserify/ripemd160
const ARRAY16 = new Array(16);
const zl = initU8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
]);
const zr = initU8Array([
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
]);
const sl = initU8Array([
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
]);
const sr = initU8Array([
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
]);
const hl = initU32Array([0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e]);
const hr = initU32Array([0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000]);
function rotl(x, n) {
return (x << n) | (x >>> (32 - n));
}
function fn1(a, b, c, d, e, m, k, s) {
return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + e) | 0;
}
function fn2(a, b, c, d, e, m, k, s) {
return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + e) | 0;
}
function fn3(a, b, c, d, e, m, k, s) {
return (rotl((a + ((b | (~c)) ^ d) + m + k) | 0, s) + e) | 0;
}
function fn4(a, b, c, d, e, m, k, s) {
return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + e) | 0;
}
function fn5(a, b, c, d, e, m, k, s) {
return (rotl((a + (b ^ (c | (~d))) + m + k) | 0, s) + e) | 0;
}
function readInt32LE(buffer, offset) {
offset >>>= 0;
return (buffer[offset])
| (buffer[offset + 1] << 8)
| (buffer[offset + 2] << 16)
| (buffer[offset + 3] << 24);
}
function writeUInt32LE(buffer, value, offset) {
value = +value;
offset >>>= 0;
buffer[offset + 3] = (value >>> 24);
buffer[offset + 2] = (value >>> 16);
buffer[offset + 1] = (value >>> 8);
buffer[offset] = (value & 0xff);
return offset + 4;
}
function writeInt32LE(buffer, value, offset) {
value = +value;
offset >>>= 0;
buffer[offset] = (value & 0xff);
buffer[offset + 1] = (value >>> 8);
buffer[offset + 2] = (value >>> 16);
buffer[offset + 3] = (value >>> 24);
return offset + 4;
}
function initU32Array(data) {
if (typeof Uint32Array !== 'undefined') {
return new Uint32Array(data);
}
else {
return data;
}
}
function initU8Array(data) {
if (typeof Uint8Array !== 'undefined') {
return new Uint8Array(data);
}
else {
return data;
}
}
function createU8Array(size) {
if (typeof Uint8Array !== 'undefined') {
return new Uint8Array(size);
}
else {
return new Array(size);
}
}
export class RIPEMD160 {
constructor() {
this._block = createU8Array(64);
this._blockSize = 64;
this._blockOffset = 0;
this._length = [0, 0, 0, 0];
this._finalized = false;
this._a = 0x67452301;
this._b = 0xefcdab89;
this._c = 0x98badcfe;
this._d = 0x10325476;
this._e = 0xc3d2e1f0;
}
update(data) {
if (this._finalized)
throw new Error('Digest already called');
// consume data
const block = this._block;
let offset = 0;
while (this._blockOffset + data.length - offset >= this._blockSize) {
for (let i = this._blockOffset; i < this._blockSize;)
block[i++] = data[offset++];
this._update();
this._blockOffset = 0;
}
while (offset < data.length)
block[this._blockOffset++] = data[offset++];
// update length
for (let j = 0, carry = data.length * 8; carry > 0; ++j) {
this._length[j] += carry;
carry = (this._length[j] / 0x0100000000) | 0;
if (carry > 0)
this._length[j] -= 0x0100000000 * carry;
}
return this;
}
_update() {
const words = ARRAY16;
for (let j = 0; j < 16; ++j) {
words[j] = readInt32LE(this._block, j * 4);
}
let al = this._a | 0;
let bl = this._b | 0;
let cl = this._c | 0;
let dl = this._d | 0;
let el = this._e | 0;
let ar = this._a | 0;
let br = this._b | 0;
let cr = this._c | 0;
let dr = this._d | 0;
let er = this._e | 0;
// computation
for (let i = 0; i < 80; i += 1) {
let tl;
let tr;
if (i < 16) {
tl = fn1(al, bl, cl, dl, el, words[zl[i]], hl[0], sl[i]);
tr = fn5(ar, br, cr, dr, er, words[zr[i]], hr[0], sr[i]);
}
else if (i < 32) {
tl = fn2(al, bl, cl, dl, el, words[zl[i]], hl[1], sl[i]);
tr = fn4(ar, br, cr, dr, er, words[zr[i]], hr[1], sr[i]);
}
else if (i < 48) {
tl = fn3(al, bl, cl, dl, el, words[zl[i]], hl[2], sl[i]);
tr = fn3(ar, br, cr, dr, er, words[zr[i]], hr[2], sr[i]);
}
else if (i < 64) {
tl = fn4(al, bl, cl, dl, el, words[zl[i]], hl[3], sl[i]);
tr = fn2(ar, br, cr, dr, er, words[zr[i]], hr[3], sr[i]);
}
else { // if (i<80) {
tl = fn5(al, bl, cl, dl, el, words[zl[i]], hl[4], sl[i]);
tr = fn1(ar, br, cr, dr, er, words[zr[i]], hr[4], sr[i]);
}
al = el;
el = dl;
dl = rotl(cl, 10);
cl = bl;
bl = tl;
ar = er;
er = dr;
dr = rotl(cr, 10);
cr = br;
br = tr;
}
// update state
const t = (this._b + cl + dr) | 0;
this._b = (this._c + dl + er) | 0;
this._c = (this._d + el + ar) | 0;
this._d = (this._e + al + br) | 0;
this._e = (this._a + bl + cr) | 0;
this._a = t;
}
digest() {
if (this._finalized) {
throw new Error('Digest already called');
}
this._finalized = true;
// create padding and handle blocks
this._block[this._blockOffset++] = 0x80;
if (this._blockOffset > 56) {
this._block.fill(0, this._blockOffset, 64);
this._update();
this._blockOffset = 0;
}
this._block.fill(0, this._blockOffset, 56);
writeUInt32LE(this._block, this._length[0], 56);
writeUInt32LE(this._block, this._length[1], 60);
this._update();
// produce result
const buffer = createU8Array(20);
writeInt32LE(buffer, this._a, 0);
writeInt32LE(buffer, this._b, 4);
writeInt32LE(buffer, this._c, 8);
writeInt32LE(buffer, this._d, 12);
writeInt32LE(buffer, this._e, 16);
// reset state
this._block.fill(0);
this._blockOffset = 0;
for (let i = 0; i < 4; ++i) {
this._length[i] = 0;
}
return buffer;
}
}
export default RIPEMD160;

View File

@@ -0,0 +1,142 @@
// SHA-256 hash function. Copyright-free.
// Requires typed arrays.
const SHA256 = {};
SHA256.K = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b,
0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152,
0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
];
// The digest function returns the hash value (digest)
// as a 32 byte (typed) array.
// message: the string or byte array to hash
SHA256.digest = function(message) {
var h0 = 0x6a09e667;
var h1 = 0xbb67ae85;
var h2 = 0x3c6ef372;
var h3 = 0xa54ff53a;
var h4 = 0x510e527f;
var h5 = 0x9b05688c;
var h6 = 0x1f83d9ab;
var h7 = 0x5be0cd19;
var K = SHA256.K;
if (typeof message == 'string') {
var s = unescape(encodeURIComponent(message)); // UTF-8
message = new Uint8Array(s.length);
for (var i = 0; i < s.length; i++) {
message[i] = s.charCodeAt(i) & 0xff;
}
}
var length = message.length;
var byteLength = Math.floor((length + 72) / 64) * 64;
var wordLength = byteLength / 4;
var bitLength = length * 8;
var m = new Uint8Array(byteLength);
m.set(message);
m[length] = 0x80;
m[byteLength - 4] = bitLength >>> 24;
m[byteLength - 3] = (bitLength >>> 16) & 0xff;
m[byteLength - 2] = (bitLength >>> 8) & 0xff;
m[byteLength - 1] = bitLength & 0xff;
var words = new Int32Array(wordLength);
var byteIndex = 0;
for (var i = 0; i < words.length; i++) {
var word = m[byteIndex++] << 24;
word |= m[byteIndex++] << 16;
word |= m[byteIndex++] << 8;
word |= m[byteIndex++];
words[i] = word;
}
var w = new Int32Array(64);
for (var j = 0; j < wordLength; j += 16) {
for (i = 0; i < 16; i++) {
w[i] = words[j + i];
}
for (i = 16; i < 64; i++) {
var v = w[i - 15];
var s0 = (v >>> 7) | (v << 25);
s0 ^= (v >>> 18) | (v << 14);
s0 ^= (v >>> 3);
v = w[i - 2];
var s1 = (v >>> 17) | (v << 15);
s1 ^= (v >>> 19) | (v << 13);
s1 ^= (v >>> 10);
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & 0xffffffff;
}
var a = h0;
var b = h1;
var c = h2;
var d = h3;
var e = h4;
var f = h5;
var g = h6;
var h = h7;
for (i = 0; i < 64; i++) {
s1 = (e >>> 6) | (e << 26);
s1 ^= (e >>> 11) | (e << 21);
s1 ^= (e >>> 25) | (e << 7);
var ch = (e & f) ^ (~e & g);
var temp1 = (h + s1 + ch + K[i] + w[i]) & 0xffffffff;
s0 = (a >>> 2) | (a << 30);
s0 ^= (a >>> 13) | (a << 19);
s0 ^= (a >>> 22) | (a << 10);
var maj = (a & b) ^ (a & c) ^ (b & c);
var temp2 = (s0 + maj) & 0xffffffff;
h = g
g = f
f = e
e = (d + temp1) & 0xffffffff;
d = c;
c = b;
b = a;
a = (temp1 + temp2) & 0xffffffff;
}
h0 = (h0 + a) & 0xffffffff;
h1 = (h1 + b) & 0xffffffff;
h2 = (h2 + c) & 0xffffffff;
h3 = (h3 + d) & 0xffffffff;
h4 = (h4 + e) & 0xffffffff;
h5 = (h5 + f) & 0xffffffff;
h6 = (h6 + g) & 0xffffffff;
h7 = (h7 + h) & 0xffffffff;
}
var hash = new Uint8Array(32);
for (var i = 0; i < 4; i++) {
hash[i] = (h0 >>> (8 * (3 - i))) & 0xff;
hash[i + 4] = (h1 >>> (8 * (3 - i))) & 0xff;
hash[i + 8] = (h2 >>> (8 * (3 - i))) & 0xff;
hash[i + 12] = (h3 >>> (8 * (3 - i))) & 0xff;
hash[i + 16] = (h4 >>> (8 * (3 - i))) & 0xff;
hash[i + 20] = (h5 >>> (8 * (3 - i))) & 0xff;
hash[i + 24] = (h6 >>> (8 * (3 - i))) & 0xff;
hash[i + 28] = (h7 >>> (8 * (3 - i))) & 0xff;
}
return hash;
}
// The hash function returns the hash value as a hex string.
// message: the string or byte array to hash
SHA256.hash = function(message) {
var digest = SHA256.digest(message);
var hex = '';
for (var i = 0; i < digest.length; i++) {
var s = '0' + digest[i].toString(16);
hex += s.length > 2 ? s.substring(1) : s;
}
return hex;
}
// == Convert to ES6 Module == //
export default SHA256;

View File

@@ -0,0 +1,74 @@
'use strict'
const utils = {
int32ToBytes(word) {
var byteArray = []
for (var b = 0; b < 32; b += 8) {
byteArray.push((word >>> (24 - b % 32)) & 0xFF)
}
return byteArray
},
stringtoUTF8Array(message) {
if (typeof message === 'string') {
var s = unescape(encodeURIComponent(message)) // UTF-8
message = new Uint8Array(s.length)
for (var i = 0; i < s.length; i++) {
message[i] = s.charCodeAt(i) & 0xff
}
}
return message
},
// ...buffers then buffers.foreach and append to buffer1
appendBuffer(buffer1, buffer2) {
buffer1 = new Uint8Array(buffer1)
buffer2 = new Uint8Array(buffer2)
let tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength)
tmp.set(buffer1, 0)
tmp.set(buffer2, buffer1.byteLength)
return tmp
},
int64ToBytes(int64) {
// we want to represent the input as a 8-bytes array
var byteArray = [0, 0, 0, 0, 0, 0, 0, 0]
for (var index = 0; index < byteArray.length; index++) {
var byte = int64 & 0xff
byteArray[byteArray.length - index - 1] = byte
int64 = (int64 - byte) / 256
}
return byteArray
},
hexToBytes(hexString) {
return new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
},
stringToHex(bytes) {
return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '')
},
equal(buf1, buf2) {
if (buf1.byteLength != buf2.byteLength) return false
var dv1 = new Uint8Array(buf1)
var dv2 = new Uint8Array(buf2)
for (var i = 0; i != buf1.byteLength; i++) {
if (dv1[i] != dv2[i]) return false
}
return true
},
bytesToHex(byteArray) {
var _byteArrayToHex = []
for (var index = 0; index < byteArray.length; index++) {
_byteArrayToHex.push((byteArray[index] >>> 4).toString(16))
_byteArrayToHex.push((byteArray[index] & 15).toString(16));
}
return _byteArrayToHex.join("")
}
}
export default utils