Skip to content

Commit

Permalink
Merge branch 'NIP0004'
Browse files Browse the repository at this point in the history
  • Loading branch information
jontey committed Aug 26, 2019
2 parents cbd2bee + e2d4d26 commit 1362a60
Show file tree
Hide file tree
Showing 30 changed files with 2,409 additions and 2,623 deletions.
46 changes: 46 additions & 0 deletions example/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NetworkType, PublicAccount } from 'nem2-sdk';
import { map, reduce, shareReplay } from 'rxjs/operators';
import { ApostilleHttp } from './../src/infrastructure/ApostilleHttp';
import { HistoricalEndpoints } from './../src/model/repository/HistoricalEndpoints';

// const accountLarge = PublicAccount.createFromPublicKey(
// 'FE9F2C724D0E0360A20B9ED7591E4E25CF25D6F4A4E8E52C491C62D2452397F8',
// NetworkType.MIJIN_TEST);

// const accountSmall = PublicAccount.createFromPublicKey(
// '38FD27361DB3B12953AF6C3813F740ADB7C980789DCB3D2486B3B654CBC91B72',
// NetworkType.MIJIN_TEST);
const accountSmall = PublicAccount.createFromPublicKey(
'95361ED8C94048BD5B0BDB229C19DF817DB7D66B59F4162E3F3A1D0D813B2AB9',
NetworkType.MIJIN_TEST);

const http = new ApostilleHttp(HistoricalEndpoints[NetworkType.MIJIN_TEST]);

const subscription = http.fetchAllTransactions(accountSmall).pipe(
map((txs) => {
return txs.map((tx) => {
if (!tx.transactionInfo) { return {}; }
console.log(tx.transactionInfo.id, tx.transactionInfo.height.compact());
return {
height: tx.transactionInfo.height,
id: tx.transactionInfo.id,
};
});
}),
reduce((acc, txs) => {
return acc.concat(txs);
}),
shareReplay(),
);

subscription.subscribe((txs) => {
console.log('First', txs.length);
});
subscription.subscribe((txs) => {
console.log('Second', txs.length);
});
setTimeout(() => {
subscription.subscribe((txs) => {
console.log('Third', txs.length);
});
}, 10000);
82 changes: 82 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import fs from 'fs';
import { Account, NetworkType, PublicAccount, SignedTransaction } from 'nem2-sdk';
import path from 'path';
import { Apostille, ApostilleHttp, HistoricalEndpoints, Initiator, SHA256, SHA3_256 } from '../index';

// ApostilleHttp
const network = NetworkType.MIJIN_TEST;
const apostilleHttp = new ApostilleHttp(HistoricalEndpoints[network]);

// Create Apostille function
function createApostille(pk, fileName) {
const file = fs.readFileSync(`${path.basename(__dirname)}/${fileName}`);
const fileData = file.toString('hex');

// Account info
const generatorAccount = Account.createFromPrivateKey(pk, network);

// FOR DEMO PURPOSES ONLY - DO NOT USE IN PRODUCTION!
const sinkPublicKey = SHA256.hash('PublicApostilleSinkAddress');
const sinkAddress = PublicAccount.createFromPublicKey(sinkPublicKey, network).address;

// Create apostille
const apostille = Apostille.initFromSeed(fileName, generatorAccount);

const fileHash = new SHA3_256().signedHashing(fileData, pk, network);

console.log(fileHash);

const apostilleTx = apostille.update(fileHash);
apostilleHttp.addTransaction({
initiator: new Initiator(generatorAccount),
transaction: apostilleTx,
});

const sinkTx = apostille.update(fileHash, sinkAddress);
apostilleHttp.addTransaction({
initiator: new Initiator(apostille.HDAccount),
transaction: sinkTx,
});

const multisigTx = apostille.associate([generatorAccount.publicAccount], 1, 1);
apostilleHttp.addTransaction(multisigTx);

const metadata = {
filename: fileName,
tags: ['apostille', 'sample', 'LuxTag'],
// tslint:disable-next-line:object-literal-sort-keys
description: 'LuxTag logo',
originFileURL: 'https://luxtag.io/wp-content/uploads/2018/04/logo-Luxtag-uai-720x269.png',
};
const metadataTx = apostille.update(JSON.stringify(metadata));
apostilleHttp.addTransaction({
initiator: new Initiator(generatorAccount),
transaction: metadataTx,
});
}

// File info
const f = 'luxtag1.png';
const accounts = [
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae1',
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae2',
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae3',
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae4',
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae5',
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae6',
'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aae7',
];

accounts.forEach((a) => createApostille(a, f));

// Announce
apostilleHttp.announceAll().subscribe(
(result) => {
if (result instanceof Array) {
if (result[0] instanceof SignedTransaction) {
return;
}
}
console.log(result);
},
);
Binary file added example/luxtag1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 22 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,44 @@
{
"name": "jeff",
"email": "[email protected]"
},
{
"name": "Jonathan Tey",
"email": "[email protected]"
}
],
"license": "MIT",
"dependencies": {
"@octokit/request": "^2.1.2",
"crypto-js": "^3.1.9-1",
"js-sha3": "0.8.0",
"js-sha3": "^0.8.0",
"lodash": "^4.17.10",
"nem-sdk": "^1.6.7",
"nem2-sdk": "^0.11.1",
"nem2-sdk": "^0.13.1",
"rxjs": "^6.2.2"
},
"devDependencies": {
"@types/crypto-js": "^3.1.43",
"@types/jest": "^23.3.9",
"@types/jest": "24.0.11",
"@types/lodash": "^4.14.113",
"@types/sinon": "5.0.7",
"@types/sinon": "7.0.11",
"codecov": "^3.0.4",
"commitizen": "3.0.4",
"commitlint": "7.2.1",
"commitizen": "3.0.7",
"commitlint": "7.5.2",
"cz-conventional-changelog": "^2.1.0",
"ghooks": "^2.0.4",
"jest": "^23.4.1",
"npm-run-all": "4.1.5",
"rimraf": "^2.6.2",
"semantic-release": "^15.8.1",
"sinon": "7.1.1",
"travis-deploy-once": "5.0.9",
"ts-jest": "^23.0.1",
"sinon": "7.3.1",
"travis-deploy-once": "5.0.11",
"ts-jest": "24.0.2",
"tslint": "^5.11.0",
"typescript": "^2.8.3",
"typescript": "3.4.3",
"validate-commit-msg": "^2.14.0"
},
"resolutions": {
"@octokit/request": "^2.1.2"
"request": "2.88.0"
},
"files": [
"dist"
Expand All @@ -92,24 +95,23 @@
},
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"preset": "ts-jest",
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
"jsx",
"node",
"ts",
"tsx"
],
"coverageDirectory": "<rootDir>/tests/unit/coverage",
"collectCoverageFrom": [
"<rootDir>/src/**/*.ts"
],
"coveragePathIgnorePatterns": [
"/types/"
]
],
"testMatch": null
}
}
27 changes: 14 additions & 13 deletions src/hash/HashFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import { NetworkType } from 'nem2-sdk';
* @class HashFunction
*/
export abstract class HashFunction {
public readonly nonSigned: string;
public readonly signed: string;
/**
*
* Returns apostille magic header
* @readonly
* @memberof HashFunction
*/
get checksum() {
return 'fe4e5459' + this.signed;
}

/**
* Creates an instance of HashFunction.
* @param {string} nonSigned - the none signed hex value
* @param {string} signed - signed hex value
* @memberof HashFunction
*/
public constructor(nonSigned: string, signed: string) {
this.nonSigned = nonSigned;
public constructor(signed: string) {
this.signed = signed;
}

/**
* hashes data and adds a magical byte for public apostille
* hashes data and adds a magical byte for apostille
*
* @abstract
* @param {string} data - raw data
Expand All @@ -30,13 +39,5 @@ export abstract class HashFunction {
* @memberof HashFunction
*/
public abstract signedHashing(data: string, signerPrivateKey: string, networkType: NetworkType): string;
/**
* hashes data and adds a magical byte for private apostille
*
* @abstract
* @param {string} data
* @returns {string}
* @memberof HashFunction
*/
public abstract nonSignedHashing(data: string): string;

}
2 changes: 2 additions & 0 deletions src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './keccak-512';
export * from './md5';
export * from './sha1';
export * from './sha256';
export * from './sha3-256';
export * from './sha3-512';
21 changes: 5 additions & 16 deletions src/hash/keccak-256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class KECCAK256 extends HashFunction {
* @memberof KECCAK256
*/
constructor() {
super('08', '88');
super('88');
}
/**
* @description - creates a signed hash for private apostille
Expand All @@ -26,26 +26,15 @@ export class KECCAK256 extends HashFunction {
* @memberof KECCAK256
*/
public signedHashing(data: string, signerPrivateKey: string, networkType: NetworkType): string {
// nem2-sdk doesn't support keccak signing yet
const dataHash = CryptoJS.SHA3(data, { outputLength: 256 }).toString();

if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) {
const keyPair = nem.crypto.keyPair.create(signerPrivateKey);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + keyPair.sign(CryptoJS.SHA3(data, { outputLength: 256 }).toString()).toString();
return this.checksum + keyPair.sign(dataHash).toString();
} else {
// sha-3 signing
const signer = Account.createFromPrivateKey(signerPrivateKey, networkType);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + signer.signData(CryptoJS.SHA3(data, { outputLength: 256 }).toString());
return this.checksum + signer.signData(dataHash);
}
}
/**
* @description - creates a hash of the digital file for public apostille
* @param {string} data - digital file raw data
* @returns - a hash with a magical byte
* @memberof KECCAK256
*/
public nonSignedHashing(data: string): string {
const CHEKSUM = 'fe4e5459' + this.nonSigned;
return CHEKSUM + CryptoJS.SHA3(data, { outputLength: 256 });
}
}
20 changes: 5 additions & 15 deletions src/hash/keccak-512.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class KECCAK512 extends HashFunction {
* @memberof KECCAK512
*/
constructor() {
super('09', '89');
super('89');
}
/**
* @description - creates a signed hash for private apostille
Expand All @@ -26,25 +26,15 @@ export class KECCAK512 extends HashFunction {
* @memberof KECCAK512
*/
public signedHashing(data: string, signerPrivateKey: string, networkType: NetworkType) {
const dataHash = CryptoJS.SHA3(data, { outputLength: 512 }).toString();

if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) {
const keyPair = nem.crypto.keyPair.create(signerPrivateKey);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + keyPair.sign(CryptoJS.SHA3(data, { outputLength: 512 }).toString()).toString();
return this.checksum + keyPair.sign(dataHash).toString();
} else {
// sha-3 signing
const signer = Account.createFromPrivateKey(signerPrivateKey, networkType);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + signer.signData(CryptoJS.SHA3(data, { outputLength: 512 }).toString());
return this.checksum + signer.signData(dataHash);
}
}
/**
* @description - creates a hash of the digital file for public apostille
* @param {string} data - digital file raw data
* @returns - a hash with a magical byte
* @memberof KECCAK512
*/
public nonSignedHashing(data: string) {
const CHEKSUM = 'fe4e5459' + this.nonSigned;
return CHEKSUM + CryptoJS.SHA3(data, { outputLength: 512 });
}
}
21 changes: 5 additions & 16 deletions src/hash/md5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class MD5 extends HashFunction {
* @memberof MD5
*/
constructor() {
super('01', '81');
super('81');
}
/**
* @description - creates a signed hash for private apostille
Expand All @@ -26,25 +26,14 @@ export class MD5 extends HashFunction {
* @memberof MD5
*/
public signedHashing(data: string, signerPrivateKey: string, networkType: NetworkType) {
const dataHash = CryptoJS.MD5(data).toString();

if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) {
const keyPair = nem.crypto.keyPair.create(signerPrivateKey);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + keyPair.sign(CryptoJS.MD5(data).toString()).toString();
return this.checksum + keyPair.sign(dataHash).toString();
} else {
// sha-3 signing
const signer = Account.createFromPrivateKey(signerPrivateKey, networkType);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + signer.signData(CryptoJS.MD5(data).toString());
return this.checksum + signer.signData(dataHash);
}
}
/**
* @description - creates a hash of the digital file for public apostille
* @param {string} data - digital file raw data
* @returns - a hash with a magical byte
* @memberof MD5
*/
public nonSignedHashing(data: string) {
const CHEKSUM = 'fe4e5459' + this.nonSigned;
return CHEKSUM + CryptoJS.MD5(data);
}
}
Loading

0 comments on commit 1362a60

Please sign in to comment.