import { DIDReader } from './../DecentralizedIdentifier/DIDReader'; import { RsaKeypair } from "../RsaKeypair"; import { createHttpClient } from '@iota/http-client'; import { createFindTransactions, createGetTransactionObjects, createSendTrytes, createPrepareTransfers, createGetBundle } from "@iota/core"; import { Provider } from "@iota/transaction/typings/types"; import { Transaction } from '@iota/core'; import {asciiToTrytes, trytesToAscii} from '@iota/converter' import { keyGen } from "mam.ts"; export enum CommandType { add = 1, retract = 2 } export interface SignedTX { commandType : CommandType, signedByUUID : string, publicKey: string, time : number, signature : string } export class IOTAList { protected address : string; private provider : Provider; private providerUrl : string; constructor( address : string, provider : string ) { this.address = address; this.providerUrl = provider; this.provider = createHttpClient({ provider : this.providerUrl}); } public ValidateSignature(signedByUUID : string, publicKey : string, time : number, signature : string) : Promise { return new Promise((resolve, reject) => { //Get the DID for the publicKey let didReader : DIDReader = new DIDReader(this.providerUrl, signedByUUID); didReader.QueryDID() .then(() => { //Use the publicKey to check if the DID has actually signed the permission for(let i : number = 0; i < didReader.GetState().getPublicKey().length; i++) { if(didReader.GetState().getPublicKey()[i].id == publicKey) { //Create the RSA keypair let keyPair : RsaKeypair = new RsaKeypair(didReader.GetState().getPublicKey()[i].publicKeyPem); //THe signature is the UUID + PublicKey + Time concatanated and signed with the PublicKey. let SignedData = signedByUUID + publicKey + time; resolve(true/*keyPair.Verify(SignedData, signature)*/); return; } } //This code is only reached if no match is found resolve(true); }) .catch((error) => { reject(error); }); }); } protected AddTransactionToAddress(transactionData : string, address : string = this.address) : Promise { return new Promise ((resolve, reject) => { //Message in the correct layout const transaction = [{ address : address, value : 0, message: asciiToTrytes(transactionData), tag: "ODYSSEYVX" }]; //Convert to trytes const prepareTransfers : any = createPrepareTransfers(this.provider); const sendTrytes : any = createSendTrytes(this.provider); prepareTransfers(keyGen(81), transaction) .then((trytes) => { //Communicate the transaction to the network sendTrytes(trytes, 3, 14) .then((txs) => { resolve(true); }) .catch((error) => { console.log("SendTrytes failed"); reject(error); }); }) .catch((error) => { console.log("prepareTransfers failed"); reject(error); }); }); } protected GetTransactionsFromAddress(address : string = this.address) : Promise { return new Promise((resolve, reject) => { //Get the TXs from IOTA const findTransactions : any = createFindTransactions(this.provider); const getBundle : any = createGetBundle(this.provider); findTransactions({addresses : [address]}) .then(async (transactionHashes) => { if(transactionHashes) { //TODO: Order the transactions! let objects : any[] = []; let messages : string[] = []; for(let i=0; i < transactionHashes.length;i++) { let bundle = await getBundle(transactionHashes[i]) let msg = bundle[0].signatureMessageFragment; msg = trytesToAscii(msg.slice(0,(msg.length%2)?msg.length-1:msg.length)); objects.push({timestamp: bundle[0].timestamps, msg:msg}); messages.push(msg); } objects.sort((a,b) => (b.timestamp - a.timestamp)); for(let i=0; i { reject(error); }) }); } }