diff --git a/e2e/infrastructure/BlockHttp.spec.ts b/e2e/infrastructure/BlockHttp.spec.ts index f87504ef83..9b721000ba 100644 --- a/e2e/infrastructure/BlockHttp.spec.ts +++ b/e2e/infrastructure/BlockHttp.spec.ts @@ -28,6 +28,7 @@ import { Deadline } from '../../src/model/transaction/Deadline'; import { TransactionInfo } from '../../src/model/transaction/TransactionInfo'; import { TransferTransaction } from '../../src/model/transaction/TransferTransaction'; import { IntegrationTestHelper } from './IntegrationTestHelper'; +import { UInt64 } from '../../src/model/UInt64'; describe('BlockHttp', () => { const helper = new IntegrationTestHelper(); @@ -87,7 +88,7 @@ describe('BlockHttp', () => { describe('getBlockByHeight', () => { it('should return block info given height', (done) => { - blockRepository.getBlockByHeight('1') + blockRepository.getBlockByHeight(UInt64.fromUint(1)) .subscribe((blockInfo) => { blockReceiptHash = blockInfo.blockReceiptsHash; blockTransactionHash = blockInfo.blockTransactionsHash; @@ -105,7 +106,7 @@ describe('BlockHttp', () => { let firstId: string; it('should return block transactions data given height', (done) => { - blockRepository.getBlockTransactions('1') + blockRepository.getBlockTransactions(UInt64.fromUint(1)) .subscribe((transactions) => { nextId = transactions[0].transactionInfo!.id; firstId = transactions[1].transactionInfo!.id; @@ -115,7 +116,7 @@ describe('BlockHttp', () => { }); it('should return block transactions data given height with paginated transactionId', (done) => { - blockRepository.getBlockTransactions('1', new QueryParams(10, nextId)) + blockRepository.getBlockTransactions(UInt64.fromUint(1), new QueryParams(10, nextId)) .subscribe((transactions) => { expect(transactions[0].transactionInfo!.id).to.be.equal(firstId); expect(transactions.length).to.be.greaterThan(0); diff --git a/e2e/service/MosaicRestrictionTransactionService.spec.ts b/e2e/service/MosaicRestrictionTransactionService.spec.ts index 73a54cad3c..eee4d636f6 100644 --- a/e2e/service/MosaicRestrictionTransactionService.spec.ts +++ b/e2e/service/MosaicRestrictionTransactionService.spec.ts @@ -74,7 +74,6 @@ describe('MosaicRestrictionTransactionService', () => { UInt64.fromUint(1000), networkType, helper.maxFee, ); - console.log(mosaicId.toHex()); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); return helper.announce(signedTransaction); }); diff --git a/e2e/service/MosaicService.spec.ts b/e2e/service/MosaicService.spec.ts index 0429d88e77..5760136205 100644 --- a/e2e/service/MosaicService.spec.ts +++ b/e2e/service/MosaicService.spec.ts @@ -44,7 +44,6 @@ describe('MosaicService', () => { const views = amountViews.map((v) => { return {mosaicId: v.fullName(), amount: v.relativeAmount()}; }); - console.log(views); done(); }); }); diff --git a/package-lock.json b/package-lock.json index b05f0ca979..e6d5021cff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -959,9 +959,9 @@ "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" }, "catbuffer": { - "version": "0.0.7", - "resolved": "https://registry.npmjs.org/catbuffer/-/catbuffer-0.0.7.tgz", - "integrity": "sha512-c7oYz0Zhxa9Sw4Nzi8idIMdMyRvZ57SzzshyZKfqTQ0TCz2LyGHLD9X5/hg5P1ZqA5EARUF7kDKoL4IoBQ6IBQ==" + "version": "0.0.11", + "resolved": "https://registry.npmjs.org/catbuffer/-/catbuffer-0.0.11.tgz", + "integrity": "sha512-xcv43mcBDeZ1NldCsKwD28gBIs/mj+QBMiu3BI9511Wqeo5VfIsqKULAI9OGTOKaf48G7AYYZHWKKov/EkhcfQ==" }, "chai": { "version": "4.1.2", @@ -2552,6 +2552,11 @@ "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", "dev": true }, + "futoin-hkdf": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/futoin-hkdf/-/futoin-hkdf-1.3.1.tgz", + "integrity": "sha512-k1DvCXIFAIx3hK8CSwApotX3JUDwA2Wb55zxyIgqwQpCBF2ZHgVqfHpyjG8mRpmsjRH7SWS1N/vj8EdSF9zBhw==" + }, "get-caller-file": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", @@ -3818,9 +3823,9 @@ } }, "nem2-sdk-openapi-typescript-node-client": { - "version": "0.7.20-beta.6", - "resolved": "https://registry.npmjs.org/nem2-sdk-openapi-typescript-node-client/-/nem2-sdk-openapi-typescript-node-client-0.7.20-beta.6.tgz", - "integrity": "sha512-RqoDZuPMAQ1/kStC5uAk8TFqZkLA8RuVtlpRH7TU3ZTIGylwgX95PdmKaGTUZAuoyAXo2b64OenVvt09ryZ3lw==", + "version": "0.7.20-alpha.7", + "resolved": "https://registry.npmjs.org/nem2-sdk-openapi-typescript-node-client/-/nem2-sdk-openapi-typescript-node-client-0.7.20-alpha.7.tgz", + "integrity": "sha512-nFSVX67iHxQIU3wwtLxAzeOcVjT1S4LLGYaJwwosDhXWXhUBkyZ6iVTeJ+OYdR1Qj8kpKvQibxyaq/MwzCeh8A==", "requires": { "@types/bluebird": "*", "@types/request": "*", diff --git a/package.json b/package.json index d6ee0283fa..80ac24322f 100644 --- a/package.json +++ b/package.json @@ -58,14 +58,15 @@ }, "dependencies": { "bluebird": "^3.5.5", - "catbuffer": "0.0.7", - "nem2-sdk-openapi-typescript-node-client": "0.7.20-beta.6", + "catbuffer": "0.0.11", "crypto-js": "^3.1.9-1", + "futoin-hkdf": "^1.3.1", "js-joda": "^1.6.2", "js-sha256": "^0.9.0", "js-sha3": "^0.8.0", "long": "^4.0.0", "merkletreejs": "^0.1.7", + "nem2-sdk-openapi-typescript-node-client": "0.7.20-alpha.7", "request": "^2.88.0", "request-promise-native": "^1.0.5", "ripemd160": "^2.0.2", diff --git a/src/core/crypto/Crypto.ts b/src/core/crypto/Crypto.ts index a3a299fec9..bf12ccaae1 100644 --- a/src/core/crypto/Crypto.ts +++ b/src/core/crypto/Crypto.ts @@ -228,20 +228,19 @@ export class Crypto { recipientPub: string, msg: string, iv: Uint8Array, - salt: Uint8Array, signSchema: SignSchema): string => { // Errors - if (!senderPriv || !recipientPub || !msg || !iv || !salt) { throw new Error('Missing argument !'); } + if (!senderPriv || !recipientPub || !msg || !iv) { throw new Error('Missing argument !'); } // Processing const keyPair = KeyPair.createKeyPairFromPrivateKeyString(senderPriv, signSchema); const pk = convert.hexToUint8(recipientPub); - const encKey = utility.ua2words(KeyPair.deriveSharedKey(keyPair, pk, salt, signSchema), 32); + const encKey = utility.ua2words(KeyPair.deriveSharedKey(keyPair, pk, signSchema), 32); const encIv = { iv: utility.ua2words(iv, 16), }; const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(msg), encKey, encIv); // Result - const result = convert.uint8ToHex(salt) + convert.uint8ToHex(iv) + CryptoJS.enc.Hex.stringify(encrypted.ciphertext); + const result = convert.uint8ToHex(iv) + CryptoJS.enc.Hex.stringify(encrypted.ciphertext); return result; } @@ -264,8 +263,7 @@ export class Crypto { if (!senderPriv || !recipientPub || !msg) { throw new Error('Missing argument !'); } // Processing const iv = Crypto.randomBytes(16); - const salt = Crypto.randomBytes(32); - const encoded = Crypto._encode(senderPriv, recipientPub, isHexString ? msg : convert.utf8ToHex(msg), iv, salt, signSchema); + const encoded = Crypto._encode(senderPriv, recipientPub, isHexString ? msg : convert.utf8ToHex(msg), iv, signSchema); // Result return encoded; } @@ -277,7 +275,6 @@ export class Crypto { * @param {string} senderPublic - A sender public key * @param {Uint8Array} payload - An encrypted message payload in bytes * @param {Uint8Array} iv - 16-byte AES initialization vector - * @param {Uint8Array} salt - 32-byte salt * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @return {string} - The decoded payload as hex */ @@ -285,14 +282,13 @@ export class Crypto { senderPublic: string, payload: Uint8Array, iv: Uint8Array, - salt: Uint8Array, signSchema: SignSchema): string => { // Error if (!recipientPrivate || !senderPublic || !payload) { throw new Error('Missing argument !'); } // Processing const keyPair = KeyPair.createKeyPairFromPrivateKeyString(recipientPrivate, signSchema); const pk = convert.hexToUint8(senderPublic); - const encKey = utility.ua2words(KeyPair.deriveSharedKey(keyPair, pk, salt, signSchema), 32); + const encKey = utility.ua2words(KeyPair.deriveSharedKey(keyPair, pk, signSchema), 32); const encIv = { iv: utility.ua2words(iv, 16), }; @@ -321,10 +317,9 @@ export class Crypto { if (!recipientPrivate || !senderPublic || !payload) { throw new Error('Missing argument !'); } // Processing const binPayload = convert.hexToUint8(payload); - const payloadBuffer = new Uint8Array(binPayload.buffer, 48); - const salt = new Uint8Array(binPayload.buffer, 0, 32); - const iv = new Uint8Array(binPayload.buffer, 32, 16); - const decoded = Crypto._decode(recipientPrivate, senderPublic, payloadBuffer, iv, salt, signSchema); + const payloadBuffer = new Uint8Array(binPayload.buffer, 16); + const iv = new Uint8Array(binPayload.buffer, 0, 16); + const decoded = Crypto._decode(recipientPrivate, senderPublic, payloadBuffer, iv, signSchema); return decoded.toUpperCase(); } diff --git a/src/core/crypto/KeyPair.ts b/src/core/crypto/KeyPair.ts index f82b5d85c5..b1181f8c2a 100644 --- a/src/core/crypto/KeyPair.ts +++ b/src/core/crypto/KeyPair.ts @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { NetworkType } from '../../model/blockchain/NetworkType'; import { Convert as convert } from '../format'; import { SignSchema } from './SignSchema'; import * as Utility from './Utilities'; @@ -44,20 +43,20 @@ export class KeyPair { * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {Uint8Array} The signature. */ - public static sign = (keyPair, data: Uint8Array, signSchema: SignSchema) => { + public static sign = (keyPair, data: Uint8Array, signSchema: SignSchema): Uint8Array => { return Utility.catapult_crypto.sign(data, keyPair.publicKey, keyPair.privateKey, Utility.catapult_hash.createHasher(64, signSchema)); } /** * Verifies a signature. - * @param {module:crypto/keyPair~PublicKey} publicKey The public key to use for verification. + * @param {Uint8Array} publicKey The public key to use for verification. * @param {Uint8Array} data The data to verify. * @param {Uint8Array} signature The signature to verify. * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {boolean} true if the signature is verifiable, false otherwise. */ - public static verify = (publicKey, data: Uint8Array, signature: Uint8Array, signSchema: SignSchema) => { + public static verify = (publicKey: Uint8Array, data: Uint8Array, signature: Uint8Array, signSchema: SignSchema): boolean => { return Utility.catapult_crypto.verify(publicKey, data, signature, Utility.catapult_hash.createHasher(64, signSchema)); } @@ -66,17 +65,13 @@ export class KeyPair { * The shared key can be used for encrypted message passing between the two. * @param {module:crypto/keyPair~KeyPair} keyPair The key pair for which to create the shared key. * @param {Uint8Array} publicKey The public key for which to create the shared key. - * @param {Uint8Array} salt A salt that should be applied to the shared key. * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {Uint8Array} The shared key. */ - public static deriveSharedKey = (keyPair, publicKey: Uint8Array, salt: Uint8Array, signSchema: SignSchema) => { - if (Utility.Key_Size !== salt.length) { - throw Error(`salt has unexpected size: ${salt.length}`); - } + public static deriveSharedKey = (keyPair, publicKey: Uint8Array, signSchema: SignSchema) => { if (Utility.Key_Size !== publicKey.length) { - throw Error(`public key has unexpected size: ${salt.length}`); + throw Error(`public key has unexpected size: ${publicKey.length}`); } - return Utility.catapult_crypto.deriveSharedKey(salt, keyPair.privateKey, publicKey, Utility.catapult_hash.func, signSchema); + return Utility.catapult_crypto.deriveSharedKey(keyPair.privateKey, publicKey, Utility.catapult_hash.func, signSchema); } } diff --git a/src/core/crypto/Utilities.ts b/src/core/crypto/Utilities.ts index 69132811d0..0f1637a693 100644 --- a/src/core/crypto/Utilities.ts +++ b/src/core/crypto/Utilities.ts @@ -18,12 +18,14 @@ import { RawArray as array } from '../format'; import * as nacl from './nacl_catapult'; import { SHA3Hasher as sha3Hasher } from './SHA3Hasher'; import { SignSchema } from './SignSchema'; + export const CryptoJS = require('crypto-js'); export const Key_Size = 32; export const Signature_Size = 64; export const Half_Signature_Size = Signature_Size / 2; export const Hash_Size = 64; export const Half_Hash_Size = Hash_Size / 2; +export const hkdf = require('futoin-hkdf'); /** * Convert an Uint8Array to WordArray @@ -68,7 +70,7 @@ export const catapult_hash = { }; // custom catapult crypto functions -export const catapult_crypto = (function() { +export const catapult_crypto = (() => { function clamp(d) { d[0] &= 248; d[31] &= 127; @@ -82,7 +84,7 @@ export const catapult_crypto = (function() { return d; } - const encodedSChecker = (function() { + const encodedSChecker = (() => { const Is_Reduced = 1; const Is_Zero = 2; @@ -202,25 +204,26 @@ export const catapult_crypto = (function() { return 0 === c.crypto_verify_32(signature, 0, t, 0); }, - deriveSharedKey: (salt, sk, pk, hashfunc, signSchema: SignSchema) => { + deriveSharedKey: (privateKey: Uint8Array, publicKey: Uint8Array, hashfunc, signSchema: SignSchema): Uint8Array => { + const sharedSecret = catapult_crypto.deriveSharedSecret(privateKey, publicKey, hashfunc, signSchema); + const info = 'catapult'; + const hash = 'SHA-256'; + return hkdf(sharedSecret, 32, {salt: new Uint8Array(32), info, hash}); + }, + + deriveSharedSecret: (privateKey: Uint8Array, publicKey: Uint8Array, hashfunc, signSchema: SignSchema): Uint8Array => { const c = nacl; - const d = prepareForScalarMult(sk, hashfunc, signSchema); + const d = prepareForScalarMult(privateKey, hashfunc, signSchema); - // sharedKey = pack(p = d (derived from sk) * q (derived from pk)) + // sharedKey = pack(p = d (derived from privateKey) * q (derived from publicKey)) const q = [c.gf(), c.gf(), c.gf(), c.gf()]; const p = [c.gf(), c.gf(), c.gf(), c.gf()]; - const sharedKey = new Uint8Array(Key_Size); - c.unpack(q, pk); + const sharedSecret = new Uint8Array(Key_Size); + + c.unpack(q, publicKey); c.scalarmult(p, q, d); - c.pack(sharedKey, p); - // salt the shared key - for (let i = 0; i < Key_Size; ++i) { - sharedKey[i] ^= salt[i]; - } - // return the hash of the result - const sharedKeyHash = new Uint8Array(Key_Size); - hashfunc(sharedKeyHash, sharedKey, Key_Size, signSchema); - return sharedKeyHash; + c.pack(sharedSecret, p); + return sharedSecret; }, }; })(); diff --git a/src/core/format/Convert.ts b/src/core/format/Convert.ts index 00102f8f82..d41850cb1d 100644 --- a/src/core/format/Convert.ts +++ b/src/core/format/Convert.ts @@ -86,7 +86,7 @@ export class Convert { * @param {Uint8Array} input A uint8 array. * @returns {string} A hex encoded string corresponding to the input. */ - public static uint8ToHex = (input) => { + public static uint8ToHex = (input): string => { let s = ''; for (const byte of input) { s += utilities.Nibble_To_Char_Map[byte >> 4]; diff --git a/src/infrastructure/BlockHttp.ts b/src/infrastructure/BlockHttp.ts index 597e9e73ed..c30b03939c 100644 --- a/src/infrastructure/BlockHttp.ts +++ b/src/infrastructure/BlockHttp.ts @@ -54,8 +54,8 @@ export class BlockHttp extends Http implements BlockRepository { * @param height - Block height * @returns Observable */ - public getBlockByHeight(height: string): Observable { - return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe( + public getBlockByHeight(height: UInt64): Observable { + return observableFrom(this.blockRoutesApi.getBlockByHeight(height.toString())).pipe( map(({body}) => this.toBlockInfo(body)), catchError((error) => throwError(this.errorHandling(error))), ); @@ -67,10 +67,10 @@ export class BlockHttp extends Http implements BlockRepository { * @param queryParams - (Optional) Query params * @returns Observable */ - public getBlockTransactions(height: string, + public getBlockTransactions(height: UInt64, queryParams?: QueryParams): Observable { return observableFrom( - this.blockRoutesApi.getBlockTransactions(height, + this.blockRoutesApi.getBlockTransactions(height.toString(), this.queryParams(queryParams).pageSize, this.queryParams(queryParams).id, this.queryParams(queryParams).order)) @@ -87,9 +87,9 @@ export class BlockHttp extends Http implements BlockRepository { * @param limit - Number of blocks returned. * @returns Observable */ - public getBlocksByHeightWithLimit(height: string, limit: number): Observable { + public getBlocksByHeightWithLimit(height: UInt64, limit: number): Observable { return observableFrom( - this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe( + this.blockRoutesApi.getBlocksByHeightWithLimit(height.toString(), limit)).pipe( map(({body}) => body.map((blockDTO) => this.toBlockInfo(blockDTO))), catchError((error) => throwError(this.errorHandling(error))), ); @@ -136,9 +136,9 @@ export class BlockHttp extends Http implements BlockRepository { * @param hash The hash of the transaction. * @return Observable */ - public getMerkleTransaction(height: string, hash: string): Observable { + public getMerkleTransaction(height: UInt64, hash: string): Observable { return observableFrom( - this.blockRoutesApi.getMerkleTransaction(height, hash)).pipe( + this.blockRoutesApi.getMerkleTransaction(height.toString(), hash)).pipe( map(({body}) => new MerkleProofInfo( body.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash)), )), diff --git a/src/infrastructure/BlockRepository.ts b/src/infrastructure/BlockRepository.ts index cd606ef22a..c4c6d97af1 100644 --- a/src/infrastructure/BlockRepository.ts +++ b/src/infrastructure/BlockRepository.ts @@ -17,8 +17,8 @@ import {Observable} from 'rxjs'; import {BlockInfo} from '../model/blockchain/BlockInfo'; import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo'; -import { Statement } from '../model/receipt/Statement'; import {Transaction} from '../model/transaction/Transaction'; +import { UInt64 } from '../model/UInt64'; import {QueryParams} from './QueryParams'; /** @@ -33,7 +33,7 @@ export interface BlockRepository { * @param height - Block height * @returns Observable */ - getBlockByHeight(height: string): Observable; + getBlockByHeight(height: UInt64): Observable; /** * Gets array of transactions included in a block for a block height @@ -41,7 +41,7 @@ export interface BlockRepository { * @param queryParams - (Optional) Query params * @returns Observable */ - getBlockTransactions(height: string, + getBlockTransactions(height: UInt64, queryParams?: QueryParams): Observable; /** @@ -51,7 +51,7 @@ export interface BlockRepository { * @returns Observable */ - getBlocksByHeightWithLimit(height: string, limit: number): Observable; + getBlocksByHeightWithLimit(height: UInt64, limit: number): Observable; /** * Get the merkle path for a given a transaction and block @@ -63,5 +63,5 @@ export interface BlockRepository { * @param hash The hash of the transaction. * @return Observable */ - getMerkleTransaction(height: string, hash: string): Observable; + getMerkleTransaction(height: UInt64, hash: string): Observable; } diff --git a/src/infrastructure/ReceiptHttp.ts b/src/infrastructure/ReceiptHttp.ts index 95ae940ae7..dad400a03a 100644 --- a/src/infrastructure/ReceiptHttp.ts +++ b/src/infrastructure/ReceiptHttp.ts @@ -21,6 +21,7 @@ import { MerklePathItem } from '../model/blockchain/MerklePathItem'; import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo'; import { NetworkType } from '../model/blockchain/NetworkType'; import { Statement } from '../model/receipt/Statement'; +import { UInt64 } from '../model/UInt64'; import { Http } from './Http'; import { CreateStatementFromDTO } from './receipt/CreateReceiptFromDTO'; import { ReceiptRepository } from './ReceiptRepository'; @@ -64,9 +65,9 @@ export class ReceiptHttp extends Http implements ReceiptRepository { * @param hash The hash of the receipt statement or resolution. * @return Observable */ - public getMerkleReceipts(height: string, hash: string): Observable { + public getMerkleReceipts(height: UInt64, hash: string): Observable { return observableFrom( - this.receiptRoutesApi.getMerkleReceipts(height, hash)).pipe( + this.receiptRoutesApi.getMerkleReceipts(height.toString(), hash)).pipe( map(({body}) => new MerkleProofInfo( body.merklePath!.map( (payload) => new MerklePathItem(payload.position, payload.hash)), @@ -81,10 +82,10 @@ export class ReceiptHttp extends Http implements ReceiptRepository { * @param queryParams - (Optional) Query params * @returns Observable */ - public getBlockReceipts(height: string): Observable { + public getBlockReceipts(height: UInt64): Observable { return this.networkTypeObservable.pipe( mergeMap((networkType) => observableFrom( - this.receiptRoutesApi.getBlockReceipts(height)).pipe( + this.receiptRoutesApi.getBlockReceipts(height.toString())).pipe( map(({body}) => CreateStatementFromDTO(body, networkType)), catchError((error) => throwError(this.errorHandling(error))), ), diff --git a/src/infrastructure/ReceiptRepository.ts b/src/infrastructure/ReceiptRepository.ts index a9e98df384..4c0660a63f 100644 --- a/src/infrastructure/ReceiptRepository.ts +++ b/src/infrastructure/ReceiptRepository.ts @@ -17,6 +17,7 @@ import {Observable} from 'rxjs'; import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo'; import { Statement } from '../model/receipt/Statement'; +import { UInt64 } from '../model/UInt64'; /** * Receipt interface repository. @@ -28,10 +29,10 @@ export interface ReceiptRepository { /** * Get receipts from a block * Returns the receipts linked to a block. - * @param {string} height The height of the block. + * @param {UInt64} height The height of the block. * @return Observable */ - getBlockReceipts(height: string): Observable; + getBlockReceipts(height: UInt64): Observable; /** * Get the merkle path for a given a receipt statement hash and block @@ -43,5 +44,5 @@ export interface ReceiptRepository { * @param hash The hash of the receipt statement or resolution. * @return Observable */ - getMerkleReceipts(height: string, hash: string): Observable; + getMerkleReceipts(height: UInt64, hash: string): Observable; } diff --git a/src/infrastructure/RepositoryFactoryHttp.ts b/src/infrastructure/RepositoryFactoryHttp.ts index 85357ed5d0..1bd08e7783 100644 --- a/src/infrastructure/RepositoryFactoryHttp.ts +++ b/src/infrastructure/RepositoryFactoryHttp.ts @@ -17,6 +17,7 @@ import { Observable, of as observableOf } from 'rxjs'; import { map, share, shareReplay } from 'rxjs/operators'; import { NetworkType } from '../model/blockchain/NetworkType'; +import { UInt64 } from '../model/UInt64'; import { AccountHttp } from './AccountHttp'; import { AccountRepository } from './AccountRepository'; import { BlockHttp } from './BlockHttp'; @@ -69,7 +70,7 @@ export class RepositoryFactoryHttp implements RepositoryFactory { this.url = url; this.networkType = networkType ? observableOf(networkType) : this.createNetworkRepository().getNetworkType().pipe(shareReplay(1)); this.generationHash = generationHash ? observableOf(generationHash) : - this.createBlockRepository().getBlockByHeight('1').pipe(map((b) => b.generationHash)).pipe(shareReplay(1)); + this.createBlockRepository().getBlockByHeight(UInt64.fromUint(1)).pipe(map((b) => b.generationHash)).pipe(shareReplay(1)); } createAccountRepository(): AccountRepository { diff --git a/src/model/message/MessageMarker.ts b/src/model/message/MessageMarker.ts index c66f96bc39..8dc4189a00 100644 --- a/src/model/message/MessageMarker.ts +++ b/src/model/message/MessageMarker.ts @@ -16,7 +16,7 @@ export class MessageMarker { /** - * 8-byte marker: FE CC 71 C7 64 BF E5 98 for PersistentDelegationRequestTransaction message + * 8-byte marker: FE2A8061577301E2 for PersistentDelegationRequestTransaction message */ - public static readonly PersistentDelegationUnlock = 'FECC71C764BFE598'; + public static readonly PersistentDelegationUnlock = 'FE2A8061577301E2'; } diff --git a/src/model/message/PersistentHarvestingDelegationMessage.ts b/src/model/message/PersistentHarvestingDelegationMessage.ts index 53c74678f0..a5d91a1d0a 100644 --- a/src/model/message/PersistentHarvestingDelegationMessage.ts +++ b/src/model/message/PersistentHarvestingDelegationMessage.ts @@ -16,11 +16,11 @@ import {Crypto, SHA3Hasher} from '../../core/crypto'; import { Convert } from '../../core/format/Convert'; +import { Account } from '../account/Account'; import { NetworkType } from '../blockchain/NetworkType'; import { Message } from './Message'; import { MessageMarker } from './MessageMarker'; import { MessageType } from './MessageType'; -import { PlainMessage } from './PlainMessage'; export class PersistentHarvestingDelegationMessage extends Message { constructor(payload: string) { @@ -33,18 +33,17 @@ export class PersistentHarvestingDelegationMessage extends Message { /** * * @param delegatedPrivateKey - Private key of delegated account - * @param senderPrivateKey - Sender private key * @param recipientPublicKey - Recipient public key * @param {NetworkType} networkType - Catapult network type * @return {PersistentHarvestingDelegationMessage} */ public static create(delegatedPrivateKey: string, - senderPrivateKey: string, recipientPublicKey: string, networkType: NetworkType): PersistentHarvestingDelegationMessage { const signSchema = SHA3Hasher.resolveSignSchema(networkType); - const encrypted = MessageMarker.PersistentDelegationUnlock + - Crypto.encode(senderPrivateKey, recipientPublicKey, delegatedPrivateKey, signSchema, true).toUpperCase(); + const ephemeralKeypair = Account.generateNewAccount(networkType); + const encrypted = MessageMarker.PersistentDelegationUnlock + ephemeralKeypair.publicKey + + Crypto.encode(ephemeralKeypair.privateKey, recipientPublicKey, delegatedPrivateKey, signSchema, true).toUpperCase(); return new PersistentHarvestingDelegationMessage(encrypted); } @@ -61,17 +60,17 @@ export class PersistentHarvestingDelegationMessage extends Message { * * @param encryptMessage - Encrypted message to be decrypted * @param privateKey - Recipient private key - * @param senderPublicKey - Sender public key * @param {NetworkType} networkType - Catapult network type * @return {string} */ public static decrypt(encryptMessage: PersistentHarvestingDelegationMessage, privateKey: string, - senderPublicKey: string, networkType: NetworkType): string { const signSchema = SHA3Hasher.resolveSignSchema(networkType); - const payload = encryptMessage.payload.substring(MessageMarker.PersistentDelegationUnlock.length); - const decrypted = Crypto.decode(privateKey, senderPublicKey, payload, signSchema); + const markerLength = MessageMarker.PersistentDelegationUnlock.length; + const ephemeralPublicKey = encryptMessage.payload.substring(markerLength, markerLength + 64); + const payload = encryptMessage.payload.substring(markerLength + ephemeralPublicKey.length); + const decrypted = Crypto.decode(privateKey, ephemeralPublicKey, payload, signSchema); return decrypted.toUpperCase(); } } diff --git a/src/model/receipt/ResolutionStatement.ts b/src/model/receipt/ResolutionStatement.ts index 5a3b50492a..e3b08cc047 100644 --- a/src/model/receipt/ResolutionStatement.ts +++ b/src/model/receipt/ResolutionStatement.ts @@ -14,8 +14,16 @@ * limitations under the License. */ -import { GeneratorUtils } from 'catbuffer'; +import { AddressDto, + AddressResolutionEntryBuilder, + AddressResolutionStatementBuilder, + MosaicIdDto, MosaicResolutionEntryBuilder, + MosaicResolutionStatementBuilder, + ReceiptSourceBuilder, + UnresolvedAddressDto, + UnresolvedMosaicIdDto } from 'catbuffer'; import { sha3_256 } from 'js-sha3'; +import { RawAddress } from '../../core/format/RawAddress'; import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping'; import { Address } from '../account/Address'; import { NetworkType } from '../blockchain/NetworkType'; @@ -68,19 +76,23 @@ export class ResolutionStatement { public generateHash(networkType: NetworkType): string { const type = this.resolutionType === ResolutionType.Address ? ReceiptType.Address_Alias_Resolution : ReceiptType.Mosaic_Alias_Resolution; - const unresolvedBytes = this.getUnresolvedBytes(this.resolutionType, networkType); + const builder = this.resolutionType === ResolutionType.Address ? new AddressResolutionStatementBuilder( + ReceiptVersion.RESOLUTION_STATEMENT, type.valueOf(), + new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.unresolved as Address | NamespaceId, networkType)), + this.resolutionEntries.map((entry) => new AddressResolutionEntryBuilder( + new ReceiptSourceBuilder(entry.source.primaryId, entry.source.secondaryId), + new AddressDto(RawAddress.stringToAddress((entry.resolved as Address).plain())), + )), + ) : new MosaicResolutionStatementBuilder(ReceiptVersion.RESOLUTION_STATEMENT, + type.valueOf(), + new UnresolvedMosaicIdDto(UInt64.fromHex((this.unresolved as MosaicId | NamespaceId).toHex()).toDTO()), + this.resolutionEntries.map((entry) => new MosaicResolutionEntryBuilder( + new ReceiptSourceBuilder(entry.source.primaryId, entry.source.secondaryId), + new MosaicIdDto((entry.resolved as MosaicId).toDTO()), + )), + ); const hasher = sha3_256.create(); - hasher.update(GeneratorUtils.uintToBuffer(ReceiptVersion.RESOLUTION_STATEMENT, 2)); - hasher.update(GeneratorUtils.uintToBuffer(type, 2)); - hasher.update(unresolvedBytes); - - let entryBytes = Uint8Array.from([]); - this.resolutionEntries.forEach((entry) => { - const bytes = entry.serialize(); - entryBytes = GeneratorUtils.concatTypedArrays(entryBytes, bytes); - }); - - hasher.update(entryBytes); + hasher.update(builder.serialize()); return hasher.hex().toUpperCase(); } @@ -195,18 +207,4 @@ export class ResolutionStatement { return Math.max(...this.resolutionEntries .map((entry) => primaryId >= entry.source.primaryId ? entry.source.primaryId : 0)); } - - /** - * @internal - * Generate buffer for unresulved - * @param {resolutionType} The resolution Type - * @param {networkType} the network type serialized in the output. - * @return {Uint8Array} - */ - private getUnresolvedBytes(resolutionType: ResolutionType, networkType: NetworkType): Uint8Array { - if (resolutionType === ResolutionType.Address) { - return UnresolvedMapping.toUnresolvedAddressBytes(this.unresolved as Address | NamespaceId, networkType); - } - return GeneratorUtils.uint64ToBuffer(UInt64.fromHex((this.unresolved as MosaicId | NamespaceId).toHex()).toDTO()); - } } diff --git a/src/model/transaction/PersistentDelegationRequestTransaction.ts b/src/model/transaction/PersistentDelegationRequestTransaction.ts index 20db9d713a..93e52137f6 100644 --- a/src/model/transaction/PersistentDelegationRequestTransaction.ts +++ b/src/model/transaction/PersistentDelegationRequestTransaction.ts @@ -28,7 +28,6 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction * @param deadline - The deadline to include the transaction. * @param delegatedPrivateKey - The private key of delegated account * @param recipientPublicKey - The recipient public key - * @param senderPrivateKey - The sender's private key * @param networkType - The network type. * @param maxFee - (Optional) Max fee defined by the sender * @returns {TransferTransaction} @@ -37,11 +36,10 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction deadline: Deadline, delegatedPrivateKey: string, recipientPublicKey: string, - senderPrivateKey: string, networkType: NetworkType, maxFee: UInt64 = new UInt64([0, 0])): PersistentDelegationRequestTransaction { const message = PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, senderPrivateKey, recipientPublicKey, networkType); + .create(delegatedPrivateKey, recipientPublicKey, networkType); return super.create(deadline, Address.createFromPublicKey(recipientPublicKey, networkType), [], message, networkType, maxFee); } } diff --git a/src/service/MosaicRestrictionTransactionService.ts b/src/service/MosaicRestrictionTransactionService.ts index bbc2b3bfc0..e068ea4df9 100644 --- a/src/service/MosaicRestrictionTransactionService.ts +++ b/src/service/MosaicRestrictionTransactionService.ts @@ -168,7 +168,6 @@ export class MosaicRestrictionTransactionService { private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable { return this.restrictionMosaicRepository.getMosaicGlobalRestriction(mosaicId).pipe( map((mosaicRestriction: MosaicGlobalRestriction) => { - console.log(mosaicRestriction.restrictions); return mosaicRestriction.restrictions.get(restrictionKey.toString()); }), catchError((err: Error) => { diff --git a/src/service/TransactionService.ts b/src/service/TransactionService.ts index 902a77496f..fc2aa8da73 100644 --- a/src/service/TransactionService.ts +++ b/src/service/TransactionService.ts @@ -181,7 +181,7 @@ export class TransactionService implements ITransactionService { * @return {Observable} */ private resolvedFromReceipt(transaction: Transaction, aggregateIndex: number): Observable { - return this.receiptRepository.getBlockReceipts(transaction.transactionInfo!.height.toString()).pipe( + return this.receiptRepository.getBlockReceipts(transaction.transactionInfo!.height).pipe( map((statement) => transaction.resolveAliases(statement, aggregateIndex)), ); } diff --git a/test/core/crypto/hkdf.spec.ts b/test/core/crypto/hkdf.spec.ts new file mode 100644 index 0000000000..55b128039f --- /dev/null +++ b/test/core/crypto/hkdf.spec.ts @@ -0,0 +1,30 @@ +/* + * Copyright 2020 NEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { expect } from 'chai'; +import { Convert } from '../../../src/core/format'; + +const hkdf = require('futoin-hkdf'); + +describe('hkdf', () => { + describe('Example tests', () => { + // This test is used to validate that values are the same in both java and ts implementation. + const sharedSecret = 'string-or-buffer'; + const hash = 'SHA-256'; + const info = 'catapult'; + const sharedKey = hkdf(sharedSecret, 32, {salt: new Uint8Array(32), info, hash}); + expect(Convert.uint8ToHex(sharedKey)).equal('E618ACB2558E1721492E4AE3BED3F4D86F26C2B0CE6AD939943A6A540855D23F'); + }); +}); diff --git a/test/core/crypto/keyPair.spec.ts b/test/core/crypto/keyPair.spec.ts index 60e968b17e..3ba607d357 100644 --- a/test/core/crypto/keyPair.spec.ts +++ b/test/core/crypto/keyPair.spec.ts @@ -13,8 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {expect} from 'chai'; -import {Crypto, KeyPair, SHA3Hasher, SignSchema} from '../../../src/core/crypto'; +import { expect } from 'chai'; +import { Crypto, KeyPair, SHA3Hasher, SignSchema } from '../../../src/core/crypto'; +import * as Utility from '../../../src/core/crypto/Utilities'; import { Convert } from '../../../src/core/format/Convert'; import { NetworkType } from '../../../src/model/blockchain/NetworkType'; @@ -27,22 +28,22 @@ describe('key pair', () => { const nis1TestSignSchema = SHA3Hasher.resolveSignSchema(NetworkType.TEST_NET); const Private_Keys = [ - '8D31B712AB28D49591EAF5066E9E967B44507FC19C3D54D742F7B3A255CFF4AB', - '15923F9D2FFFB11D771818E1F7D7DDCD363913933264D58533CB3A5DD2DAA66A', - 'A9323CEF24497AB770516EA572A0A2645EE2D5A75BC72E78DE534C0A03BC328E', - 'D7D816DA0566878EE739EDE2131CD64201BCCC27F88FA51BA5815BCB0FE33CC8', - '27FC9998454848B987FAD89296558A34DEED4358D1517B953572F3E0AAA0A22D', + '575dbb3062267eff57c970a336ebbc8fbcfe12c5bd3ed7bc11eb0481d7704ced'.toUpperCase(), + '5b0e3fa5d3b49a79022d7c1e121ba1cbbf4db5821f47ab8c708ef88defc29bfe'.toUpperCase(), + '738ba9bb9110aea8f15caa353aca5653b4bdfca1db9f34d0efed2ce1325aeeda'.toUpperCase(), + 'e8bf9bc0f35c12d8c8bf94dd3a8b5b4034f1063948e3cc5304e55e31aa4b95a6'.toUpperCase(), + 'c325ea529674396db5675939e7988883d59a5fc17a28ca977e3ba85370232a83'.toUpperCase(), ]; describe('construction', () => { it('can extract from private key test vectors', () => { // Arrange: const Expected_Public_Keys = [ - '53C659B47C176A70EB228DE5C0A0FF391282C96640C2A42CD5BBD0982176AB1B', - '3FE4A1AA148F5E76891CE924F5DC05627A87047B2B4AD9242C09C0ECED9B2338', - 'F398C0A2BDACDBD7037D2F686727201641BBF87EF458F632AE2A04B4E8F57994', - '6A283A241A8D8203B3A1E918B1E6F0A3E14E75E16D4CFFA45AE4EF89E38ED6B5', - '4DC62B38215826438DE2369743C6BBE6D13428405025DFEFF2857B9A9BC9D821', + 'BD8D3F8B7E1B3839C650F458234AB1FF87CDB1EDA36338D9E446E27D454717F2', + '26821636A618FD524A3AB57276EFC36CAF787DF19EE00F60035CE376A18E8C47', + 'DFC7F40FC549AC8BB2EF097600103FF457A1D7DC5755D434474761459B030E6F', + '96C7AB358EBB91104322C56435642BD939A77432286B229372987FC366EA319F', + '9488CFB5D7D439213B11FA80C1B57E8A7AB7E41B64CBA18A89180D412C04915C', ]; // Sanity: @@ -97,11 +98,11 @@ describe('key pair', () => { ]; const Expected_Public_Keys = [ - 'c5f54ba980fcbb657dbaaa42700539b207873e134d2375efeab5f1ab52f87844', - '96eb2a145211b1b7ab5f0d4b14f8abc8d695c7aee31a3cfc2d4881313c68eea3', - '2d8425e4ca2d8926346c7a7ca39826acd881a8639e81bd68820409c6e30d142a', - '4feed486777ed38e44c489c7c4e93a830e4c4a907fa19a174e630ef0f6ed0409', - '83ee32e4e145024d29bca54f71fa335a98b3e68283f1a3099c4d4ae113b53e54', + 'D6C3845431236C5A5A907A9E45BD60DA0E12EFD350B970E7F58E3499E2E7A2F0', + 'F3BD51ADD90A7BE8ED81F64EEE9456AF3B38478275B17EABE1853DFCFD3BF2CD', + '017CB008D00E41D17A6A09A6BE5C65C89E1E28706A621B0791B270E4F6182CC3', + '60068A23A0893538B5C364CAC86CB8670668BE84AEE2B6FCFF83FDF39A03F822', + 'AE054EF2A458A0CEE6B34A1DD32597A9236C4D453040B9AF58B5AE22A73024B7', ]; // Sanity: @@ -109,7 +110,7 @@ describe('key pair', () => { for (let i = 0; i < Nis1_Private_Key.length; ++i) { // Arrange: - const privateKeyHex = Convert.uint8ToHex(Convert.hexToUint8Reverse(Nis1_Private_Key[i])); + const privateKeyHex = Nis1_Private_Key[i]; const expectedPublicKey = Expected_Public_Keys[i]; // Act: @@ -279,16 +280,16 @@ describe('key pair', () => { 'd2488e854dbcdfdb2c9d16c8c0b2fdbc0abb6bac991bfe2b14d359a6bc99d66c00fd60d731ae06d0', ]; const Expected_Signature = [ - 'd9cec0cc0e3465fab229f8e1d6db68ab9cc99a18cb0435f70deb6100948576cd5c0aa1feb550bdd8693ef81eb10a556a622db1f9301986827b96716a7134230c', - '98bca58b075d1748f1c3a7ae18f9341bc18e90d1beb8499e8a654c65d8a0b4fbd2e084661088d1e5069187a2811996ae31f59463668ef0f8cb0ac46a726e7902', - 'ef257d6e73706bb04878875c58aa385385bf439f7040ea8297f7798a0ea30c1c5eff5ddc05443f801849c68e98111ae65d088e726d1d9b7eeca2eb93b677860c', - '0c684e71b35fed4d92b222fc60561db34e0d8afe44bdd958aaf4ee965911bef5991236f3e1bced59fc44030693bcac37f34d29e5ae946669dc326e706e81b804', - '6f17f7b21ef9d6907a7ab104559f77d5a2532b557d95edffd6d88c073d87ac00fc838fc0d05282a0280368092a4bd67e95c20f3e14580be28d8b351968c65e03', + '62D65D4728E07489152A45FC66BF680A85674AA1D971F265C02AC45E09EB115791FFA524C28F9A3238A18751C314EDC0CDD7124A1BDEE1B1414DE7365F02950B', + 'E48FFDE4FE2298E6DE06CE1FC0006A68544B5A8CEA4F93CFAD3E671720A010092FBB4E2DE3B7414C4E09766129BA26BA30A06A7676269B8868E55913779D4B0E', + 'C65ECD2E25797A92CA6BB92DC817ABCABA9B40F19F181A77150222F8C8519391B5FA713DE495040E9DE1354ADCE35485F2C56608DAD30A94FA0EAAE468651308', + 'C8B018ACB41191A09B354FCA3C8A207EDA8875F3FE6778B90288E67AA4648C9FF183E6576B452F6DC88B1D31E369BE5AF79A96FE4520EC40D156232B7B08F900', + '5A3CDDAC6E61DB8A614C8475A5B55EF0EC35C5206FA3EE4057CEADDD0E101A2EC4AFCBC688F8CF3488AB25FA5B6C879CEE59F4A1F51115383EEA2F9A9E1D820C', ]; for (let i = 0; i < Nis1_Private_Key.length; ++i) { // Arrange: - const privateKey = Convert.uint8ToHex(Convert.hexToUint8Reverse(Nis1_Private_Key[i])); + const privateKey = Nis1_Private_Key[i]; const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKey, nis1TestSignSchema); const payload = Convert.hexToUint8(Nis1_Data[i]); @@ -538,112 +539,16 @@ describe('key pair', () => { }); }); - describe('test vectors', () => { - const Input_Data = [ - '8ce03cd60514233b86789729102ea09e867fc6d964dea8c2018ef7d0a2e0e24bf7e348e917116690b9', - 'e4a92208a6fc52282b620699191ee6fb9cf04daf48b48fd542c5e43daa9897763a199aaa4b6f10546109f47ac3564fade0', - '13ed795344c4448a3b256f23665336645a853c5c44dbff6db1b9224b5303b6447fbf8240a2249c55', - 'a2704638434e9f7340f22d08019c4c8e3dbee0df8dd4454a1d70844de11694f4c8ca67fdcb08fed0cec9abb2112b5e5f89', - 'd2488e854dbcdfdb2c9d16c8c0b2fdbc0abb6bac991bfe2b14d359a6bc99d66c00fd60d731ae06d0', - ]; - const Expected_Signatures = [ - 'C9B1342EAB27E906567586803DA265CC15CCACA411E0AEF44508595ACBC47600D0' + - '2527F2EED9AB3F28C856D27E30C3808AF7F22F5F243DE698182D373A9ADE03', - '0755E437ED4C8DD66F1EC29F581F6906AB1E98704ECA94B428A25937DF00EC6479' + - '6F08E5FEF30C6F6C57E4A5FB4C811D617FA661EB6958D55DAE66DDED205501', - '15D6585A2A456E90E89E8774E9D12FE01A6ACFE09936EE41271AA1FBE0551264A9' + - 'FF9329CB6FEE6AE034238C8A91522A6258361D48C5E70A41C1F1C51F55330D', - 'F6FB0D8448FEC0605CF74CFFCC7B7AE8D31D403BCA26F7BD21CB4AC87B00769E9C' + - 'C7465A601ED28CDF08920C73C583E69D621BA2E45266B86B5FCF8165CBE309', - 'E88D8C32FE165D34B775F70657B96D8229FFA9C783E61198A6F3CCB92F487982D0' + - '8F8B16AB9157E2EFC3B78F126088F585E26055741A9F25127AC13E883C9A05', - ]; - - function assertCanSignTestVectors(dataTransform) { - // Sanity: - expect(Private_Keys.length).equal(Input_Data.length); - expect(Private_Keys.length).equal(Expected_Signatures.length); - - for (let i = 0; i < Private_Keys.length; ++i) { - // Arrange: - const inputData = dataTransform(Input_Data[i]); - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[i], mijinTestSignSchema); - - // Act: - const signature = KeyPair.sign(keyPair, inputData, mijinTestSignSchema); - - // Assert: - const message = `signing with ${Private_Keys[i]}`; - expect(Convert.uint8ToHex(signature), message).equal(Expected_Signatures[i]); - } - } - - it('can sign test vectors as hex string', () => { - // Assert: - assertCanSignTestVectors((data) => data); - }); - - it('can sign test vectors as binary', () => { - // Assert: - assertCanSignTestVectors((data) => Convert.hexToUint8(data)); - }); - - function assertCanVerifyTestVectors(dataTransform) { - // Sanity: - expect(Private_Keys.length).equal(Input_Data.length); - expect(Private_Keys.length).equal(Expected_Signatures.length); - - for (let i = 0; i < Private_Keys.length; ++i) { - // Arrange: - const inputData = dataTransform(Input_Data[i]); - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[i], mijinTestSignSchema); - const signature = KeyPair.sign(keyPair, inputData, mijinTestSignSchema); - - // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, inputData, signature, mijinTestSignSchema); - - // Assert: - const message = `verifying with ${Private_Keys[i]}`; - expect(isVerified, message).equal(true); - } - } - - it('can verify test vectors as hex string', () => { - // Assert: - assertCanVerifyTestVectors((data) => data); - }); - - it('can verify test vectors as binary', () => { - // Assert: - assertCanVerifyTestVectors((data) => Convert.hexToUint8(data)); - }); - }); - describe('derive shared key', () => { - const Salt_Size = 32; - - it('fails if salt is wrong size', () => { - // Arrange: create a salt that is too long - const keyPair = randomKeyPair(); - const publicKey = Crypto.randomBytes(32); - const salt = Crypto.randomBytes(Salt_Size + 1); - - // Act: - expect(() => { - KeyPair.deriveSharedKey(keyPair, publicKey, salt, mijinTestSignSchema); - }) - .to.throw('salt has unexpected size'); - }); it('derives same shared key for both partners', () => { // Arrange: const keyPair1 = randomKeyPair(); const keyPair2 = randomKeyPair(); - const salt = Crypto.randomBytes(Salt_Size); // Act: - const sharedKey1 = KeyPair.deriveSharedKey(keyPair1, keyPair2.publicKey, salt, mijinTestSignSchema); - const sharedKey2 = KeyPair.deriveSharedKey(keyPair2, keyPair1.publicKey, salt, mijinTestSignSchema); + const sharedKey1 = KeyPair.deriveSharedKey(keyPair1, keyPair2.publicKey, mijinTestSignSchema); + const sharedKey2 = KeyPair.deriveSharedKey(keyPair2, keyPair1.publicKey, mijinTestSignSchema); // Assert: expect(sharedKey1).to.deep.equal(sharedKey2); @@ -654,11 +559,10 @@ describe('key pair', () => { const keyPair = randomKeyPair(); const publicKey1 = Crypto.randomBytes(32); const publicKey2 = Crypto.randomBytes(32); - const salt = Crypto.randomBytes(Salt_Size); // Act: - const sharedKey1 = KeyPair.deriveSharedKey(keyPair, publicKey1, salt, mijinTestSignSchema); - const sharedKey2 = KeyPair.deriveSharedKey(keyPair, publicKey2, salt, mijinTestSignSchema); + const sharedKey1 = KeyPair.deriveSharedKey(keyPair, publicKey1, mijinTestSignSchema); + const sharedKey2 = KeyPair.deriveSharedKey(keyPair, publicKey2, mijinTestSignSchema); // Assert: expect(sharedKey1).to.not.deep.equal(sharedKey2); @@ -669,13 +573,12 @@ describe('key pair', () => { const keyPair = KeyPair.createKeyPairFromPrivateKeyString( '8F545C2816788AB41D352F236D80DBBCBC34705B5F902EFF1F1D88327C7C1300', mijinTestSignSchema); const publicKey = Convert.hexToUint8('BF684FB1A85A8C8091EE0442EDDB22E51683802AFA0C0E7C6FE3F3E3E87A8D72'); - const salt = Convert.hexToUint8('422C39DF16AAE42A74A5597D6EE2D59CFB4EEB6B3F26D98425B9163A03DAA3B5'); // Act: - const sharedKey = KeyPair.deriveSharedKey(keyPair, publicKey, salt, mijinTestSignSchema); + const sharedKey = KeyPair.deriveSharedKey(keyPair, publicKey, mijinTestSignSchema); // Assert: - expect(Convert.uint8ToHex(sharedKey)).to.equal('007FD607264C64C7BB83509E7CFA96E0FEAF34A373CDA75FACAA4DE9E141257B'); + expect(Convert.uint8ToHex(sharedKey)).to.equal('9E2DF6A12BABD5673F02C5B81898A03AC2E2D152639502E3DCECEF7C91C115F6'); }); }); @@ -687,50 +590,50 @@ describe('key pair', () => { // Arrange: create a salt that is too long // Arrange: const Nis1_Private_Key = [ + '00137c7c32881d1fff2e905f5b7034bcbcdb806d232f351db48a7816285c548f', 'e8857f8e488d4e6d4b71bcd44bb4cff49208c32651e1f6500c3b58cafeb8def6', 'd7f67b5f52cbcd1a1367e0376a8eb1012b634acfcf35e8322bae8b22bb9e8dea', 'd026ddb445fb3bbf3020e4b55ed7b5f9b7fd1278c34978ca1a6ed6b358dadbae', 'c522b38c391d1c3fa539cc58802bc66ac34bb3c73accd7f41b47f539bedcd016', - '2f1b82be8e65d610a4e588f000a89a733a9de98ffc1ac9d55e138b3b0a855da0', ]; const Nis1_Public_Keys = [ - '9d8e5f200b05a2638fb084a375408cabd6d5989590d96e3eea5f2cb34668178e', - '9735c92d150dcee0ade5a8d1822f46a4db22c9cda25f33773ae856fe374a3e8a', - 'd19e6beca3b26b9d1abc127835ebeb7a6c19c33dec8ec472e1c4d458202f4ec8', - 'ea5b6a0053237f7712b1d2347c447d3e83e0f2191762d07e1f53f8eb7f2dfeaa', - '65aeda1b47f66c978a4a41d4dcdfbd8eab5cdeb135695c2b0c28f54417b1486d', + '4C3B71636D3088ED3DF93D81E9169604EF0D4D68107BE0B446715DC12096243B', + '3B2BE0C315CDA77876D70845FD355D46E6825BA26985ABAB3E07B65C71266126', + '536D5C32538C8E66623F2E4F0FA7E124EF0AF1F0DBC97338C61188F37C4937EA', + 'FA9AF9448E76D1D0B6EBE05336D813918315B4941F78C1CC5EA1D1573177022C', + '63ABC887EAD7B80247244E0C58CB7689C313444F4924278DBEA72DDB3391F9DA', ]; - const Nis1_Salt = [ - 'ad63ac08f9afc85eb0bf4f8881ca6eaa0215924c87aa2f137d56109bb76c6f98', - '96104f0a28f9cca40901c066cd435134662a3b053eb6c8df80ee0d05dc941963', - 'd8f94a0bbb1de80aea17aab42e2ffb982e73fc49b649a318479e951e392d8728', - '3f8c969678a8abdbfb76866a142c284a6f01636c1c1607947436e0d2c30d5245', - 'e66415c58b981c7f1f2b8f45a42149e9144616ff6de49ff83d97000ac6f6f992', + const Expected_ScalarMulResult = [ + 'FEB242779628761122E9FB1718512025040C683D69B8105BA1F383D47578B29F', + '277FD55F1A54F57F3E5B408CACA560360BA75F67AB954FB4E27A7ADBCAAB6719', + '8B38A30824E322555A748A5BEB67FBE0E0F1BD2B51635549F1F19CDF76BA0E9A', + '9342380F112C70B17B7254EFCD3FCF6F4F1FF844C7790426BF6EDC4C800BD0A0', + 'A5593308E182D6D86D633F18DF19D87FAA4EF447FD1904891600CEEF303FD591', ]; const Expected_Derived_Key = [ - '990a5f611c65fbcde735378bdec38e1039c5466503511e8c645bbe42795c752b', - 'b498aa21d4ba4879ea9fd4225e93bacc760dcd9c119f8f38ab0716457d1a6f85', - 'd012afe3d1d932304e613c91545bf571cf2c7281d6cafa8e81393a551f675540', - '7e27efa50eed1c2ac51a12089cbab6a192624709c7330c016d5bc9af146584c1', - 'bb4ab31c334e55d378937978c90bb33779b23cd5ef4c68342a394f4ec8fa1ada', + '1E203DF5CD53961AAF75B24EFD5548AB6F01F0E9EF1C0C721F6A0ECAAF91AC9A', + 'E77654455AACFADE2E3291E3B88191847D06295CAE7160D55FB32E6D86A48C17', + '0368A074CA19F446659DF7BA04B2CE92F6BDCB423510D69223BD75E3827F0893', + '2F58733AB16A170B8389C39F5BA10509E59EC803CF9168739C693D0E6035A402', + '28DB21A1EBA731E85B0A58109CA273AF4B461726F5A5F62951777C3DCCD9281C', ]; for (let i = 0; i < Nis1_Private_Key.length; ++i) { // Arrange: - const privateKey = Convert.uint8ToHex(Convert.hexToUint8Reverse(Nis1_Private_Key[i])); + const privateKey = Nis1_Private_Key[i]; const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKey, nis1TestSignSchema); const publicKey = Convert.hexToUint8(Nis1_Public_Keys[i]); - const salt = Convert.hexToUint8(Nis1_Salt[i]); // Act: - const sharedKey = Convert.uint8ToHex(KeyPair.deriveSharedKey(keyPair, publicKey, salt, nis1TestSignSchema)); + const sharedKey = Convert.uint8ToHex(KeyPair.deriveSharedKey(keyPair, publicKey, nis1TestSignSchema)); + const sharedSecret = Convert.uint8ToHex(Utility.catapult_crypto.deriveSharedSecret(keyPair.privateKey, publicKey, Utility.catapult_hash.func, nis1TestSignSchema)); // Assert: - const message = ` from ${Nis1_Private_Key[i]}`; expect(sharedKey.toUpperCase()).to.deep.equal(Expected_Derived_Key[i].toUpperCase()); + expect(sharedSecret.toUpperCase()).to.deep.equal(Expected_ScalarMulResult[i].toUpperCase()); } }); }); @@ -751,41 +654,41 @@ describe('key pair', () => { ]; const Public_Keys = [ - 'bf684fb1a85a8c8091ee0442eddb22e51683802afa0c0e7c6fe3f3e3e87a8d72', - '9d8e5f200b05a2638fb084a375408cabd6d5989590d96e3eea5f2cb34668178e', - '9735c92d150dcee0ade5a8d1822f46a4db22c9cda25f33773ae856fe374a3e8a', - 'd19e6beca3b26b9d1abc127835ebeb7a6c19c33dec8ec472e1c4d458202f4ec8', - 'ea5b6a0053237f7712b1d2347c447d3e83e0f2191762d07e1f53f8eb7f2dfeaa', + '134A753D1FBC098879D3EC04F67EF661B2976B15C6037F7961AA343DC23D9D5E', + '2B3CFEF43CEE139691F466643F779EC3CEBFC9B1AB92DBA87FF9AC8E5F49B454', + '31C960BF74D53DE0EA92E11DE39DC69D60F1133131F634C2C2595F05E35B687E', + 'FDA70F88C7985C5B7D8BDE4CD6C2162E1DF0C7F44555125B6EA54E87958F6722', + '9896A394B035E0F18855B2F6EE934E20ABADBF6B65B26E0C9329C283C1A9F980', ]; - const Salt = [ - '422c39df16aae42a74a5597d6ee2d59cfb4eeb6b3f26d98425b9163a03daa3b5', - 'ad63ac08f9afc85eb0bf4f8881ca6eaa0215924c87aa2f137d56109bb76c6f98', - '96104f0a28f9cca40901c066cd435134662a3b053eb6c8df80ee0d05dc941963', - 'd8f94a0bbb1de80aea17aab42e2ffb982e73fc49b649a318479e951e392d8728', - '3f8c969678a8abdbfb76866a142c284a6f01636c1c1607947436e0d2c30d5245', + const Expected_ScalarMulResult = [ + '4118C2B3058714A288A9E218B7E0F9CCAD74C2EC44884CABBE20A2A33DB1282F', + 'DA4BFF3709E40B4299988806435819D3E8E90ADE435EAC086EE295E0ADC3E0BB', + '81B15312FA6A87285C883FF205BA7E7DB319DEA1E7A79D1F78B93E73A156AA64', + '100221B07AB55C3260A7B7F12CDA0A8FA97998664C6509661A2E51417AF9C998', + 'EE74B025E7A304BD0D3F131EF1132D2C194568034B9A7DE93653327D6618215C', ]; const Expected_Derived_Key = [ - '32628d4ecf167487881de9e81466614a3442c1b1f6eb146ebe7ad69c37184696', - 'da30f8081c065f5c4e2d17a551af3634a63395991af9642c29a7bbc9312b98a5', - '58c9a027b30fcbec8ac7962df1eb81f27317ab6f39ada66be8d9453653a305af', - '58815adb5f8ef154c6091c65b28e3d6d5ea25da40040e7489ee05f65ecffa61c', - '8c677358d7512c4d53b0f5d59cff421625851322fae4c66e98f670c49c916f32', + '8ADE930F8352A5D2B42B575B698C8791834525CBB1A3C94F814DA676922EDBBD', + 'C831BAFDF301F3E7A2910557B14EAEE054F8072A5E3C58797DF2F7EAD6C2F8E2', + '9C885AA670354E517320403E6E613FD076F486A3C6F986F3D7605F36D542F705', + '857312941280887C38FA7077AA808C52C755A27EAEAC1220A2F404CFDABBA131', + '85F5C2F191D2E3A3833C63059566D11B8EA552775AE0002D237BC35B5439FB59', ]; for (let i = 0; i < Private_Key.length; ++i) { // Arrange: const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Key[i], mijinTestSignSchema); const publicKey = Convert.hexToUint8(Public_Keys[i]); - const salt = Convert.hexToUint8(Salt[i]); // Act: - const sharedKey = Convert.uint8ToHex(KeyPair.deriveSharedKey(keyPair, publicKey, salt, mijinTestSignSchema)); + const sharedKey = Convert.uint8ToHex(KeyPair.deriveSharedKey(keyPair, publicKey, mijinTestSignSchema)); + const sharedSecret = Convert.uint8ToHex(Utility.catapult_crypto.deriveSharedSecret(keyPair.privateKey, publicKey, Utility.catapult_hash.func, mijinTestSignSchema)); // Assert: - const message = ` from ${Private_Key[i]}`; expect(sharedKey.toUpperCase()).to.deep.equal(Expected_Derived_Key[i].toUpperCase()); + expect(sharedSecret.toUpperCase()).to.deep.equal(Expected_ScalarMulResult[i].toUpperCase()); } }); }); diff --git a/test/core/format/RawAddress.spec.ts b/test/core/format/RawAddress.spec.ts index e4a5e65798..242b1daeba 100644 --- a/test/core/format/RawAddress.spec.ts +++ b/test/core/format/RawAddress.spec.ts @@ -199,19 +199,19 @@ describe('address', () => { it('can create Address from NIS public Key', () => { // Arrange: const Public_Keys = [ - 'c5f54ba980fcbb657dbaaa42700539b207873e134d2375efeab5f1ab52f87844', - '96eb2a145211b1b7ab5f0d4b14f8abc8d695c7aee31a3cfc2d4881313c68eea3', - '2d8425e4ca2d8926346c7a7ca39826acd881a8639e81bd68820409c6e30d142a', - '4feed486777ed38e44c489c7c4e93a830e4c4a907fa19a174e630ef0f6ed0409', - '83ee32e4e145024d29bca54f71fa335a98b3e68283f1a3099c4d4ae113b53e54', + 'd6c3845431236c5a5a907a9e45bd60da0e12efd350b970e7f58e3499e2e7a2f0', + 'f3bd51add90a7be8ed81f64eee9456af3b38478275b17eabe1853dfcfd3bf2cd', + '017cb008d00e41d17a6a09a6be5c65c89e1e28706a621b0791b270e4f6182cc3', + '60068a23a0893538b5c364cac86cb8670668be84aee2b6fcff83fdf39a03f822', + 'ae054ef2a458a0cee6b34a1dd32597a9236c4d453040b9af58b5ae22a73024b7', ]; const Addresses = [ - 'NDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5JFXJ4R4', - 'NABHFGE5ORQD3LE4O6B7JUFN47ECOFBFASC3SCAC', - 'NAVOZX4HDVOAR4W6K4WJHWPD3MOFU27DFHC7KZOZ', - 'NBZ6JK5YOCU6UPSSZ5D3G27UHAPHTY5HDQMGE6TT', - 'NCQW2P5DNZ5BBXQVGS367DQ4AHC3RXOEVGRCLY6V', + 'NCFGSLITSWMRROU2GO7FPMIUUDELUPSZUNJABUMH', + 'NAAPGLKY7HZNQJYM4T6JEKM4NU6M2MOBIV3T4GNF', + 'NCF5QZSBRM2CXTXIG3YWXZ7X6NARCF5JMD55OCV6', + 'NC3U645TJAVWXXYOLRQIX7GIZEVZWLN5W44Z3KZT', + 'NBLWRN5WLGP5MUBKMDQCHK6BWJL2LSJRVMDT7HQD', ]; // Sanity: @@ -240,19 +240,19 @@ describe('address', () => { it('can create Address from NIS public Key', () => { // Arrange: const Public_Keys = [ - 'c5f54ba980fcbb657dbaaa42700539b207873e134d2375efeab5f1ab52f87844', - '96eb2a145211b1b7ab5f0d4b14f8abc8d695c7aee31a3cfc2d4881313c68eea3', - '2d8425e4ca2d8926346c7a7ca39826acd881a8639e81bd68820409c6e30d142a', - '4feed486777ed38e44c489c7c4e93a830e4c4a907fa19a174e630ef0f6ed0409', - '83ee32e4e145024d29bca54f71fa335a98b3e68283f1a3099c4d4ae113b53e54', + 'd6c3845431236c5a5a907a9e45bd60da0e12efd350b970e7f58e3499e2e7a2f0', + 'f3bd51add90a7be8ed81f64eee9456af3b38478275b17eabe1853dfcfd3bf2cd', + '017cb008d00e41d17a6a09a6be5c65c89e1e28706a621b0791b270e4f6182cc3', + '60068a23a0893538b5c364cac86cb8670668be84aee2b6fcff83fdf39a03f822', + 'ae054ef2a458a0cee6b34a1dd32597a9236c4d453040b9af58b5ae22a73024b7', ]; const Addresses = [ - 'TDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5KZPFMK2', - 'TABHFGE5ORQD3LE4O6B7JUFN47ECOFBFATE53N2I', - 'TAVOZX4HDVOAR4W6K4WJHWPD3MOFU27DFEJDR2PR', - 'TBZ6JK5YOCU6UPSSZ5D3G27UHAPHTY5HDQCDS5YA', - 'TCQW2P5DNZ5BBXQVGS367DQ4AHC3RXOEVFZOQCJ6', + 'TCFGSLITSWMRROU2GO7FPMIUUDELUPSZUNUEZF33', + 'TAAPGLKY7HZNQJYM4T6JEKM4NU6M2MOBIXGXU3O6', + 'TCF5QZSBRM2CXTXIG3YWXZ7X6NARCF5JMB6EXFEQ', + 'TC3U645TJAVWXXYOLRQIX7GIZEVZWLN5W7JQHW5U', + 'TBLWRN5WLGP5MUBKMDQCHK6BWJL2LSJRVO7ZCX3R', ]; // Sanity: diff --git a/test/infrastructure/RepositoryFactory.spec.ts b/test/infrastructure/RepositoryFactory.spec.ts index 5dfcbcfd05..e80f44bf9b 100644 --- a/test/infrastructure/RepositoryFactory.spec.ts +++ b/test/infrastructure/RepositoryFactory.spec.ts @@ -16,12 +16,13 @@ import { expect } from 'chai'; import { of as observableOf } from 'rxjs'; import { map } from 'rxjs/operators'; -import { instance, mock, when } from 'ts-mockito'; +import { deepEqual, instance, mock, when } from 'ts-mockito'; import { BlockRepository } from '../../src/infrastructure/BlockRepository'; import { NetworkRepository } from '../../src/infrastructure/NetworkRepository'; import { RepositoryFactoryHttp } from '../../src/infrastructure/RepositoryFactoryHttp'; import { BlockInfo } from '../../src/model/blockchain/BlockInfo'; import { NetworkType } from '../../src/model/blockchain/NetworkType'; +import { UInt64 } from '../../src/model/UInt64'; describe('RepositoryFactory', () => { it('Should create repositories', () => { @@ -52,7 +53,7 @@ describe('RepositoryFactory', () => { counter++; return v; })); - when(repositoryMock.getBlockByHeight('1')).thenReturn(observableOfBlockInfo); + when(repositoryMock.getBlockByHeight(deepEqual(UInt64.fromUint(1)))).thenReturn(observableOfBlockInfo); expect(observableOfBlockInfo).to.be.equals(observableOfBlockInfo); const repositoryFactory = new (class RepositoryFactoryHttpForTest extends RepositoryFactoryHttp { diff --git a/test/model/message/EncryptedMessage.spec.ts b/test/model/message/EncryptedMessage.spec.ts index 1d8e102cde..7031093396 100644 --- a/test/model/message/EncryptedMessage.spec.ts +++ b/test/model/message/EncryptedMessage.spec.ts @@ -56,7 +56,7 @@ describe('EncryptedMessage', () => { expect(plainMessage.payload).to.be.equal('Testing simple transfer'); }); - it('should return should return decrepted message reading from message payload', () => { + it('should return decrepted message reading from message payload', () => { const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; const transferTransaction = TransferTransaction.create( Deadline.create(), diff --git a/test/model/message/PersistentHarvestingDelegationMessage.spec.ts b/test/model/message/PersistentHarvestingDelegationMessage.spec.ts index 4eb0987a20..dfec7f2397 100644 --- a/test/model/message/PersistentHarvestingDelegationMessage.spec.ts +++ b/test/model/message/PersistentHarvestingDelegationMessage.spec.ts @@ -45,7 +45,7 @@ describe('PersistentHarvestingDelegationMessage', () => { it('should create a PersistentHarvestingDelegation message', () => { const encryptedMessage = PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST); + .create(delegatedPrivateKey, recipient.publicKey, NetworkType.MIJIN_TEST); expect(encryptedMessage.payload.length).to.be.equal(208); expect(encryptedMessage.type).to.be.equal(MessageType.PersistentHarvestingDelegationMessage); }); @@ -69,20 +69,19 @@ describe('PersistentHarvestingDelegationMessage', () => { it('should create and decrypt message', () => { const encryptedMessage = PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST); + .create(delegatedPrivateKey, recipient.publicKey, NetworkType.MIJIN_TEST); const plainMessage = - PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST); + PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient.privateKey, NetworkType.MIJIN_TEST); expect(plainMessage).to.be.equal(delegatedPrivateKey); }); - it('should return should return decrepted message reading from message payload', () => { + it('return decrepted message reading from message payload', () => { const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; const tx = PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), delegatedPrivateKey, recipient.publicKey, - sender.privateKey, NetworkType.MIJIN_TEST, ); const signedTransaction = tx.signWith(sender, generationHash); @@ -90,18 +89,17 @@ describe('PersistentHarvestingDelegationMessage', () => { PersistentHarvestingDelegationMessage .createFromPayload(signedTransaction.payload.substring(322, signedTransaction.payload.length)); const plainMessage = - PersistentHarvestingDelegationMessage.decrypt(encryptMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST); + PersistentHarvestingDelegationMessage.decrypt(encryptMessage, recipient.privateKey, NetworkType.MIJIN_TEST); expect(plainMessage).to.be.equal(delegatedPrivateKey); }); it('should encrypt and decrypt message using NIS1 schema', () => { const encryptedMessage = PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, sender_nis.privateKey, recipient_nis.publicKey, NetworkType.TEST_NET); + .create(delegatedPrivateKey, recipient_nis.publicKey, NetworkType.TEST_NET); const plainMessage = PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient_nis.privateKey, - sender_nis.publicKey, NetworkType.TEST_NET); expect(plainMessage).to.be.equal(delegatedPrivateKey); }); diff --git a/test/model/receipt/Receipt.spec.ts b/test/model/receipt/Receipt.spec.ts index 2b0bb8ca00..4c81a6312f 100644 --- a/test/model/receipt/Receipt.spec.ts +++ b/test/model/receipt/Receipt.spec.ts @@ -347,14 +347,14 @@ describe('Receipt', () => { const statement = CreateStatementFromDTO(statementDTO, netWorkType); const receipt = statement.mosaicResolutionStatements[0]; const hash = receipt.generateHash(NetworkType.MAIN_NET); - expect(hash).to.be.equal('99381CE398D3AAE110FC97E984D7D35A710A5C525A4F959EC8916B382DE78A63'); + expect(hash).to.be.equal('DE29FB6356530E5D1FBEE0A84202520C155D882C46EA74456752D6C75F0707B3'); }); it('should generate hash for AddressResolutionStatement', () => { const statement = CreateStatementFromDTO(statementDTO, netWorkType); const receipt = statement.addressResolutionStatements[0]; const hash = receipt.generateHash(NetworkType.MAIN_NET); - expect(hash).to.be.equal('952225717E26295B97F9A35E719CA1319114CCF23C927BCBD14E7A7AA4BAC617'); + expect(hash).to.be.equal('EFE3D51BF14E861E6E219D1ADB5715D132D6AB7481B29726104933E517F7FBCC'); }); it('should generate hash for TransactionStatement', () => { diff --git a/test/model/transaction/Deadline.spec.ts b/test/model/transaction/Deadline.spec.ts index 17cda9bbb0..74620cff8d 100644 --- a/test/model/transaction/Deadline.spec.ts +++ b/test/model/transaction/Deadline.spec.ts @@ -53,7 +53,6 @@ describe('Deadline', () => { it('make sure epochAdjustment is correct', () => { const epochAdjustment = new Date(Deadline.timestampNemesisBlock * 1000); - console.log(epochAdjustment.toUTCString()); expect(epochAdjustment.getUTCFullYear()).to.be.equal(2019); expect(epochAdjustment.getUTCMonth() + 1).to.be.equal(11); diff --git a/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts b/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts index 473e87d9d0..db339164d2 100644 --- a/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts +++ b/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts @@ -18,6 +18,7 @@ import { expect } from 'chai'; import { Account } from '../../../src/model/account/Account'; import { Address } from '../../../src/model/account/Address'; import { NetworkType } from '../../../src/model/blockchain/NetworkType'; +import { MessageMarker } from '../../../src/model/message/MessageMarker'; import { Deadline } from '../../../src/model/transaction/Deadline'; import { PersistentDelegationRequestTransaction, @@ -30,7 +31,7 @@ describe('PersistentDelegationRequestTransaction', () => { const delegatedPrivateKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A'; const recipientPublicKey = '9DBF67474D6E1F8B131B4EB1F5BA0595AFFAE1123607BC1048F342193D7E669F'; const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; - const messageMarker = 'FECC71C764BFE598'; + const messageMarker = MessageMarker.PersistentDelegationUnlock; before(() => { account = TestingAccount; @@ -42,7 +43,6 @@ describe('PersistentDelegationRequestTransaction', () => { Deadline.create(), delegatedPrivateKey, recipientPublicKey, - account.privateKey, NetworkType.MIJIN_TEST, ); @@ -56,7 +56,6 @@ describe('PersistentDelegationRequestTransaction', () => { Deadline.create(), delegatedPrivateKey, recipientPublicKey, - account.privateKey, NetworkType.MIJIN_TEST, new UInt64([1, 0]), ); @@ -71,7 +70,6 @@ describe('PersistentDelegationRequestTransaction', () => { Deadline.create(), delegatedPrivateKey, recipientPublicKey, - account.privateKey, NetworkType.MIJIN_TEST, ); @@ -96,20 +94,6 @@ describe('PersistentDelegationRequestTransaction', () => { Deadline.create(), 'abc', recipientPublicKey, - account.privateKey, - NetworkType.MIJIN_TEST, - new UInt64([1, 0]), - ); - }).to.throw(); - }); - - it('should throw exception with invalid sender private key', () => { - expect(() => { - PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( - Deadline.create(), - delegatedPrivateKey, - recipientPublicKey, - 'abc', NetworkType.MIJIN_TEST, new UInt64([1, 0]), ); diff --git a/test/model/transaction/TransferTransaction.spec.ts b/test/model/transaction/TransferTransaction.spec.ts index 83e3d09d9b..6bdc150911 100644 --- a/test/model/transaction/TransferTransaction.spec.ts +++ b/test/model/transaction/TransferTransaction.spec.ts @@ -20,6 +20,7 @@ import { CreateTransactionFromPayload } from '../../../src/infrastructure/transa import { Account } from '../../../src/model/account/Account'; import { Address } from '../../../src/model/account/Address'; import { NetworkType } from '../../../src/model/blockchain/NetworkType'; +import { MessageMarker } from '../../../src/model/message/MessageMarker'; import { MessageType } from '../../../src/model/message/MessageType'; import { PersistentHarvestingDelegationMessage } from '../../../src/model/message/PersistentHarvestingDelegationMessage'; import { PlainMessage } from '../../../src/model/message/PlainMessage'; @@ -40,7 +41,7 @@ describe('TransferTransaction', () => { const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; const delegatedPrivateKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A'; const recipientPublicKey = '9DBF67474D6E1F8B131B4EB1F5BA0595AFFAE1123607BC1048F342193D7E669F'; - const messageMarker = 'FECC71C764BFE598'; + const messageMarker = MessageMarker.PersistentDelegationUnlock; let statement: Statement; const unresolvedAddress = new NamespaceId('address'); const unresolvedMosaicId = new NamespaceId('mosaic'); @@ -252,7 +253,7 @@ describe('TransferTransaction', () => { Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [], PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), + .create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); @@ -265,10 +266,9 @@ describe('TransferTransaction', () => { Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [], PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), + .create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); - expect(transferTransaction.message.payload.length).to.be.equal(192 + messageMarker.length); expect(transferTransaction.message.payload.includes(messageMarker)).to.be.true; expect(transferTransaction.mosaics.length).to.be.equal(0); @@ -291,7 +291,7 @@ describe('TransferTransaction', () => { Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [NetworkCurrencyMosaic.createRelative(100)], PersistentHarvestingDelegationMessage - .create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), + .create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); }).to.throw(Error, 'PersistentDelegationRequestTransaction should be created without Mosaic'); @@ -303,7 +303,7 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [NetworkCurrencyMosaic.createRelative(100)], - PersistentHarvestingDelegationMessage.create('abc', account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage.create('abc', recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); }).to.throw(); @@ -315,7 +315,7 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [NetworkCurrencyMosaic.createRelative(100)], - PersistentHarvestingDelegationMessage.create(delegatedPrivateKey, 'abc', recipientPublicKey, NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage.create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); }).to.throw();