-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ApostilleHttp.ts): Fix announceAll() function
Added examples
- Loading branch information
Showing
5 changed files
with
183 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import fs from 'fs'; | ||
import { Account, NetworkType, PublicAccount } from 'nem2-sdk'; | ||
import path from 'path'; | ||
import { SHA256 } from './../src/hash/sha256'; | ||
import { SHA3_256 } from './../src/hash/sha3-256'; | ||
import { ApostilleHttp } from './../src/infrastructure/ApostilleHttp'; | ||
import { Initiator } from './../src/infrastructure/Initiator'; | ||
import { Apostille } from './../src/model/apostille/Apostille'; | ||
import { HistoricalEndpoints } from './../src/model/repository/HistoricalEndpoints'; | ||
|
||
// 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( | ||
(signedTxs) => console.log(signedTxs), | ||
); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters