diff --git a/e2e/infrastructure/MosaicHttp.spec.ts b/e2e/infrastructure/MosaicHttp.spec.ts index 4f7fbae6de..33afaecc6b 100644 --- a/e2e/infrastructure/MosaicHttp.spec.ts +++ b/e2e/infrastructure/MosaicHttp.spec.ts @@ -18,9 +18,9 @@ import { Listener, TransactionHttp } from '../../src/infrastructure/infrastructu import {MosaicHttp} from '../../src/infrastructure/MosaicHttp'; import { Account } from '../../src/model/account/Account'; import { NetworkType } from '../../src/model/blockchain/NetworkType'; +import { MosaicFlags } from '../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../src/model/mosaic/MosaicId'; import { MosaicNonce } from '../../src/model/mosaic/MosaicNonce'; -import { MosaicProperties } from '../../src/model/mosaic/MosaicProperties'; import { AliasAction } from '../../src/model/namespace/AliasAction'; import {NamespaceId} from '../../src/model/namespace/NamespaceId'; import { Deadline } from '../../src/model/transaction/Deadline'; @@ -74,12 +74,9 @@ describe('MosaicHttp', () => { Deadline.create(), nonce, mosaicId, - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(true, true, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); diff --git a/e2e/infrastructure/TransactionHttp.spec.ts b/e2e/infrastructure/TransactionHttp.spec.ts index ff29a8ea3d..2f3506d179 100644 --- a/e2e/infrastructure/TransactionHttp.spec.ts +++ b/e2e/infrastructure/TransactionHttp.spec.ts @@ -29,9 +29,9 @@ import { AccountRestrictionModificationAction } from '../../src/model/account/Ac import { AccountRestrictionType } from '../../src/model/account/AccountRestrictionType'; import {NetworkType} from '../../src/model/blockchain/NetworkType'; import { Mosaic } from '../../src/model/mosaic/Mosaic'; +import {MosaicFlags} from '../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../src/model/mosaic/MosaicId'; import {MosaicNonce} from '../../src/model/mosaic/MosaicNonce'; -import {MosaicProperties} from '../../src/model/mosaic/MosaicProperties'; import { MosaicRestrictionType } from '../../src/model/mosaic/MosaicRestrictionType'; import {MosaicSupplyChangeAction} from '../../src/model/mosaic/MosaicSupplyChangeAction'; import {NetworkCurrencyMosaic} from '../../src/model/mosaic/NetworkCurrencyMosaic'; @@ -143,23 +143,20 @@ describe('TransactionHttp', () => { Deadline.create(), nonce, mosaicId, - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create( true, true, true), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); listener.confirmed(account.address).subscribe((transaction: MosaicDefinitionTransaction) => { expect(transaction.mosaicId, 'MosaicId').not.to.be.undefined; expect(transaction.nonce, 'Nonce').not.to.be.undefined; - expect(transaction.mosaicProperties.divisibility, 'Divisibility').not.to.be.undefined; - expect(transaction.mosaicProperties.duration, 'Duration').not.to.be.undefined; - expect(transaction.mosaicProperties.supplyMutable, 'SupplyMutable').not.to.be.undefined; - expect(transaction.mosaicProperties.transferable, 'Transferable').not.to.be.undefined; + expect(transaction.divisibility, 'Divisibility').not.to.be.undefined; + expect(transaction.duration, 'Duration').not.to.be.undefined; + expect(transaction.flags.supplyMutable, 'SupplyMutable').not.to.be.undefined; + expect(transaction.flags.transferable, 'Transferable').not.to.be.undefined; + expect(transaction.flags.restrictable, 'Restrictable').not.to.be.undefined; done(); }); listener.status(account.address).subscribe((error) => { @@ -186,13 +183,9 @@ describe('TransactionHttp', () => { Deadline.create(), nonce, MosaicId.createFromNonce(nonce, account.publicAccount), - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - duration: UInt64.fromUint(0), - restrictable: true, - }), + MosaicFlags.create( true, true, true), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), diff --git a/src/core/crypto/Crypto.ts b/src/core/crypto/Crypto.ts index 86349dd888..992e3afee3 100644 --- a/src/core/crypto/Crypto.ts +++ b/src/core/crypto/Crypto.ts @@ -17,8 +17,9 @@ import { WalletAlgorithm } from '../../model/wallet/WalletAlgorithm'; import { Convert as convert } from '../format/Convert'; import { KeyPair } from './KeyPair'; -import * as utility from './Utilities'; import { SignSchema } from './SignSchema'; +import * as utility from './Utilities'; +// tslint:disable-next-line: no-var-requires const CryptoJS = require('crypto-js'); export class Crypto { /** @@ -219,10 +220,10 @@ export class Crypto { * @param {string} msg - A text message * @param {Uint8Array} iv - An initialization vector * @param {Uint8Array} salt - A salt - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @return {string} - The encoded message */ - public static _encode = (senderPriv, recipientPub, msg, iv, salt, signSchema: SignSchema = SignSchema.SHA3) => { + public static _encode = (senderPriv, recipientPub, msg, iv, salt, signSchema: SignSchema) => { // Errors if (!senderPriv || !recipientPub || !msg || !iv || !salt) { throw new Error('Missing argument !'); } // Processing @@ -244,10 +245,10 @@ export class Crypto { * @param {string} senderPriv - A sender private key * @param {string} recipientPub - A recipient public key * @param {string} msg - A text message - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @return {string} - The encoded message */ - public static encode = (senderPriv, recipientPub, msg, signSchema: SignSchema = SignSchema.SHA3) => { + public static encode = (senderPriv, recipientPub, msg, signSchema: SignSchema) => { // Errors if (!senderPriv || !recipientPub || !msg) { throw new Error('Missing argument !'); } // Processing @@ -264,10 +265,10 @@ export class Crypto { * @param {string} recipientPrivate - A recipient private key * @param {string} senderPublic - A sender public key * @param {Uint8Array} _payload - An encrypted message payload in bytes - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @return {string} - The decoded payload as hex */ - public static _decode = (recipientPrivate, senderPublic, payload, iv, salt, signSchema: SignSchema = SignSchema.SHA3) => { + public static _decode = (recipientPrivate, senderPublic, payload, iv, salt, signSchema: SignSchema) => { // Error if (!recipientPrivate || !senderPublic || !payload) { throw new Error('Missing argument !'); } // Processing @@ -290,19 +291,19 @@ export class Crypto { * * @param {string} recipientPrivate - A recipient private key * @param {string} senderPublic - A sender public key - * @param {string} _payload - An encrypted message payload - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {string} payload - An encrypted message payload + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @return {string} - The decoded payload as hex */ - public static decode = (recipientPrivate, senderPublic, _payload, signSchema: SignSchema = SignSchema.SHA3) => { + public static decode = (recipientPrivate, senderPublic, payload, signSchema: SignSchema) => { // Error - if (!recipientPrivate || !senderPublic || !_payload) { throw new Error('Missing argument !'); } + if (!recipientPrivate || !senderPublic || !payload) { throw new Error('Missing argument !'); } // Processing - const binPayload = convert.hexToUint8(_payload); - const payload = new Uint8Array(binPayload.buffer, 48); + 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, payload, iv, salt, signSchema); + const decoded = Crypto._decode(recipientPrivate, senderPublic, payloadBuffer, iv, salt, signSchema); return decoded; } diff --git a/src/core/crypto/KeyPair.ts b/src/core/crypto/KeyPair.ts index 5b2404206a..f82b5d85c5 100644 --- a/src/core/crypto/KeyPair.ts +++ b/src/core/crypto/KeyPair.ts @@ -13,6 +13,7 @@ * 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'; @@ -21,18 +22,15 @@ export class KeyPair { /** * Creates a key pair from a private key string. * @param {string} privateKeyString A hex encoded private key string. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {module:crypto/keyPair~KeyPair} The key pair. */ - public static createKeyPairFromPrivateKeyString = (privateKeyString, signSchema = SignSchema.SHA3) => { + public static createKeyPairFromPrivateKeyString = (privateKeyString: string, signSchema: SignSchema) => { const privateKey = convert.hexToUint8(privateKeyString); - - // KECCAK_REVERSED_KEY uses reversed private key. - const secretKey = signSchema === SignSchema.SHA3 ? privateKey : convert.hexToUint8Reverse(privateKeyString); if (Utility.Key_Size !== privateKey.length) { throw Error(`private key has unexpected size: ${privateKey.length}`); } - const publicKey = Utility.catapult_crypto.extractPublicKey(secretKey, Utility.catapult_hash.func, signSchema); + const publicKey = Utility.catapult_crypto.extractPublicKey(privateKey, Utility.catapult_hash.func, signSchema); return { privateKey, publicKey, @@ -43,16 +41,11 @@ export class KeyPair { * Signs a data buffer with a key pair. * @param {module:crypto/keyPair~KeyPair} keyPair The key pair to use for signing. * @param {Uint8Array} data The data to sign. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {Uint8Array} The signature. */ - public static sign = (keyPair, data, signSchema = SignSchema.SHA3) => { - let secretKey = keyPair.privateKey; - // KECCAK_REVERSED_KEY uses reversed private key. - if (signSchema === SignSchema.KECCAK_REVERSED_KEY) { - secretKey = convert.hexToUint8Reverse(convert.uint8ToHex(secretKey)); - } - return Utility.catapult_crypto.sign(data, keyPair.publicKey, secretKey, + public static sign = (keyPair, data: Uint8Array, signSchema: SignSchema) => { + return Utility.catapult_crypto.sign(data, keyPair.publicKey, keyPair.privateKey, Utility.catapult_hash.createHasher(64, signSchema)); } @@ -61,10 +54,10 @@ export class KeyPair { * @param {module:crypto/keyPair~PublicKey} 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_REVERSED_KEY / SHA3) + * @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, signature, signSchema = SignSchema.SHA3) => { + public static verify = (publicKey, data: Uint8Array, signature: Uint8Array, signSchema: SignSchema) => { return Utility.catapult_crypto.verify(publicKey, data, signature, Utility.catapult_hash.createHasher(64, signSchema)); } @@ -74,21 +67,16 @@ export class KeyPair { * @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_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {Uint8Array} The shared key. */ - public static deriveSharedKey = (keyPair, publicKey, salt, signSchema = SignSchema.SHA3) => { + public static deriveSharedKey = (keyPair, publicKey: Uint8Array, salt: Uint8Array, signSchema: SignSchema) => { if (Utility.Key_Size !== salt.length) { throw Error(`salt has unexpected size: ${salt.length}`); } if (Utility.Key_Size !== publicKey.length) { throw Error(`public key has unexpected size: ${salt.length}`); } - let secretKey = keyPair.privateKey; - // KECCAK_REVERSED_KEY uses reversed private key. - if (signSchema === SignSchema.KECCAK_REVERSED_KEY) { - secretKey = convert.hexToUint8Reverse(convert.uint8ToHex(secretKey)); - } - return Utility.catapult_crypto.deriveSharedKey(salt, secretKey, publicKey, Utility.catapult_hash.func, signSchema); + return Utility.catapult_crypto.deriveSharedKey(salt, keyPair.privateKey, publicKey, Utility.catapult_hash.func, signSchema); } } diff --git a/src/core/crypto/SHA3Hasher.ts b/src/core/crypto/SHA3Hasher.ts index 2416447b8b..fc58ccba7c 100644 --- a/src/core/crypto/SHA3Hasher.ts +++ b/src/core/crypto/SHA3Hasher.ts @@ -15,6 +15,7 @@ */ import { keccak256, keccak512, sha3_256, sha3_512 } from 'js-sha3'; +import { NetworkType } from '../../model/blockchain/NetworkType'; import { Convert as convert, RawArray as array } from '../format'; import { SignSchema } from './SignSchema'; @@ -24,9 +25,9 @@ export class SHA3Hasher { * @param {Uint8Array} dest The computed hash destination. * @param {Uint8Array} data The data to hash. * @param {numeric} length The hash length in bytes. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) */ - public static func = (dest, data, length, signSchema = SignSchema.SHA3) => { + public static func = (dest, data, length, signSchema: SignSchema) => { const hasher = SHA3Hasher.getHasher(length, signSchema); const hash = hasher.arrayBuffer(data); array.copy(dest, array.uint8View(hash)); @@ -35,10 +36,10 @@ export class SHA3Hasher { /** * Creates a hasher object. * @param {numeric} length The hash length in bytes. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {object} The hasher. */ - public static createHasher = (length = 64, signSchema = SignSchema.SHA3) => { + public static createHasher = (length = 64, signSchema: SignSchema) => { let hash; return { reset: () => { @@ -62,13 +63,27 @@ export class SHA3Hasher { /** * Get a hasher instance. * @param {numeric} length The hash length in bytes. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {SignSchema} signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult)) * @returns {object} The hasher. */ - public static getHasher = (length = 64, signSchema = SignSchema.SHA3) => { + public static getHasher = (length = 64, signSchema: SignSchema) => { return { 32: signSchema === SignSchema.SHA3 ? sha3_256 : keccak256, 64: signSchema === SignSchema.SHA3 ? sha3_512 : keccak512 , } [length]; } + + /** + * Resolve signature schema from given network type + * + * @param {NetworkType} networkType - Network type + * + * @return {SignSchema} + */ + public static resolveSignSchema(networkType: NetworkType): SignSchema { + if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) { + return SignSchema.KECCAK; + } + return SignSchema.SHA3; + } } diff --git a/src/core/crypto/SignSchema.ts b/src/core/crypto/SignSchema.ts index f9bc1e7d83..2249a22714 100644 --- a/src/core/crypto/SignSchema.ts +++ b/src/core/crypto/SignSchema.ts @@ -15,10 +15,10 @@ */ /** - * [KECCAK_REVERSED_KEY]: Keccak hash algorithm with reversed private keys. + * [KECCAK]: Keccak hash algorithm. * [SHA3]: SHA3 hash algorithm without key reversal */ export enum SignSchema { - KECCAK_REVERSED_KEY = 1, + KECCAK = 1, SHA3 = 2, } diff --git a/src/core/crypto/Utilities.ts b/src/core/crypto/Utilities.ts index b80d1aed95..69132811d0 100644 --- a/src/core/crypto/Utilities.ts +++ b/src/core/crypto/Utilities.ts @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { RawArray as array } from '../format'; import * as nacl from './nacl_catapult'; import { SHA3Hasher as sha3Hasher } from './SHA3Hasher'; diff --git a/src/core/format/RawAddress.ts b/src/core/format/RawAddress.ts index 3e304a7311..63c0e2a219 100644 --- a/src/core/format/RawAddress.ts +++ b/src/core/format/RawAddress.ts @@ -16,7 +16,9 @@ import { keccak256, sha3_256 } from 'js-sha3'; import RIPEMD160 = require('ripemd160'); -import { SignSchema } from '../crypto'; +import { NetworkType } from '../../model/blockchain/NetworkType'; +import { SignSchema} from '../crypto'; +import { SHA3Hasher } from '../crypto/SHA3Hasher'; import { Base32 } from './Base32'; import { Convert } from './Convert'; import { RawArray } from './RawArray'; @@ -74,14 +76,13 @@ export class RawAddress { /** * Converts a public key to a decoded address for a specific network. * @param {Uint8Array} publicKey The public key. - * @param {number} networkIdentifier The network identifier. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {NetworkType} networkType The network identifier. * @returns {Uint8Array} The decoded address corresponding to the inputs. */ public static publicKeyToAddress = (publicKey: Uint8Array, - networkIdentifier: number, - signSchema: SignSchema = SignSchema.SHA3): Uint8Array => { + networkType: NetworkType): Uint8Array => { // step 1: sha3 hash of the public key + const signSchema = SHA3Hasher.resolveSignSchema(networkType); const publicKeyHash = signSchema === SignSchema.SHA3 ? sha3_256.arrayBuffer(publicKey) : keccak256.arrayBuffer(publicKey); // step 2: ripemd160 hash of (1) @@ -89,13 +90,14 @@ export class RawAddress { // step 3: add network identifier byte in front of (2) const decodedAddress = new Uint8Array(RawAddress.constants.sizes.addressDecoded); - decodedAddress[0] = networkIdentifier; + decodedAddress[0] = networkType; RawArray.copy(decodedAddress, ripemdHash, RawAddress.constants.sizes.ripemd160, 1); // step 4: concatenate (3) and the checksum of (3) const hash = signSchema === SignSchema.SHA3 ? sha3_256.arrayBuffer(decodedAddress.subarray(0, RawAddress.constants.sizes.ripemd160 + 1)) : keccak256.arrayBuffer(decodedAddress.subarray(0, RawAddress.constants.sizes.ripemd160 + 1)); + RawArray.copy(decodedAddress, RawArray.uint8View(hash), RawAddress.constants.sizes.checksum, RawAddress.constants.sizes.ripemd160 + 1); @@ -105,10 +107,11 @@ export class RawAddress { /** * Determines the validity of a decoded address. * @param {Uint8Array} decoded The decoded address. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {NetworkType} networkType The network identifier. * @returns {boolean} true if the decoded address is valid, false otherwise. */ - public static isValidAddress = (decoded: Uint8Array, signSchema: SignSchema = SignSchema.SHA3): boolean => { + public static isValidAddress = (decoded: Uint8Array, networkType: NetworkType): boolean => { + const signSchema = SHA3Hasher.resolveSignSchema(networkType); const hash = signSchema === SignSchema.SHA3 ? sha3_256.create() : keccak256.create(); const checksumBegin = RawAddress.constants.sizes.addressDecoded - RawAddress.constants.sizes.checksum; hash.update(decoded.subarray(0, checksumBegin)); @@ -120,16 +123,17 @@ export class RawAddress { /** * Determines the validity of an encoded address string. * @param {string} encoded The encoded address string. + * @param {NetworkType} networkType The network identifier. * @returns {boolean} true if the encoded address string is valid, false otherwise. */ - public static isValidEncodedAddress = (encoded: string): boolean => { + public static isValidEncodedAddress = (encoded: string, networkType: NetworkType): boolean => { if (RawAddress.constants.sizes.addressEncoded !== encoded.length) { return false; } try { const decoded = RawAddress.stringToAddress(encoded); - return RawAddress.isValidAddress(decoded); + return RawAddress.isValidAddress(decoded, networkType); } catch (err) { return false; } diff --git a/src/core/format/Utilities.ts b/src/core/format/Utilities.ts index 332433afff..b1e04b2fba 100644 --- a/src/core/format/Utilities.ts +++ b/src/core/format/Utilities.ts @@ -152,7 +152,7 @@ export const encodeBlock = (input, inputOffset, output, outputOffset) => { }; export const Char_To_Decoded_Char_Map = () => { - const builder = this.createBuilder(); + const builder = createBuilder(); builder.addRange('A', 'Z', 0); builder.addRange('2', '7', 26); return builder.map; diff --git a/src/core/utils/TransactionMapping.ts b/src/core/utils/TransactionMapping.ts index 7c4c36a0e4..64a387c4d4 100644 --- a/src/core/utils/TransactionMapping.ts +++ b/src/core/utils/TransactionMapping.ts @@ -35,12 +35,10 @@ export class TransactionMapping { * Create transaction class from payload binary. * @param {string} payload The transaction binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} The transaction class. */ public static createFromPayload(payload: string, - isEmbedded = false, - signSchema = SignSchema.SHA3): Transaction | InnerTransaction { - return CreateTransactionFromPayload(payload, isEmbedded, signSchema); + isEmbedded = false): Transaction | InnerTransaction { + return CreateTransactionFromPayload(payload, isEmbedded); } } diff --git a/src/infrastructure/MosaicHttp.ts b/src/infrastructure/MosaicHttp.ts index 27ca2383bf..28eae72a02 100644 --- a/src/infrastructure/MosaicHttp.ts +++ b/src/infrastructure/MosaicHttp.ts @@ -18,10 +18,10 @@ import { ClientResponse } from 'http'; import {from as observableFrom, Observable, throwError} from 'rxjs'; import {catchError, map, mergeMap} from 'rxjs/operators'; import {PublicAccount} from '../model/account/PublicAccount'; +import {MosaicFlags} from '../model/mosaic/MosaicFlags'; import {MosaicId} from '../model/mosaic/MosaicId'; import {MosaicInfo} from '../model/mosaic/MosaicInfo'; import { MosaicNames } from '../model/mosaic/MosaicNames'; -import {MosaicProperties} from '../model/mosaic/MosaicProperties'; import {NamespaceId} from '../model/namespace/NamespaceId'; import { NamespaceName } from '../model/namespace/NamespaceName'; import {UInt64} from '../model/UInt64'; @@ -64,29 +64,15 @@ export class MosaicHttp extends Http implements MosaicRepository { this.mosaicRoutesApi.getMosaic(mosaicId.toHex())).pipe( map((response: { response: ClientResponse; body: MosaicInfoDTO; } ) => { const mosaicInfoDTO = response.body; - let mosaicFlag; - let divisibility; - let duration; - if (mosaicInfoDTO.mosaic.flags) { - mosaicFlag = mosaicInfoDTO.mosaic.flags; - } - if (mosaicInfoDTO.mosaic.divisibility) { - divisibility = mosaicInfoDTO.mosaic.divisibility; - } - if (mosaicInfoDTO.mosaic.duration) { - duration = mosaicInfoDTO.mosaic.duration; - } return new MosaicInfo( new MosaicId(mosaicInfoDTO.mosaic.id), UInt64.fromNumericString(mosaicInfoDTO.mosaic.supply), UInt64.fromNumericString(mosaicInfoDTO.mosaic.startHeight), PublicAccount.createFromPublicKey(mosaicInfoDTO.mosaic.ownerPublicKey, networkType), mosaicInfoDTO.mosaic.revision, - new MosaicProperties( - mosaicFlag, - divisibility, - UInt64.fromNumericString(duration), - ), + new MosaicFlags(mosaicInfoDTO.mosaic.flags), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); }), catchError((error) => throwError(this.errorHandling(error))), @@ -109,29 +95,15 @@ export class MosaicHttp extends Http implements MosaicRepository { map((response: { response: ClientResponse; body: MosaicInfoDTO[]; }) => { const mosaicInfosDTO = response.body; return mosaicInfosDTO.map((mosaicInfoDTO) => { - let mosaicFlag; - let divisibility; - let duration; - if (mosaicInfoDTO.mosaic.flags) { - mosaicFlag = mosaicInfoDTO.mosaic.flags; - } - if (mosaicInfoDTO.mosaic.divisibility) { - divisibility = mosaicInfoDTO.mosaic.divisibility; - } - if (mosaicInfoDTO.mosaic.duration) { - duration = mosaicInfoDTO.mosaic.duration; - } return new MosaicInfo( new MosaicId(mosaicInfoDTO.mosaic.id), UInt64.fromNumericString(mosaicInfoDTO.mosaic.supply), UInt64.fromNumericString(mosaicInfoDTO.mosaic.startHeight), PublicAccount.createFromPublicKey(mosaicInfoDTO.mosaic.ownerPublicKey, networkType), mosaicInfoDTO.mosaic.revision, - new MosaicProperties( - mosaicFlag, - divisibility, - UInt64.fromNumericString(duration), - ), + new MosaicFlags(mosaicInfoDTO.mosaic.flags), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); }); }), diff --git a/src/infrastructure/receipt/CreateReceiptFromDTO.ts b/src/infrastructure/receipt/CreateReceiptFromDTO.ts index 1cb61fe1b1..ef497b771b 100644 --- a/src/infrastructure/receipt/CreateReceiptFromDTO.ts +++ b/src/infrastructure/receipt/CreateReceiptFromDTO.ts @@ -73,6 +73,7 @@ export const CreateReceiptFromDTO = (receiptDTO, networkType): Receipt => { return createBalanceTransferReceipt(receiptDTO, networkType); case ReceiptType.Mosaic_Expired: case ReceiptType.Namespace_Expired: + case ReceiptType.Namespace_Deleted: return createArtifactExpiryReceipt(receiptDTO); case ReceiptType.Inflation: return createInflationReceipt(receiptDTO); diff --git a/src/infrastructure/transaction/CreateTransactionFromDTO.ts b/src/infrastructure/transaction/CreateTransactionFromDTO.ts index 2165727001..0a91ed32ab 100644 --- a/src/infrastructure/transaction/CreateTransactionFromDTO.ts +++ b/src/infrastructure/transaction/CreateTransactionFromDTO.ts @@ -20,8 +20,8 @@ import {PublicAccount} from '../../model/account/PublicAccount'; import {NetworkType} from '../../model/blockchain/NetworkType'; import {Id} from '../../model/Id'; import {Mosaic} from '../../model/mosaic/Mosaic'; +import {MosaicFlags} from '../../model/mosaic/MosaicFlags'; import {MosaicId} from '../../model/mosaic/MosaicId'; -import {MosaicProperties} from '../../model/mosaic/MosaicProperties'; import {NamespaceId} from '../../model/namespace/NamespaceId'; import {AccountAddressRestrictionTransaction} from '../../model/transaction/AccountAddressRestrictionTransaction'; import { AccountLinkTransaction } from '../../model/transaction/AccountLinkTransaction'; @@ -163,11 +163,9 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr UInt64.fromNumericString(transactionDTO.maxFee || '0'), transactionDTO.nonce, new MosaicId(transactionDTO.id), - new MosaicProperties( - transactionDTO.flags, - transactionDTO.divisibility, - UInt64.fromNumericString(transactionDTO.duration), - ), + new MosaicFlags(transactionDTO.flags), + transactionDTO.divisibility, + UInt64.fromNumericString(transactionDTO.duration), transactionDTO.signature, transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey, extractNetworkType(transactionDTO.version)) : undefined, diff --git a/src/infrastructure/transaction/CreateTransactionFromPayload.ts b/src/infrastructure/transaction/CreateTransactionFromPayload.ts index 386c17c26b..968fc2ab33 100644 --- a/src/infrastructure/transaction/CreateTransactionFromPayload.ts +++ b/src/infrastructure/transaction/CreateTransactionFromPayload.ts @@ -46,13 +46,11 @@ import { TransactionBuilder } from '../catbuffer/TransactionBuilder'; * @internal * @param payload - The transaction binary data * @param isEmbedded - Is the transaction an embedded inner transaction - * @param signSchema - The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} * @constructor */ export const CreateTransactionFromPayload = (payload: string, - isEmbedded = false, - signSchema = SignSchema.SHA3): Transaction | InnerTransaction => { + isEmbedded = false): Transaction | InnerTransaction => { const transactionBuilder = isEmbedded ? EmbeddedTransactionBuilder.loadFromBinary(convert.hexToUint8(payload)) : TransactionBuilder.loadFromBinary(convert.hexToUint8(payload)); const type = transactionBuilder.getType().valueOf(); @@ -62,48 +60,48 @@ export const CreateTransactionFromPayload = (payload: string, case TransactionType.ACCOUNT_RESTRICTION_MOSAIC: switch (type) { case TransactionType.ACCOUNT_RESTRICTION_ADDRESS: - return AccountAddressRestrictionTransaction.createFromPayload(payload, isEmbedded, signSchema); + return AccountAddressRestrictionTransaction.createFromPayload(payload, isEmbedded); case TransactionType.ACCOUNT_RESTRICTION_MOSAIC: - return AccountMosaicRestrictionTransaction.createFromPayload(payload, isEmbedded, signSchema); + return AccountMosaicRestrictionTransaction.createFromPayload(payload, isEmbedded); case TransactionType.ACCOUNT_RESTRICTION_OPERATION: - return AccountOperationRestrictionTransaction.createFromPayload(payload, isEmbedded, signSchema); + return AccountOperationRestrictionTransaction.createFromPayload(payload, isEmbedded); } throw new Error ('Account restriction transaction type not recognised.'); case TransactionType.LINK_ACCOUNT: - return AccountLinkTransaction.createFromPayload(payload, isEmbedded, signSchema); + return AccountLinkTransaction.createFromPayload(payload, isEmbedded); case TransactionType.ADDRESS_ALIAS: - return AddressAliasTransaction.createFromPayload(payload, isEmbedded, signSchema); + return AddressAliasTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MOSAIC_ALIAS: - return MosaicAliasTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MosaicAliasTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MOSAIC_DEFINITION: - return MosaicDefinitionTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MosaicDefinitionTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MOSAIC_SUPPLY_CHANGE: - return MosaicSupplyChangeTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MosaicSupplyChangeTransaction.createFromPayload(payload, isEmbedded); case TransactionType.REGISTER_NAMESPACE: - return NamespaceRegistrationTransaction.createFromPayload(payload, isEmbedded, signSchema); + return NamespaceRegistrationTransaction.createFromPayload(payload, isEmbedded); case TransactionType.TRANSFER: - return TransferTransaction.createFromPayload(payload, isEmbedded, signSchema); + return TransferTransaction.createFromPayload(payload, isEmbedded); case TransactionType.SECRET_LOCK: - return SecretLockTransaction.createFromPayload(payload, isEmbedded, signSchema); + return SecretLockTransaction.createFromPayload(payload, isEmbedded); case TransactionType.SECRET_PROOF: - return SecretProofTransaction.createFromPayload(payload, isEmbedded, signSchema); + return SecretProofTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MODIFY_MULTISIG_ACCOUNT: - return MultisigAccountModificationTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MultisigAccountModificationTransaction.createFromPayload(payload, isEmbedded); case TransactionType.LOCK: - return LockFundsTransaction.createFromPayload(payload, isEmbedded, signSchema); + return LockFundsTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MOSAIC_GLOBAL_RESTRICTION: - return MosaicGlobalRestrictionTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MosaicGlobalRestrictionTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MOSAIC_ADDRESS_RESTRICTION: - return MosaicAddressRestrictionTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MosaicAddressRestrictionTransaction.createFromPayload(payload, isEmbedded); case TransactionType.ACCOUNT_METADATA_TRANSACTION: - return AccountMetadataTransaction.createFromPayload(payload, isEmbedded, signSchema); + return AccountMetadataTransaction.createFromPayload(payload, isEmbedded); case TransactionType.MOSAIC_METADATA_TRANSACTION: - return MosaicMetadataTransaction.createFromPayload(payload, isEmbedded, signSchema); + return MosaicMetadataTransaction.createFromPayload(payload, isEmbedded); case TransactionType.NAMESPACE_METADATA_TRANSACTION: - return NamespaceMetadataTransaction.createFromPayload(payload, isEmbedded, signSchema); + return NamespaceMetadataTransaction.createFromPayload(payload, isEmbedded); case TransactionType.AGGREGATE_COMPLETE: case TransactionType.AGGREGATE_BONDED: - return AggregateTransaction.createFromPayload(payload, signSchema); + return AggregateTransaction.createFromPayload(payload); default: throw new Error ('Transaction type not implemented yet.'); } diff --git a/src/infrastructure/transaction/SerializeTransactionToJSON.ts b/src/infrastructure/transaction/SerializeTransactionToJSON.ts index 074b2dc816..3544a83fce 100644 --- a/src/infrastructure/transaction/SerializeTransactionToJSON.ts +++ b/src/infrastructure/transaction/SerializeTransactionToJSON.ts @@ -112,13 +112,12 @@ export const SerializeTransactionToJSON = (transaction: Transaction): any => { mosaicId: (transaction as MosaicAliasTransaction).mosaicId.toHex(), }; case TransactionType.MOSAIC_DEFINITION: - const properties = (transaction as MosaicDefinitionTransaction).mosaicProperties.toDTO(); return { nonce: (transaction as MosaicDefinitionTransaction).nonce, mosaicId: (transaction as MosaicDefinitionTransaction).mosaicId.toHex(), - flags: properties.flags, - divisibility: properties.divisibility, - duration: properties.duration, + flags: (transaction as MosaicDefinitionTransaction).flags.getValue(), + divisibility: (transaction as MosaicDefinitionTransaction).divisibility, + duration: (transaction as MosaicDefinitionTransaction).duration.toString(), }; case TransactionType.MOSAIC_SUPPLY_CHANGE: return { diff --git a/src/model/account/Account.ts b/src/model/account/Account.ts index 7395b232fb..ae37180d08 100644 --- a/src/model/account/Account.ts +++ b/src/model/account/Account.ts @@ -14,8 +14,9 @@ * limitations under the License. */ -import {Crypto, KeyPair, SignSchema} from '../../core/crypto'; -import {Convert as convert, RawAddress as AddressLibrary} from '../../core/format'; +import {Crypto, KeyPair} from '../../core/crypto'; +import {SHA3Hasher} from '../../core/crypto/SHA3Hasher'; +import {Convert, RawAddress} from '../../core/format'; import {NetworkType} from '../blockchain/NetworkType'; import {AggregateTransaction} from '../transaction/AggregateTransaction'; import {CosignatureSignedTransaction} from '../transaction/CosignatureSignedTransaction'; @@ -41,7 +42,6 @@ export class Account { * @internal * @param address * @param keyPair - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) */ private constructor( /** @@ -51,82 +51,76 @@ export class Account { /** * The account keyPair, public and private key. */ - private readonly keyPair: IKeyPair, - /** - * The Sign Schema (KECCAK_REVERSED_KEY / SHA3). - */ - private readonly signSchema: SignSchema = SignSchema.SHA3) { + private readonly keyPair: IKeyPair) { } /** * Create an Account from a given private key * @param privateKey - Private key from an account * @param networkType - Network type - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {Account} */ public static createFromPrivateKey(privateKey: string, - networkType: NetworkType, - signSchema: SignSchema = SignSchema.SHA3): Account { + networkType: NetworkType): Account { + const signSchema = SHA3Hasher.resolveSignSchema(networkType); const keyPair: IKeyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKey, signSchema); - const address = AddressLibrary.addressToString( - AddressLibrary.publicKeyToAddress(keyPair.publicKey, networkType, signSchema)); + const address = RawAddress.addressToString( + RawAddress.publicKeyToAddress(keyPair.publicKey, networkType)); return new Account( Address.createFromRawAddress(address), keyPair, - signSchema, ); } /** * Generate a new account * @param networkType - Network type - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) */ - public static generateNewAccount(networkType: NetworkType, signSchema: SignSchema = SignSchema.SHA3): Account { + public static generateNewAccount(networkType: NetworkType): Account { // Create random bytes const randomBytesArray = Crypto.randomBytes(32); // Hash random bytes with entropy seed // Finalize and keep only 32 bytes - const hashKey = convert.uint8ToHex(randomBytesArray); + const hashKey = Convert.uint8ToHex(randomBytesArray); // Create KeyPair from hash key + const signSchema = SHA3Hasher.resolveSignSchema(networkType); const keyPair = KeyPair.createKeyPairFromPrivateKeyString(hashKey, signSchema); - const address = Address.createFromPublicKey(convert.uint8ToHex(keyPair.publicKey), networkType, signSchema); - return new Account(address, keyPair, signSchema); + const address = Address.createFromPublicKey(Convert.uint8ToHex(keyPair.publicKey), networkType); + return new Account(address, keyPair); } /** * Create a new encrypted Message * @param message - Plain message to be encrypted * @param recipientPublicAccount - Recipient public account - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param networkType - Network type * @returns {EncryptedMessage} */ public encryptMessage(message: string, recipientPublicAccount: PublicAccount, - signSchema: SignSchema = SignSchema.SHA3): EncryptedMessage { - return EncryptedMessage.create(message, recipientPublicAccount, this.privateKey, signSchema); + networkType: NetworkType): EncryptedMessage { + return EncryptedMessage.create(message, recipientPublicAccount, this.privateKey, networkType); } /** * Decrypts an encrypted message * @param encryptedMessage - Encrypted message * @param publicAccount - The public account originally encrypted the message - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param networkType - Network type * @returns {PlainMessage} */ public decryptMessage(encryptedMessage: EncryptedMessage, publicAccount: PublicAccount, - signSchema: SignSchema = SignSchema.SHA3): PlainMessage { - return EncryptedMessage.decrypt(encryptedMessage, this.privateKey, publicAccount, signSchema); + networkType: NetworkType): PlainMessage { + return EncryptedMessage.decrypt(encryptedMessage, this.privateKey, publicAccount, networkType); } /** * Account public key. * @return {string} */ get publicKey(): string { - return convert.uint8ToHex(this.keyPair.publicKey); + return Convert.uint8ToHex(this.keyPair.publicKey); } /** @@ -134,7 +128,7 @@ export class Account { * @return {PublicAccount} */ get publicAccount(): PublicAccount { - return PublicAccount.createFromPublicKey(this.publicKey, this.address.networkType, this.signSchema); + return PublicAccount.createFromPublicKey(this.publicKey, this.address.networkType); } /** @@ -142,18 +136,25 @@ export class Account { * @return {string} */ get privateKey(): string { - return convert.uint8ToHex(this.keyPair.privateKey); + return Convert.uint8ToHex(this.keyPair.privateKey); + } + + /** + * Account network type. + * @return {NetworkType} + */ + get networkType(): NetworkType { + return this.address.networkType; } /** * Sign a transaction * @param transaction - The transaction to be signed. * @param generationHash - Network generation hash hex - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {SignedTransaction} */ - public sign(transaction: Transaction, generationHash, signSchema: SignSchema = SignSchema.SHA3): SignedTransaction { - return transaction.signWith(this, generationHash, signSchema); + public sign(transaction: Transaction, generationHash: string): SignedTransaction { + return transaction.signWith(this, generationHash); } /** @@ -161,14 +162,12 @@ export class Account { * @param transaction - The aggregate transaction to be signed. * @param cosignatories - The array of accounts that will cosign the transaction * @param generationHash - Network generation hash hex - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {SignedTransaction} */ public signTransactionWithCosignatories(transaction: AggregateTransaction, cosignatories: Account[], - generationHash: string, - signSchema: SignSchema = SignSchema.SHA3): SignedTransaction { - return transaction.signTransactionWithCosignatories(this, cosignatories, generationHash, signSchema); + generationHash: string): SignedTransaction { + return transaction.signTransactionWithCosignatories(this, cosignatories, generationHash); } /** @@ -177,36 +176,31 @@ export class Account { * @param initiatorAccount - Initiator account * @param {CosignatureSignedTransaction[]} cosignatureSignedTransactions - Array of cosigned transaction * @param generationHash - Network generation hash hex - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {SignedTransaction} */ public signTransactionGivenSignatures(transaction: AggregateTransaction, cosignatureSignedTransactions: CosignatureSignedTransaction[], - generationHash: string, - signSchema: SignSchema = SignSchema.SHA3): SignedTransaction { - return transaction.signTransactionGivenSignatures(this, cosignatureSignedTransactions, generationHash, signSchema); + generationHash: string): SignedTransaction { + return transaction.signTransactionGivenSignatures(this, cosignatureSignedTransactions, generationHash); } /** * Sign aggregate signature transaction * @param cosignatureTransaction - The aggregate signature transaction. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {CosignatureSignedTransaction} */ - public signCosignatureTransaction(cosignatureTransaction: CosignatureTransaction, - signSchema: SignSchema = SignSchema.SHA3): CosignatureSignedTransaction { - return cosignatureTransaction.signWith(this, signSchema); + public signCosignatureTransaction(cosignatureTransaction: CosignatureTransaction): CosignatureSignedTransaction { + return cosignatureTransaction.signWith(this); } /** * Sign raw data * @param data - Data to be signed - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {string} - Signed data result */ - public signData(data: string, signSchema: SignSchema = SignSchema.SHA3): string { - return convert.uint8ToHex(KeyPair.sign(this.keyPair, - convert.hexToUint8(convert.utf8ToHex(data)), - signSchema, + public signData(data: string): string { + return Convert.uint8ToHex(KeyPair.sign(this.keyPair, + Convert.hexToUint8(Convert.utf8ToHex(data)), + SHA3Hasher.resolveSignSchema(this.networkType), )); } } diff --git a/src/model/account/Address.ts b/src/model/account/Address.ts index f96c972452..f2bd51f453 100644 --- a/src/model/account/Address.ts +++ b/src/model/account/Address.ts @@ -13,9 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import { SignSchema } from '../../core/crypto'; -import { Convert as convert, RawAddress as AddressLibrary} from '../../core/format'; +import { Convert, RawAddress } from '../../core/format'; import {NetworkType} from '../blockchain/NetworkType'; /** @@ -26,15 +24,13 @@ export class Address { * Create from private key * @param publicKey - The account public key. * @param networkType - The NEM network type. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Address} */ public static createFromPublicKey(publicKey: string, - networkType: NetworkType, - signSchema = SignSchema.SHA3): Address { - const publicKeyUint8 = convert.hexToUint8(publicKey); - const address = AddressLibrary - .addressToString(AddressLibrary.publicKeyToAddress(publicKeyUint8, networkType, signSchema)); + networkType: NetworkType): Address { + const publicKeyUint8 = Convert.hexToUint8(publicKey); + const address = RawAddress + .addressToString(RawAddress.publicKeyToAddress(publicKeyUint8, networkType)); return new Address(address, networkType); } @@ -44,7 +40,7 @@ export class Address { * ex: SB3KUBHATFCPV7UZQLWAQ2EUR6SIHBSBEOEDDDF3 or SB3KUB-HATFCP-V7UZQL-WAQ2EU-R6SIHB-SBEOED-DDF3 * @returns {Address} */ - public static createFromRawAddress(rawAddress: string) { + public static createFromRawAddress(rawAddress: string): Address { let networkType: NetworkType; const addressTrimAndUpperCase: string = rawAddress .trim() @@ -74,8 +70,8 @@ export class Address { * @return {Address} */ public static createFromEncoded(encoded: string): Address { - return Address.createFromRawAddress(AddressLibrary - .addressToString(convert.hexToUint8(encoded))); + return Address.createFromRawAddress(RawAddress + .addressToString(Convert.hexToUint8(encoded))); } /** diff --git a/src/model/account/PublicAccount.ts b/src/model/account/PublicAccount.ts index 7f3eb57bc0..fa8399696c 100644 --- a/src/model/account/PublicAccount.ts +++ b/src/model/account/PublicAccount.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { KeyPair, SignSchema } from '../../core/crypto'; +import { KeyPair, SHA3Hasher } from '../../core/crypto'; import { Convert as convert} from '../../core/format'; import { NetworkType } from '../blockchain/NetworkType'; import { Address } from './Address'; @@ -47,14 +47,13 @@ export class PublicAccount { * Create a PublicAccount from a public key and network type. * @param publicKey Public key * @param networkType Network type - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {PublicAccount} */ - static createFromPublicKey(publicKey: string, networkType: NetworkType, signSchema = SignSchema.SHA3): PublicAccount { + static createFromPublicKey(publicKey: string, networkType: NetworkType): PublicAccount { if (publicKey == null || (publicKey.length !== 64 && publicKey.length !== 66)) { throw new Error('Not a valid public key'); } - const address = Address.createFromPublicKey(publicKey, networkType, signSchema); + const address = Address.createFromPublicKey(publicKey, networkType); return new PublicAccount(publicKey, address); } @@ -63,10 +62,9 @@ export class PublicAccount { * * @param {string} data - The data to verify. * @param {string} signature - The signature to verify. - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {boolean} - True if the signature is valid, false otherwise. */ - public verifySignature(data: string, signature: string, signSchema: SignSchema = SignSchema.SHA3): boolean { + public verifySignature(data: string, signature: string): boolean { if (!signature) { throw new Error('Missing argument'); } @@ -83,7 +81,7 @@ export class PublicAccount { // Convert to Uint8Array const convertedData = convert.hexToUint8(convert.utf8ToHex(data)); - + const signSchema = SHA3Hasher.resolveSignSchema(this.address.networkType); return KeyPair.verify(convert.hexToUint8(this.publicKey), convertedData, convertedSignature, signSchema); } @@ -92,7 +90,7 @@ export class PublicAccount { * @param publicAccount * @returns {boolean} */ - equals(publicAccount: PublicAccount) { + equals(publicAccount: PublicAccount): boolean { return this.publicKey === publicAccount.publicKey && this.address.plain() === publicAccount.address.plain(); } diff --git a/src/model/blockchain/NetworkSetting.ts b/src/model/blockchain/NetworkSetting.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/model/model.ts b/src/model/model.ts index 696f98ba8a..a218ab2bb0 100644 --- a/src/model/model.ts +++ b/src/model/model.ts @@ -50,7 +50,7 @@ export * from './mosaic/MosaicInfo'; export * from './mosaic/MosaicId'; export * from './mosaic/MosaicNonce'; export * from './mosaic/MosaicSupplyChangeAction'; -export * from './mosaic/MosaicProperties'; +export * from './mosaic/MosaicFlags'; export * from '../service/MosaicView'; export * from '../service/MosaicAmountView'; export * from './mosaic/NetworkCurrencyMosaic'; diff --git a/src/model/mosaic/MosaicFlag.ts b/src/model/mosaic/MosaicFlag.ts deleted file mode 100644 index dcf6a8b500..0000000000 --- a/src/model/mosaic/MosaicFlag.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2018 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. - */ -export enum MosaicFlags { - /** No flags present. */ - NONE = 0, - /** Mosaic supports supply changes even when mosaic owner owns partial supply. */ - SUPPLY_MUTABLE = 1, - /** Mosaic supports transfers between arbitrary accounts \note when not set, mosaic can only be transferred to and from mosaic owner. */ - TRANSFERABLE = 2, - /** Mosaic supports custom restrictions configured by mosaic owner. */ - RESTRICTABLE = 4, -} diff --git a/src/model/mosaic/MosaicProperties.ts b/src/model/mosaic/MosaicFlags.ts similarity index 58% rename from src/model/mosaic/MosaicProperties.ts rename to src/model/mosaic/MosaicFlags.ts index 17fa8c6b62..21ff8017ac 100644 --- a/src/model/mosaic/MosaicProperties.ts +++ b/src/model/mosaic/MosaicFlags.ts @@ -14,12 +14,10 @@ * limitations under the License. */ -import {UInt64} from '../UInt64'; - /** - * Mosaic properties model + * Mosaic flags model */ -export class MosaicProperties { +export class MosaicFlags { /** * The creator can choose between a definition that allows a mosaic supply change at a later point or an immutable supply. @@ -47,22 +45,7 @@ export class MosaicProperties { * @param divisibility * @param duration */ - constructor(flags: number, - /** - * The divisibility determines up to what decimal place the mosaic can be divided into. - * Thus a divisibility of 3 means that a mosaic can be divided into smallest parts of 0.001 mosaics - * i.e. milli mosaics is the smallest sub-unit. - * When transferring mosaics via a transfer transaction the quantity transferred - * is given in multiples of those smallest parts. - * The divisibility must be in the range of 0 and 6. The default value is "0". - */ - public readonly divisibility: number, - /** - * The duration in blocks a mosaic will be available. - * After the duration finishes mosaic is inactive and can be renewed. - * Duration is optional when defining the mosaic - */ - public readonly duration: UInt64) { + constructor(flags: number) { let binaryFlags = '00' + (flags >>> 0).toString(2); binaryFlags = binaryFlags.substr(binaryFlags.length - 3, 3); this.supplyMutable = binaryFlags[2] === '1'; @@ -72,19 +55,25 @@ export class MosaicProperties { /** * Static constructor function with default parameters - * @returns {MosaicProperties} - * @param params + * @returns {MosaicFlags} + * @param supplyMutable + * @param transferable + * @param restrictable */ - public static create(params: { + public static create( supplyMutable: boolean, transferable: boolean, - divisibility: number, - duration: UInt64, - restrictable?: boolean, - }) { - const restrictable = params.restrictable ? 4 : 0; - const flags = (params.supplyMutable ? 1 : 0) + (params.transferable ? 2 : 0) + restrictable; - return new MosaicProperties(flags, params.divisibility, params.duration); + restrictable: boolean = false ): MosaicFlags { + const flags = (supplyMutable ? 1 : 0) + (transferable ? 2 : 0) + (restrictable ? 4 : 0); + return new MosaicFlags(flags); + } + + /** + * Get mosaic flag value in number + * @returns {number} + */ + public getValue(): number { + return (this.supplyMutable ? 1 : 0) + (this.transferable ? 2 : 0) + (this.restrictable ? 4 : 0); } /** @@ -92,9 +81,7 @@ export class MosaicProperties { */ toDTO() { return { - flags: (this.supplyMutable ? 1 : 0) + (this.transferable ? 2 : 0) + (this.transferable ? 4 : 0), - divisibility: this.divisibility, - duration: this.duration ? this.duration.toString() : '0', + flags: this.getValue(), }; } } diff --git a/src/model/mosaic/MosaicInfo.ts b/src/model/mosaic/MosaicInfo.ts index 1077924abe..b145241268 100644 --- a/src/model/mosaic/MosaicInfo.ts +++ b/src/model/mosaic/MosaicInfo.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -import {PublicAccount} from '../account/PublicAccount'; -import {UInt64} from '../UInt64'; -import {MosaicId} from './MosaicId'; -import {MosaicProperties} from './MosaicProperties'; +import { PublicAccount } from '../account/PublicAccount'; +import { UInt64 } from '../UInt64'; +import { MosaicFlags } from './MosaicFlags'; +import { MosaicId } from './MosaicId'; /** * The mosaic info structure describes a mosaic. @@ -55,34 +55,26 @@ export class MosaicInfo { */ public readonly revision: number, /** - * The mosaic properties. + * The mosaic flags. */ - private readonly properties: MosaicProperties, + public readonly flags: MosaicFlags, + /** + * Mosaic divisibility + */ + public readonly divisibility: number, + /** + * Mosaic duration + */ + public readonly duration: UInt64, ) { } - /** - * Mosaic divisibility - * @returns {number} - */ - public get divisibility(): number { - return this.properties.divisibility; - } - - /** - * Mosaic duration - * @returns {UInt64} - */ - public get duration(): UInt64 { - return this.properties.duration; - } - /** * Is mosaic supply mutable * @returns {boolean} */ public isSupplyMutable(): boolean { - return this.properties.supplyMutable; + return this.flags.supplyMutable; } /** @@ -90,7 +82,7 @@ export class MosaicInfo { * @returns {boolean} */ public isTransferable(): boolean { - return this.properties.transferable; + return this.flags.transferable; } /** @@ -98,6 +90,6 @@ export class MosaicInfo { * @returns {boolean} */ public isRestrictable(): boolean { - return this.properties.restrictable; + return this.flags.restrictable; } } diff --git a/src/model/receipt/ReceiptType.ts b/src/model/receipt/ReceiptType.ts index ff8c412a39..74a1314360 100644 --- a/src/model/receipt/ReceiptType.ts +++ b/src/model/receipt/ReceiptType.ts @@ -50,16 +50,20 @@ export enum ReceiptType { * The sender and recipient of the mosaicId and amount representing the cost of registering the mosaic. * It is recorded when a mosaic is registered. */ - Mosaic_Rental_Fee = 0x134D, + Mosaic_Rental_Fee = 0x124D, /** - * The namespaceId expiring in this block. It is recorded when a namespace expires. + * The identifier of the namespace expiring in this block. It is recorded when the namespace lifetime elapses. */ Namespace_Expired = 0x414E, /** * The sender and recipient of the mosaicId and amount representing the cost of extending the namespace. * It is recorded when a namespace is registered or its duration is extended. */ - Namespace_Rental_Fee = 0x124E, + Namespace_Rental_Fee = 0x134E, + /** + * The identifier of the namespace deleted in this block. It is recorded when the namespace grace period elapses. + */ + Namespace_Deleted = 0x424E, /** * The lockhash sender, mosaicId and amount locked. It is recorded when a valid HashLockTransaction is announced. */ diff --git a/src/model/transaction/AccountAddressRestrictionTransaction.ts b/src/model/transaction/AccountAddressRestrictionTransaction.ts index 3ff874c446..c6c1670582 100644 --- a/src/model/transaction/AccountAddressRestrictionTransaction.ts +++ b/src/model/transaction/AccountAddressRestrictionTransaction.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, RawAddress } from '../../core/format'; import { AccountAddressRestrictionModificationBuilder } from '../../infrastructure/catbuffer/AccountAddressRestrictionModificationBuilder'; import { AccountAddressRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/AccountAddressRestrictionTransactionBuilder'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; -import { EmbeddedAccountAddressRestrictionTransactionBuilder }from '../../infrastructure/catbuffer/EmbeddedAccountAddressRestrictionTransactionBuilder'; +import { + EmbeddedAccountAddressRestrictionTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedAccountAddressRestrictionTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; @@ -89,12 +90,10 @@ export class AccountAddressRestrictionTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedAccountAddressRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : AccountAddressRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -113,7 +112,7 @@ export class AccountAddressRestrictionTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountAddressRestrictionTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/AccountLinkTransaction.ts b/src/model/transaction/AccountLinkTransaction.ts index e15e56929e..a5490b1f8c 100644 --- a/src/model/transaction/AccountLinkTransaction.ts +++ b/src/model/transaction/AccountLinkTransaction.ts @@ -14,12 +14,10 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AccountLinkTransactionBuilder } from '../../infrastructure/catbuffer/AccountLinkTransactionBuilder'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { EmbeddedAccountLinkTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedAccountLinkTransactionBuilder'; -import { EntityTypeDto } from '../../infrastructure/catbuffer/EntityTypeDto'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; @@ -93,12 +91,10 @@ export class AccountLinkTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedAccountLinkTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : AccountLinkTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -111,7 +107,7 @@ export class AccountLinkTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountLinkTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/AccountMetadataTransaction.ts b/src/model/transaction/AccountMetadataTransaction.ts index 90b7c0c349..3355b4e332 100644 --- a/src/model/transaction/AccountMetadataTransaction.ts +++ b/src/model/transaction/AccountMetadataTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AccountMetadataTransactionBuilder } from '../../infrastructure/catbuffer/AccountMetadataTransactionBuilder'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; @@ -111,12 +110,10 @@ export class AccountMetadataTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedAccountMetadataTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : AccountMetadataTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -131,7 +128,7 @@ export class AccountMetadataTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountMetadataTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/AccountMosaicRestrictionTransaction.ts b/src/model/transaction/AccountMosaicRestrictionTransaction.ts index af24e4164d..cbf4d4b416 100644 --- a/src/model/transaction/AccountMosaicRestrictionTransaction.ts +++ b/src/model/transaction/AccountMosaicRestrictionTransaction.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AccountMosaicRestrictionModificationBuilder } from '../../infrastructure/catbuffer/AccountMosaicRestrictionModificationBuilder'; import { AccountMosaicRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/AccountMosaicRestrictionTransactionBuilder'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; -import { EmbeddedAccountMosaicRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedAccountMosaicRestrictionTransactionBuilder'; +import { + EmbeddedAccountMosaicRestrictionTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedAccountMosaicRestrictionTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; @@ -89,12 +90,10 @@ export class AccountMosaicRestrictionTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedAccountMosaicRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : AccountMosaicRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -113,7 +112,7 @@ export class AccountMosaicRestrictionTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountMosaicRestrictionTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/AccountOperationRestrictionTransaction.ts b/src/model/transaction/AccountOperationRestrictionTransaction.ts index bab5131586..9ee8b3498a 100644 --- a/src/model/transaction/AccountOperationRestrictionTransaction.ts +++ b/src/model/transaction/AccountOperationRestrictionTransaction.ts @@ -14,12 +14,17 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; -import { AccountOperationRestrictionModificationBuilder } from '../../infrastructure/catbuffer/AccountOperationRestrictionModificationBuilder'; -import { AccountOperationRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/AccountOperationRestrictionTransactionBuilder'; +import { + AccountOperationRestrictionModificationBuilder, +} from '../../infrastructure/catbuffer/AccountOperationRestrictionModificationBuilder'; +import { + AccountOperationRestrictionTransactionBuilder, +} from '../../infrastructure/catbuffer/AccountOperationRestrictionTransactionBuilder'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; -import { EmbeddedAccountOperationRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedAccountOperationRestrictionTransactionBuilder'; +import { + EmbeddedAccountOperationRestrictionTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedAccountOperationRestrictionTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; @@ -87,12 +92,10 @@ export class AccountOperationRestrictionTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedAccountOperationRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : AccountOperationRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signer = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -110,7 +113,7 @@ export class AccountOperationRestrictionTransaction extends Transaction { networkType, isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountOperationRestrictionTransactionBuilder).fee.amount), ); - return isEmbedded ? transaction.toAggregate(PublicAccount.createFromPublicKey(signer, networkType, signSchema)) : transaction; + return isEmbedded ? transaction.toAggregate(PublicAccount.createFromPublicKey(signer, networkType)) : transaction; } /** diff --git a/src/model/transaction/AddressAliasTransaction.ts b/src/model/transaction/AddressAliasTransaction.ts index f5e530baed..3f75b23661 100644 --- a/src/model/transaction/AddressAliasTransaction.ts +++ b/src/model/transaction/AddressAliasTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, RawAddress } from '../../core/format'; import { AddressAliasTransactionBuilder } from '../../infrastructure/catbuffer/AddressAliasTransactionBuilder'; import { AddressDto } from '../../infrastructure/catbuffer/AddressDto'; @@ -107,12 +106,10 @@ export class AddressAliasTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedAddressAliasTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : AddressAliasTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -127,7 +124,7 @@ export class AddressAliasTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AddressAliasTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/AggregateTransaction.ts b/src/model/transaction/AggregateTransaction.ts index 83b02def86..d400f1cf1e 100644 --- a/src/model/transaction/AggregateTransaction.ts +++ b/src/model/transaction/AggregateTransaction.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { KeyPair, SignSchema } from '../../core/crypto'; +import { KeyPair, SHA3Hasher } from '../../core/crypto'; import { Convert } from '../../core/format'; import { AggregateBondedTransactionBuilder } from '../../infrastructure/catbuffer/AggregateBondedTransactionBuilder'; import { AggregateCompleteTransactionBuilder } from '../../infrastructure/catbuffer/AggregateCompleteTransactionBuilder'; @@ -31,7 +31,6 @@ import { NetworkType } from '../blockchain/NetworkType'; import { UInt64 } from '../UInt64'; import { AggregateTransactionCosignature } from './AggregateTransactionCosignature'; import { CosignatureSignedTransaction } from './CosignatureSignedTransaction'; -import { CosignatureTransaction } from './CosignatureTransaction'; import { Deadline } from './Deadline'; import { InnerTransaction } from './InnerTransaction'; import { SignedTransaction } from './SignedTransaction'; @@ -127,10 +126,9 @@ export class AggregateTransaction extends Transaction { /** * Create a transaction object from payload * @param {string} payload Binary payload - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {AggregateTransaction} */ - public static createFromPayload(payload: string, signSchema: SignSchema = SignSchema.SHA3): AggregateTransaction { + public static createFromPayload(payload: string): AggregateTransaction { /** * Get transaction type from the payload hex * As buffer uses separate builder class for Complete and bonded @@ -162,14 +160,14 @@ export class AggregateTransaction extends Transaction { const consignatures = consignatureArray ? consignatureArray.map((cosignature) => new AggregateTransactionCosignature( cosignature.substring(64, 192), - PublicAccount.createFromPublicKey(cosignature.substring(0, 64), networkType, signSchema), + PublicAccount.createFromPublicKey(cosignature.substring(0, 64), networkType), )) : []; return type === TransactionType.AGGREGATE_COMPLETE ? AggregateTransaction.createComplete( Deadline.createFromDTO(builder.deadline.timestamp), embeddedTransactionArray.map((transactionRaw) => { - return CreateTransactionFromPayload(transactionRaw, true, signSchema) as InnerTransaction; + return CreateTransactionFromPayload(transactionRaw, true) as InnerTransaction; }), networkType, consignatures, @@ -177,7 +175,7 @@ export class AggregateTransaction extends Transaction { ) : AggregateTransaction.createBonded( Deadline.createFromDTO(builder.deadline.timestamp), embeddedTransactionArray.map((transactionRaw) => { - return CreateTransactionFromPayload(transactionRaw, true, signSchema) as InnerTransaction; + return CreateTransactionFromPayload(transactionRaw, true) as InnerTransaction; }), networkType, consignatures, @@ -202,17 +200,16 @@ export class AggregateTransaction extends Transaction { * @param initiatorAccount - Initiator account * @param cosignatories - The array of accounts that will cosign the transaction * @param generationHash - Network generation hash hex - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {SignedTransaction} */ public signTransactionWithCosignatories(initiatorAccount: Account, cosignatories: Account[], - generationHash: string, - signSchema: SignSchema = SignSchema.SHA3): SignedTransaction { - const signedTransaction = this.signWith(initiatorAccount, generationHash, signSchema); + generationHash: string): SignedTransaction { + const signedTransaction = this.signWith(initiatorAccount, generationHash); const transactionHashBytes = Convert.hexToUint8(signedTransaction.hash); let signedPayload = signedTransaction.payload; cosignatories.forEach((cosigner) => { + const signSchema = SHA3Hasher.resolveSignSchema(cosigner.networkType); const signature = KeyPair.sign(cosigner, transactionHashBytes, signSchema); signedPayload += cosigner.publicKey + Convert.uint8ToHex(signature); }); @@ -235,14 +232,12 @@ export class AggregateTransaction extends Transaction { * @param initiatorAccount - Initiator account * @param {CosignatureSignedTransaction[]} cosignatureSignedTransactions - Array of cosigned transaction * @param generationHash - Network generation hash hex - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @return {SignedTransaction} */ public signTransactionGivenSignatures(initiatorAccount: Account, cosignatureSignedTransactions: CosignatureSignedTransaction[], - generationHash: string, - signSchema: SignSchema= SignSchema.SHA3): SignedTransaction { - const signedTransaction = this.signWith(initiatorAccount, generationHash, signSchema); + generationHash: string): SignedTransaction { + const signedTransaction = this.signWith(initiatorAccount, generationHash); let signedPayload = signedTransaction.payload; cosignatureSignedTransactions.forEach((cosignedTransaction) => { signedPayload += cosignedTransaction.signerPublicKey + cosignedTransaction.signature; diff --git a/src/model/transaction/CosignatureTransaction.ts b/src/model/transaction/CosignatureTransaction.ts index eeec13d66a..224119ad06 100644 --- a/src/model/transaction/CosignatureTransaction.ts +++ b/src/model/transaction/CosignatureTransaction.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { KeyPair, SignSchema } from '../../core/crypto'; +import { KeyPair, SHA3Hasher } from '../../core/crypto'; import { Convert } from '../../core/format/Convert'; import {Account} from '../account/Account'; import {AggregateTransaction} from './AggregateTransaction'; @@ -50,18 +50,18 @@ export class CosignatureTransaction { * @param account - The signing account * @param payload - off transaction payload (aggregated transaction is unannounced) * @param generationHash - Network generation hash - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {CosignatureSignedTransaction} */ public static signTransactionPayload(account: Account, payload: string, - generationHash: string, - signSchema: SignSchema = SignSchema.SHA3): CosignatureSignedTransaction { + generationHash: string): CosignatureSignedTransaction { /** * For aggregated complete transaction, cosignatories are gathered off chain announced. */ - const transactionHash = Transaction.createTransactionHash(payload, Array.from(Convert.hexToUint8(generationHash))); + const transactionHash = + Transaction.createTransactionHash(payload, Array.from(Convert.hexToUint8(generationHash)), account.networkType); const hashBytes = Convert.hexToUint8(transactionHash); + const signSchema = SHA3Hasher.resolveSignSchema(account.networkType); const signature = KeyPair.sign(account, new Uint8Array(hashBytes), signSchema); return new CosignatureSignedTransaction( Convert.uint8ToHex(hashBytes), @@ -74,15 +74,15 @@ export class CosignatureTransaction { * @internal * Serialize and sign transaction creating a new SignedTransaction * @param account - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {CosignatureSignedTransaction} */ - public signWith(account: Account, signSchema: SignSchema = SignSchema.SHA3): CosignatureSignedTransaction { + public signWith(account: Account): CosignatureSignedTransaction { if (!this.transactionToCosign.transactionInfo!.hash) { throw new Error('Transaction to cosign should be announced first'); } const hash = this.transactionToCosign.transactionInfo!.hash; const hashBytes = Convert.hexToUint8(hash ? hash : ''); + const signSchema = SHA3Hasher.resolveSignSchema(account.networkType); const signature = KeyPair.sign(account, new Uint8Array(hashBytes), signSchema); return new CosignatureSignedTransaction( hash ? hash : '', diff --git a/src/model/transaction/EncryptedMessage.ts b/src/model/transaction/EncryptedMessage.ts index bdf0846953..16ca7c4bad 100644 --- a/src/model/transaction/EncryptedMessage.ts +++ b/src/model/transaction/EncryptedMessage.ts @@ -14,8 +14,9 @@ * limitations under the License. */ -import {Crypto, SignSchema} from '../../core/crypto'; +import {Crypto, SHA3Hasher} from '../../core/crypto'; import {PublicAccount} from '../account/PublicAccount'; +import { NetworkType } from '../blockchain/NetworkType'; import {Message} from './Message'; import {MessageType} from './MessageType'; import {PlainMessage} from './PlainMessage'; @@ -38,12 +39,16 @@ export class EncryptedMessage extends Message { * @param message - Plain message to be encrypted * @param recipientPublicAccount - Recipient public account * @param privateKey - Sender private key - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {NetworkType} networkType - Catapult network type * @return {EncryptedMessage} */ - public static create(message: string, recipientPublicAccount: PublicAccount, privateKey, signSchema: SignSchema = SignSchema.SHA3) { + public static create(message: string, recipientPublicAccount: PublicAccount, privateKey, networkType: NetworkType) { + const signSchema = SHA3Hasher.resolveSignSchema(networkType); return new EncryptedMessage( - Crypto.encode(privateKey, recipientPublicAccount.publicKey, message, signSchema).toUpperCase(), + Crypto.encode(privateKey, + recipientPublicAccount.publicKey, + message, + signSchema).toUpperCase(), recipientPublicAccount); } @@ -60,14 +65,18 @@ export class EncryptedMessage extends Message { * @param encryptMessage - Encrypted message to be decrypted * @param privateKey - Recipient private key * @param recipientPublicAccount - Sender public account - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) + * @param {NetworkType} networkType - Catapult network type * @return {PlainMessage} */ public static decrypt(encryptMessage: EncryptedMessage, privateKey, recipientPublicAccount: PublicAccount, - signSchema: SignSchema = SignSchema.SHA3): PlainMessage { + networkType: NetworkType): PlainMessage { + const signSchema = SHA3Hasher.resolveSignSchema(networkType); return new PlainMessage(this.decodeHex( - Crypto.decode(privateKey, recipientPublicAccount.publicKey, encryptMessage.payload, signSchema))); + Crypto.decode(privateKey, + recipientPublicAccount.publicKey, + encryptMessage.payload, + signSchema))); } } diff --git a/src/model/transaction/LockFundsTransaction.ts b/src/model/transaction/LockFundsTransaction.ts index 38f1615fbf..410c649a5e 100644 --- a/src/model/transaction/LockFundsTransaction.ts +++ b/src/model/transaction/LockFundsTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { BlockDurationDto } from '../../infrastructure/catbuffer/BlockDurationDto'; @@ -118,12 +117,10 @@ export class LockFundsTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedHashLockTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : HashLockTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -141,7 +138,7 @@ export class LockFundsTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as HashLockTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/MosaicAddressRestrictionTransaction.ts b/src/model/transaction/MosaicAddressRestrictionTransaction.ts index d1c5f82b33..61d1560ddf 100644 --- a/src/model/transaction/MosaicAddressRestrictionTransaction.ts +++ b/src/model/transaction/MosaicAddressRestrictionTransaction.ts @@ -14,10 +14,11 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, RawAddress } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; -import { EmbeddedMosaicAddressRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedMosaicAddressRestrictionTransactionBuilder'; +import { + EmbeddedMosaicAddressRestrictionTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedMosaicAddressRestrictionTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { MosaicAddressRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/MosaicAddressRestrictionTransactionBuilder'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; @@ -125,12 +126,10 @@ export class MosaicAddressRestrictionTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMosaicAddressRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MosaicAddressRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -147,7 +146,7 @@ export class MosaicAddressRestrictionTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicAddressRestrictionTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/MosaicAliasTransaction.ts b/src/model/transaction/MosaicAliasTransaction.ts index b31f47564e..382a926653 100644 --- a/src/model/transaction/MosaicAliasTransaction.ts +++ b/src/model/transaction/MosaicAliasTransaction.ts @@ -14,11 +14,9 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { EmbeddedMosaicAliasTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedMosaicAliasTransactionBuilder'; -import { EntityTypeDto } from '../../infrastructure/catbuffer/EntityTypeDto'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { MosaicAliasTransactionBuilder } from '../../infrastructure/catbuffer/MosaicAliasTransactionBuilder'; import { MosaicIdDto } from '../../infrastructure/catbuffer/MosaicIdDto'; @@ -104,12 +102,10 @@ export class MosaicAliasTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMosaicAliasTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MosaicAliasTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -123,7 +119,7 @@ export class MosaicAliasTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicAliasTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/MosaicDefinitionTransaction.ts b/src/model/transaction/MosaicDefinitionTransaction.ts index 8447738813..024037a051 100644 --- a/src/model/transaction/MosaicDefinitionTransaction.ts +++ b/src/model/transaction/MosaicDefinitionTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { BlockDurationDto } from '../../infrastructure/catbuffer/BlockDurationDto'; @@ -28,10 +27,9 @@ import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; import { PublicAccount } from '../account/PublicAccount'; import { NetworkType } from '../blockchain/NetworkType'; -import { MosaicFlags } from '../mosaic/MosaicFlag'; +import { MosaicFlags } from '../mosaic/MosaicFlags'; import { MosaicId } from '../mosaic/MosaicId'; import { MosaicNonce } from '../mosaic/MosaicNonce'; -import { MosaicProperties } from '../mosaic/MosaicProperties'; import { UInt64 } from '../UInt64'; import { Deadline } from './Deadline'; import { InnerTransaction } from './InnerTransaction'; @@ -51,7 +49,9 @@ export class MosaicDefinitionTransaction extends Transaction { * @param deadline - The deadline to include the transaction. * @param nonce - The mosaic nonce ex: MosaicNonce.createRandom(). * @param mosaicId - The mosaic id ex: new MosaicId([481110499, 231112638]). - * @param mosaicProperties - The mosaic properties. + * @param flags - The mosaic flags. + * @param divisibility - The mosaic divicibility. + * @param duration - The mosaic duration. * @param networkType - The network type. * @param maxFee - (Optional) Max fee defined by the sender * @returns {MosaicDefinitionTransaction} @@ -59,7 +59,9 @@ export class MosaicDefinitionTransaction extends Transaction { public static create(deadline: Deadline, nonce: MosaicNonce, mosaicId: MosaicId, - mosaicProperties: MosaicProperties, + flags: MosaicFlags, + divisibility: number, + duration: UInt64, networkType: NetworkType, maxFee: UInt64 = new UInt64([0, 0])): MosaicDefinitionTransaction { return new MosaicDefinitionTransaction(networkType, @@ -68,7 +70,9 @@ export class MosaicDefinitionTransaction extends Transaction { maxFee, nonce, mosaicId, - mosaicProperties, + flags, + divisibility, + duration, ); } @@ -99,7 +103,15 @@ export class MosaicDefinitionTransaction extends Transaction { /** * The mosaic properties. */ - public readonly mosaicProperties: MosaicProperties, + public readonly flags: MosaicFlags, + /** + * Mosaic divisibility + */ + public readonly divisibility: number, + /** + * Mosaic duration, 0 value for eternal mosaic + */ + public readonly duration: UInt64 = UInt64.fromUint(0), signature?: string, signer?: PublicAccount, transactionInfo?: TransactionInfo) { @@ -110,12 +122,10 @@ export class MosaicDefinitionTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMosaicDefinitionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MosaicDefinitionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -125,18 +135,17 @@ export class MosaicDefinitionTransaction extends Transaction { (builder as MosaicDefinitionTransactionBuilder).getDeadline().timestamp), new MosaicNonce(builder.getNonce().serialize()), new MosaicId(builder.getId().mosaicId), - MosaicProperties.create({ - supplyMutable: (builder.getFlags() & 1) === 1, - transferable: (builder.getFlags() & 2) === 2, - divisibility: builder.getDivisibility(), - restrictable: (builder.getFlags() & 4) === 4, - duration: new UInt64(builder.getDuration().blockDuration), - }), + MosaicFlags.create( + (builder.getFlags() & 1) === 1, + (builder.getFlags() & 2) === 2, + (builder.getFlags() & 4) === 4), + builder.getDivisibility(), + new UInt64(builder.getDuration().blockDuration), networkType, isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicDefinitionTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** @@ -160,28 +169,6 @@ export class MosaicDefinitionTransaction extends Transaction { return byteSize + byteNonce + byteMosaicId + byteNumProps + byteFlags + byteDivisibility + byteDurationSize + byteDuration; } - /** - * @description get the calculated mosaic flag value - * @returns {number} - * @memberof MosaicDefinitionTransaction - */ - public getMosaicFlagValue(): number { - let flag = MosaicFlags.NONE; - if (this.mosaicProperties.supplyMutable === true) { - flag += MosaicFlags.SUPPLY_MUTABLE; - } - - if (this.mosaicProperties.transferable === true) { - flag += MosaicFlags.TRANSFERABLE; - } - - if (this.mosaicProperties.restrictable === true) { - flag += MosaicFlags.RESTRICTABLE; - } - - return flag.valueOf(); - } - /** * @description Get mosaic nonce int value * @returns {number} @@ -208,10 +195,9 @@ export class MosaicDefinitionTransaction extends Transaction { new TimestampDto(this.deadline.toDTO()), new MosaicNonceDto(this.getMosaicNonceIntValue()), new MosaicIdDto(this.mosaicId.id.toDTO()), - this.getMosaicFlagValue(), - this.mosaicProperties.divisibility, - new BlockDurationDto(this.mosaicProperties.duration ? - this.mosaicProperties.duration.toDTO() : UInt64.fromUint(0).toDTO()), + this.flags.getValue(), + this.divisibility, + new BlockDurationDto(this.duration.toDTO()), ); return transactionBuilder.serialize(); } @@ -227,10 +213,9 @@ export class MosaicDefinitionTransaction extends Transaction { TransactionType.MOSAIC_DEFINITION.valueOf(), new MosaicNonceDto(this.getMosaicNonceIntValue()), new MosaicIdDto(this.mosaicId.id.toDTO()), - this.getMosaicFlagValue(), - this.mosaicProperties.divisibility, - new BlockDurationDto(this.mosaicProperties.duration ? - this.mosaicProperties.duration.toDTO() : UInt64.fromUint(0).toDTO()), + this.flags.getValue(), + this.divisibility, + new BlockDurationDto(this.duration.toDTO()), ); return transactionBuilder.serialize(); } diff --git a/src/model/transaction/MosaicGlobalRestrictionTransaction.ts b/src/model/transaction/MosaicGlobalRestrictionTransaction.ts index 1b49985af5..f55a120269 100644 --- a/src/model/transaction/MosaicGlobalRestrictionTransaction.ts +++ b/src/model/transaction/MosaicGlobalRestrictionTransaction.ts @@ -14,16 +14,16 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; -import { EmbeddedMosaicGlobalRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedMosaicGlobalRestrictionTransactionBuilder'; +import { + EmbeddedMosaicGlobalRestrictionTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedMosaicGlobalRestrictionTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { MosaicGlobalRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/MosaicGlobalRestrictionTransactionBuilder'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; import { UnresolvedMosaicIdDto } from '../../infrastructure/catbuffer/UnresolvedMosaicIdDto'; -import { Address } from '../account/Address'; import { PublicAccount } from '../account/PublicAccount'; import { NetworkType } from '../blockchain/NetworkType'; import { MosaicId } from '../mosaic/MosaicId'; @@ -142,12 +142,10 @@ export class MosaicGlobalRestrictionTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMosaicGlobalRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MosaicGlobalRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -166,7 +164,7 @@ export class MosaicGlobalRestrictionTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicGlobalRestrictionTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/MosaicMetadataTransaction.ts b/src/model/transaction/MosaicMetadataTransaction.ts index b85bc36de7..383312379a 100644 --- a/src/model/transaction/MosaicMetadataTransaction.ts +++ b/src/model/transaction/MosaicMetadataTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { EmbeddedMosaicMetadataTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedMosaicMetadataTransactionBuilder'; @@ -121,12 +120,10 @@ export class MosaicMetadataTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMosaicMetadataTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MosaicMetadataTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -142,7 +139,7 @@ export class MosaicMetadataTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicMetadataTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/MosaicSupplyChangeTransaction.ts b/src/model/transaction/MosaicSupplyChangeTransaction.ts index bfed50a0a8..034f272b7d 100644 --- a/src/model/transaction/MosaicSupplyChangeTransaction.ts +++ b/src/model/transaction/MosaicSupplyChangeTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { EmbeddedMosaicSupplyChangeTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedMosaicSupplyChangeTransactionBuilder'; @@ -105,12 +104,10 @@ export class MosaicSupplyChangeTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMosaicSupplyChangeTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MosaicSupplyChangeTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -125,7 +122,7 @@ export class MosaicSupplyChangeTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicSupplyChangeTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/MultisigAccountModificationTransaction.ts b/src/model/transaction/MultisigAccountModificationTransaction.ts index 680e1d71fb..df7575d868 100644 --- a/src/model/transaction/MultisigAccountModificationTransaction.ts +++ b/src/model/transaction/MultisigAccountModificationTransaction.ts @@ -14,13 +14,15 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { CosignatoryModificationBuilder } from '../../infrastructure/catbuffer/CosignatoryModificationBuilder'; -import { EmbeddedMultisigAccountModificationTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedMultisigAccountModificationTransactionBuilder'; +import { + EmbeddedMultisigAccountModificationTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedMultisigAccountModificationTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; -import { MultisigAccountModificationTransactionBuilder } from '../../infrastructure/catbuffer/MultisigAccountModificationTransactionBuilder'; +import {MultisigAccountModificationTransactionBuilder, +} from '../../infrastructure/catbuffer/MultisigAccountModificationTransactionBuilder'; import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto'; import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto'; import { PublicAccount } from '../account/PublicAccount'; @@ -107,12 +109,10 @@ export class MultisigAccountModificationTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedMultisigAccountModificationTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : MultisigAccountModificationTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -135,7 +135,7 @@ export class MultisigAccountModificationTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MultisigAccountModificationTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/NamespaceMetadataTransaction.ts b/src/model/transaction/NamespaceMetadataTransaction.ts index a90e8717d1..bf44327ca9 100644 --- a/src/model/transaction/NamespaceMetadataTransaction.ts +++ b/src/model/transaction/NamespaceMetadataTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { EmbeddedNamespaceMetadataTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedNamespaceMetadataTransactionBuilder'; @@ -121,12 +120,10 @@ export class NamespaceMetadataTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedNamespaceMetadataTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : NamespaceMetadataTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -143,7 +140,7 @@ export class NamespaceMetadataTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as NamespaceMetadataTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/NamespaceRegistrationTransaction.ts b/src/model/transaction/NamespaceRegistrationTransaction.ts index 75015f6b49..436bf20c9a 100644 --- a/src/model/transaction/NamespaceRegistrationTransaction.ts +++ b/src/model/transaction/NamespaceRegistrationTransaction.ts @@ -14,11 +14,12 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, Convert as convert } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { BlockDurationDto } from '../../infrastructure/catbuffer/BlockDurationDto'; -import { EmbeddedNamespaceRegistrationTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedNamespaceRegistrationTransactionBuilder'; +import { + EmbeddedNamespaceRegistrationTransactionBuilder, +} from '../../infrastructure/catbuffer/EmbeddedNamespaceRegistrationTransactionBuilder'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { NamespaceIdDto } from '../../infrastructure/catbuffer/NamespaceIdDto'; import { NamespaceRegistrationTransactionBuilder } from '../../infrastructure/catbuffer/NamespaceRegistrationTransactionBuilder'; @@ -151,12 +152,10 @@ export class NamespaceRegistrationTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedNamespaceRegistrationTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : NamespaceRegistrationTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const namespaceType = builder.getRegistrationType().valueOf(); @@ -179,7 +178,7 @@ export class NamespaceRegistrationTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as NamespaceRegistrationTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/SecretLockTransaction.ts b/src/model/transaction/SecretLockTransaction.ts index f06be2630f..e95d1b40a4 100644 --- a/src/model/transaction/SecretLockTransaction.ts +++ b/src/model/transaction/SecretLockTransaction.ts @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, Convert as convert, RawAddress } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { BlockDurationDto } from '../../infrastructure/catbuffer/BlockDurationDto'; import { EmbeddedSecretLockTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedSecretLockTransactionBuilder'; -import { EntityTypeDto } from '../../infrastructure/catbuffer/EntityTypeDto'; import { Hash256Dto } from '../../infrastructure/catbuffer/Hash256Dto'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { SecretLockTransactionBuilder } from '../../infrastructure/catbuffer/SecretLockTransactionBuilder'; @@ -129,12 +127,10 @@ export class SecretLockTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedSecretLockTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : SecretLockTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -154,7 +150,7 @@ export class SecretLockTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as SecretLockTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/SecretProofTransaction.ts b/src/model/transaction/SecretProofTransaction.ts index f5b47109cf..60a365616a 100644 --- a/src/model/transaction/SecretProofTransaction.ts +++ b/src/model/transaction/SecretProofTransaction.ts @@ -14,11 +14,9 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, Convert as convert, RawAddress } from '../../core/format'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; import { EmbeddedSecretProofTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedSecretProofTransactionBuilder'; -import { EntityTypeDto } from '../../infrastructure/catbuffer/EntityTypeDto'; import { Hash256Dto } from '../../infrastructure/catbuffer/Hash256Dto'; import { KeyDto } from '../../infrastructure/catbuffer/KeyDto'; import { SecretProofTransactionBuilder } from '../../infrastructure/catbuffer/SecretProofTransactionBuilder'; @@ -105,12 +103,10 @@ export class SecretProofTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedSecretProofTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : SecretProofTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key); @@ -126,7 +122,7 @@ export class SecretProofTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as SecretProofTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/transaction/Transaction.ts b/src/model/transaction/Transaction.ts index 0ee907d713..d9ce19ec56 100644 --- a/src/model/transaction/Transaction.ts +++ b/src/model/transaction/Transaction.ts @@ -83,9 +83,10 @@ export abstract class Transaction { * Generate transaction hash hex * @param {string} transactionPayload HexString Payload * @param {Array} generationHashBuffer Network generation hash byte + * @param {NetworkType} networkType Catapult network identifier * @returns {string} Returns Transaction Payload hash */ - public static createTransactionHash(transactionPayload: string, generationHashBuffer: number[]): string { + public static createTransactionHash(transactionPayload: string, generationHashBuffer: number[], networkType: NetworkType): string { const byteBuffer = Array.from(Convert.hexToUint8(transactionPayload)); const signingBytes = byteBuffer .slice(4, 36) @@ -96,8 +97,8 @@ export abstract class Transaction { .splice(4 + 64 + 32, byteBuffer.length)); const hash = new Uint8Array(32); - - SHA3Hasher.func(hash, signingBytes, 32); + const signSchema = SHA3Hasher.resolveSignSchema(networkType); + SHA3Hasher.func(hash, signingBytes, 32, signSchema); return Convert.uint8ToHex(hash); } @@ -117,11 +118,11 @@ export abstract class Transaction { * Serialize and sign transaction creating a new SignedTransaction * @param account - The account to sign the transaction * @param generationHash - Network generation hash hex - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {SignedTransaction} */ - public signWith(account: Account, generationHash: string, signSchema: SignSchema = SignSchema.SHA3): SignedTransaction { + public signWith(account: Account, generationHash: string): SignedTransaction { const generationHashBytes = Array.from(Convert.hexToUint8(generationHash)); + const signSchema = SHA3Hasher.resolveSignSchema(account.networkType); const byteBuffer = Array.from(this.generateBytes()); const signingBytes = generationHashBytes.concat(byteBuffer.slice(4 + 64 + 32)); const keyPairEncoded = KeyPair.createKeyPairFromPrivateKeyString(account.privateKey, signSchema); @@ -135,7 +136,7 @@ export abstract class Transaction { const payload = Convert.uint8ToHex(signedTransactionBuffer); return new SignedTransaction( payload, - Transaction.createTransactionHash(payload, generationHashBytes), + Transaction.createTransactionHash(payload, generationHashBytes, account.networkType), account.publicKey, this.type, this.networkType); diff --git a/src/model/transaction/TransferTransaction.ts b/src/model/transaction/TransferTransaction.ts index c2d7d325f7..4be30510df 100644 --- a/src/model/transaction/TransferTransaction.ts +++ b/src/model/transaction/TransferTransaction.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SignSchema } from '../../core/crypto/SignSchema'; import { Convert, Convert as convert } from '../../core/format'; import { RawAddress } from '../../core/format/RawAddress'; import { AmountDto } from '../../infrastructure/catbuffer/AmountDto'; @@ -112,12 +111,10 @@ export class TransferTransaction extends Transaction { * Create a transaction object from payload * @param {string} payload Binary payload * @param {Boolean} isEmbedded Is embedded transaction (Default: false) - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Transaction | InnerTransaction} */ public static createFromPayload(payload: string, - isEmbedded: boolean = false, - signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction { + isEmbedded: boolean = false): Transaction | InnerTransaction { const builder = isEmbedded ? EmbeddedTransferTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) : TransferTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)); const messageType = builder.getMessage()[0]; @@ -140,7 +137,7 @@ export class TransferTransaction extends Transaction { isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as TransferTransactionBuilder).fee.amount), ); return isEmbedded ? - transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType, signSchema)) : transaction; + transaction.toAggregate(PublicAccount.createFromPublicKey(signerPublicKey, networkType)) : transaction; } /** diff --git a/src/model/wallet/SimpleWallet.ts b/src/model/wallet/SimpleWallet.ts index 31cd67c98c..3b4d51f21e 100644 --- a/src/model/wallet/SimpleWallet.ts +++ b/src/model/wallet/SimpleWallet.ts @@ -15,7 +15,7 @@ */ import {LocalDateTime} from 'js-joda'; -import {Crypto, KeyPair, SignSchema} from '../../core/crypto'; +import {Crypto, KeyPair, SHA3Hasher} from '../../core/crypto'; import { Convert as convert} from '../../core/format'; import {Account} from '../account/Account'; import {Address} from '../account/Address'; @@ -53,13 +53,11 @@ export class SimpleWallet extends Wallet { * @param name - Wallet name * @param password - Password to encrypt wallet * @param network - Network id - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {SimpleWallet} */ public static create(name: string, password: Password, - network: NetworkType, - signSchema: SignSchema = SignSchema.SHA3): SimpleWallet { + network: NetworkType): SimpleWallet { // Create random bytes const randomBytesArray = Crypto.randomBytes(32); // Hash random bytes with entropy seed @@ -67,6 +65,7 @@ export class SimpleWallet extends Wallet { const hashKey = convert.uint8ToHex(randomBytesArray); // TODO: derive private key correctly // Create KeyPair from hash key + const signSchema = SHA3Hasher.resolveSignSchema(network); const keyPair = KeyPair.createKeyPairFromPrivateKeyString(hashKey, signSchema); // Create address from public key @@ -86,19 +85,18 @@ export class SimpleWallet extends Wallet { * @param password - Password to encrypt wallet * @param privateKey - Wallet private key * @param network - Network id - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {SimpleWallet} */ static createFromPrivateKey(name: string, password: Password, privateKey: string, - network: NetworkType, - signSchema: SignSchema = SignSchema.SHA3): SimpleWallet { + network: NetworkType): SimpleWallet { // Create KeyPair from hash key + const signSchema = SHA3Hasher.resolveSignSchema(network); const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKey, signSchema); // Create address from public key - const address = Address.createFromPublicKey(convert.uint8ToHex(keyPair.publicKey), network, signSchema); + const address = Address.createFromPublicKey(convert.uint8ToHex(keyPair.publicKey), network); // Encrypt private key using password const encrypted = Crypto.encodePrivateKey(privateKey, password.value); @@ -111,11 +109,10 @@ export class SimpleWallet extends Wallet { /** * Open a wallet and generate an Account * @param password - Password to decrypt private key - * @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3) * @returns {Account} */ - public open(password: Password, signSchema: SignSchema = SignSchema.SHA3): Account { - return Account.createFromPrivateKey(this.encryptedPrivateKey.decrypt(password), this.network, signSchema); + public open(password: Password): Account { + return Account.createFromPrivateKey(this.encryptedPrivateKey.decrypt(password), this.network); } } diff --git a/test/core/crypto/crypto.spec.ts b/test/core/crypto/crypto.spec.ts index f022085c48..f96733fe98 100644 --- a/test/core/crypto/crypto.spec.ts +++ b/test/core/crypto/crypto.spec.ts @@ -14,9 +14,9 @@ * limitations under the License. */ import {expect} from 'chai'; -import {Crypto} from '../../../src/core/crypto'; +import {Crypto, SignSchema} from '../../../src/core/crypto'; import {Convert as convert} from '../../../src/core/format'; -import { WalletAlgorithm } from '../../../src/model/model'; +import { WalletAlgorithm } from '../../../src/model/wallet/WalletAlgorithm'; const CryptoJS = require('crypto-js'); describe('crypto tests', () => { @@ -340,11 +340,11 @@ describe('crypto tests', () => { const senderPriv = 'E1C8521608F4896CA26A0C2DE739310EA4B06861D126CF4D6922064678A1969B'; const recipientPublic = '12AAD2D33020C3EAE12592875CD7D2FF54A61DD03C1FAADB84A083D41F75C229'; const message = 'NEM is awesome !'; - const encryptedMessage = Crypto.encode(senderPriv, recipientPublic, message); + const encryptedMessage = Crypto.encode(senderPriv, recipientPublic, message, SignSchema.SHA3); const senderPublic = '9F784BF20318AE3CA6246C0EC2207FE095FFF7A84B6787E7E3C2CE4C3B92A2EA'; const recipientPriv = 'A22A4BBF126A2D7D7ECE823174DFD184C5DE0FDE4CB2075D30CFA409F7EF8908'; const expectedMessage = 'NEM is awesome !'; - const decrypted = Crypto.decode(recipientPriv, senderPublic, encryptedMessage); + const decrypted = Crypto.decode(recipientPriv, senderPublic, encryptedMessage, SignSchema.SHA3); expect(decrypted).equal(convert.utf8ToHex(expectedMessage)); }); @@ -353,11 +353,11 @@ describe('crypto tests', () => { const senderPriv = 'E1C8521608F4896CA26A0C2DE739310EA4B06861D126CF4D6922064678A1969B'; const recipientPublic = '12AAD2D33020C3EAE12592875CD7D2FF54A61DD03C1FAADB84A083D41F75C229'; const message = 'NEM is awesome !'; - const encryptedMessage = Crypto.encode(senderPriv, recipientPublic, message); + const encryptedMessage = Crypto.encode(senderPriv, recipientPublic, message, SignSchema.SHA3); const senderPublic = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; const recipientPriv = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; const expectedMessage = 'NEM is awesome !'; - const decrypted = Crypto.decode(recipientPriv, senderPublic, encryptedMessage); + const decrypted = Crypto.decode(recipientPriv, senderPublic, encryptedMessage, SignSchema.SHA3); expect(decrypted).not.equal(convert.utf8ToHex(expectedMessage)); }); diff --git a/test/core/crypto/keyPair.spec.ts b/test/core/crypto/keyPair.spec.ts index 3b8df8fc10..60e968b17e 100644 --- a/test/core/crypto/keyPair.spec.ts +++ b/test/core/crypto/keyPair.spec.ts @@ -14,15 +14,17 @@ * limitations under the License. */ import {expect} from 'chai'; -import {Crypto, KeyPair} from '../../../src/core/crypto'; -import { SignSchema } from '../../../src/core/crypto/SignSchema'; -import {Convert as convert} from '../../../src/core/format'; +import {Crypto, KeyPair, SHA3Hasher, SignSchema} from '../../../src/core/crypto'; import { Convert } from '../../../src/core/format/Convert'; +import { NetworkType } from '../../../src/model/blockchain/NetworkType'; describe('key pair', () => { - const randomKeyPair = () => KeyPair.createKeyPairFromPrivateKeyString(convert.uint8ToHex(Crypto.randomBytes(32))); + const randomKeyPair = () => + KeyPair.createKeyPairFromPrivateKeyString(Convert.uint8ToHex(Crypto.randomBytes(32)), SignSchema.SHA3); const Private_Key_Size = 32; const Signature_Size = 64; + const mijinTestSignSchema = SHA3Hasher.resolveSignSchema(NetworkType.MIJIN_TEST); + const nis1TestSignSchema = SHA3Hasher.resolveSignSchema(NetworkType.TEST_NET); const Private_Keys = [ '8D31B712AB28D49591EAF5066E9E967B44507FC19C3D54D742F7B3A255CFF4AB', @@ -52,12 +54,12 @@ describe('key pair', () => { const expectedPublicKey = Expected_Public_Keys[i]; // Act: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex, mijinTestSignSchema); // Assert: const message = ` from ${privateKeyHex}`; - expect(convert.uint8ToHex(keyPair.publicKey), `public ${message}`).equal(expectedPublicKey); - expect(convert.uint8ToHex(keyPair.privateKey), `private ${message}`).equal(privateKeyHex); + expect(Convert.uint8ToHex(keyPair.publicKey), `public ${message}`).equal(expectedPublicKey); + expect(Convert.uint8ToHex(keyPair.privateKey), `private ${message}`).equal(privateKeyHex); } }); @@ -73,7 +75,7 @@ describe('key pair', () => { invalidPrivateKeys.forEach((privateKey) => { // Assert: expect(() => { - KeyPair.createKeyPairFromPrivateKeyString(privateKey); + KeyPair.createKeyPairFromPrivateKeyString(privateKey, mijinTestSignSchema); }, `from ${privateKey}`) .to.throw('private key has unexpected size'); }); @@ -107,16 +109,16 @@ describe('key pair', () => { for (let i = 0; i < Nis1_Private_Key.length; ++i) { // Arrange: - const privateKeyHex = Nis1_Private_Key[i]; + const privateKeyHex = Convert.uint8ToHex(Convert.hexToUint8Reverse(Nis1_Private_Key[i])); const expectedPublicKey = Expected_Public_Keys[i]; // Act: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex, SignSchema.KECCAK_REVERSED_KEY); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex, nis1TestSignSchema); // Assert: const message = ` from ${privateKeyHex}`; - expect(convert.uint8ToHex(keyPair.publicKey).toUpperCase(), `public ${message}`).equal(expectedPublicKey.toUpperCase()); - expect(convert.uint8ToHex(keyPair.privateKey).toUpperCase(), `private ${message}`).equal(privateKeyHex.toUpperCase()); + expect(Convert.uint8ToHex(keyPair.publicKey).toUpperCase(), `public ${message}`).equal(expectedPublicKey.toUpperCase()); + expect(Convert.uint8ToHex(keyPair.privateKey).toUpperCase(), `private ${message}`).equal(privateKeyHex.toUpperCase()); } }); @@ -132,7 +134,7 @@ describe('key pair', () => { invalidPrivateKeys.forEach((privateKey) => { // Assert: expect(() => { - KeyPair.createKeyPairFromPrivateKeyString(privateKey); + KeyPair.createKeyPairFromPrivateKeyString(privateKey, mijinTestSignSchema); }, `from ${privateKey}`) .to.throw('private key has unexpected size'); }); @@ -170,12 +172,12 @@ describe('key pair', () => { const expectedPublicKey = Expected_Public_Keys[i]; // Act: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex, mijinTestSignSchema); // Assert: const message = ` from ${privateKeyHex}`; - expect(convert.uint8ToHex(keyPair.publicKey).toUpperCase(), `public ${message}`).equal(expectedPublicKey.toUpperCase()); - expect(convert.uint8ToHex(keyPair.privateKey).toUpperCase(), `private ${message}`).equal(privateKeyHex.toUpperCase()); + expect(Convert.uint8ToHex(keyPair.publicKey).toUpperCase(), `public ${message}`).equal(expectedPublicKey.toUpperCase()); + expect(Convert.uint8ToHex(keyPair.privateKey).toUpperCase(), `private ${message}`).equal(privateKeyHex.toUpperCase()); } }); @@ -191,7 +193,7 @@ describe('key pair', () => { invalidPrivateKeys.forEach((privateKey) => { // Assert: expect(() => { - KeyPair.createKeyPairFromPrivateKeyString(privateKey); + KeyPair.createKeyPairFromPrivateKeyString(privateKey, mijinTestSignSchema); }, `from ${privateKey}`) .to.throw('private key has unexpected size'); }); @@ -229,12 +231,12 @@ describe('key pair', () => { const expectedPublicKey = Expected_Public_Keys[i]; // Act: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKeyHex, mijinTestSignSchema); // Assert: const message = ` from ${privateKeyHex}`; - expect(convert.uint8ToHex(keyPair.publicKey).toUpperCase(), `public ${message}`).equal(expectedPublicKey.toUpperCase()); - expect(convert.uint8ToHex(keyPair.privateKey).toUpperCase(), `private ${message}`).equal(privateKeyHex.toUpperCase()); + expect(Convert.uint8ToHex(keyPair.publicKey).toUpperCase(), `public ${message}`).equal(expectedPublicKey.toUpperCase()); + expect(Convert.uint8ToHex(keyPair.privateKey).toUpperCase(), `private ${message}`).equal(privateKeyHex.toUpperCase()); } }); @@ -250,7 +252,7 @@ describe('key pair', () => { invalidPrivateKeys.forEach((privateKey) => { // Assert: expect(() => { - KeyPair.createKeyPairFromPrivateKeyString(privateKey); + KeyPair.createKeyPairFromPrivateKeyString(privateKey, mijinTestSignSchema); }, `from ${privateKey}`) .to.throw('private key has unexpected size'); }); @@ -286,18 +288,19 @@ describe('key pair', () => { for (let i = 0; i < Nis1_Private_Key.length; ++i) { // Arrange: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Nis1_Private_Key[i], SignSchema.KECCAK_REVERSED_KEY); + const privateKey = Convert.uint8ToHex(Convert.hexToUint8Reverse(Nis1_Private_Key[i])); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(privateKey, nis1TestSignSchema); const payload = Convert.hexToUint8(Nis1_Data[i]); // Act: - const signature = KeyPair.sign(keyPair, payload, SignSchema.KECCAK_REVERSED_KEY); + const signature = KeyPair.sign(keyPair, payload, nis1TestSignSchema); // Assert: const message = ` from ${Nis1_Private_Key[i]}`; - expect(Convert.uint8ToHex(KeyPair.sign(keyPair, payload, SignSchema.KECCAK_REVERSED_KEY)).toUpperCase(), + expect(Convert.uint8ToHex(KeyPair.sign(keyPair, payload, nis1TestSignSchema)).toUpperCase(), `private ${message}`).to.deep.equal(Expected_Signature[i].toUpperCase()); - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, SignSchema.KECCAK_REVERSED_KEY); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, nis1TestSignSchema); expect(isVerified, `private ${message}`).to.equal(true); } }); @@ -330,17 +333,17 @@ describe('key pair', () => { for (let i = 0; i < Catapult_Private_Key.length; ++i) { // Arrange: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Catapult_Private_Key[i], SignSchema.SHA3); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Catapult_Private_Key[i], mijinTestSignSchema); const payload = Convert.hexToUint8(Catapult_Data[i]); // Act: - const signature = KeyPair.sign(keyPair, payload, SignSchema.SHA3); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); // Assert: const message = ` from ${Catapult_Private_Key[i]}`; expect(Convert.uint8ToHex(signature).toUpperCase(), `private ${message}`).to.deep.equal(Expected_Signature[i].toUpperCase()); - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); expect(isVerified, `private ${message}`).to.equal(true); } }); @@ -353,7 +356,7 @@ describe('key pair', () => { const payload = Crypto.randomBytes(100); // Act: - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); // Assert: expect(signature).to.not.deep.equal(new Uint8Array(Signature_Size)); @@ -361,14 +364,14 @@ describe('key pair', () => { it('returns same signature for same data signed by same key pairs', () => { // Arrange: - const privateKey = convert.uint8ToHex(Crypto.randomBytes(Private_Key_Size)); - const keyPair1 = KeyPair.createKeyPairFromPrivateKeyString(privateKey); - const keyPair2 = KeyPair.createKeyPairFromPrivateKeyString(privateKey); + const privateKey = Convert.uint8ToHex(Crypto.randomBytes(Private_Key_Size)); + const keyPair1 = KeyPair.createKeyPairFromPrivateKeyString(privateKey, mijinTestSignSchema); + const keyPair2 = KeyPair.createKeyPairFromPrivateKeyString(privateKey, mijinTestSignSchema); const payload = Crypto.randomBytes(100); // Act: - const signature1 = KeyPair.sign(keyPair1, payload); - const signature2 = KeyPair.sign(keyPair2, payload); + const signature1 = KeyPair.sign(keyPair1, payload, mijinTestSignSchema); + const signature2 = KeyPair.sign(keyPair2, payload, mijinTestSignSchema); // Assert: expect(signature2).to.deep.equal(signature1); @@ -381,22 +384,13 @@ describe('key pair', () => { const payload = Crypto.randomBytes(100); // Act: - const signature1 = KeyPair.sign(keyPair1, payload); - const signature2 = KeyPair.sign(keyPair2, payload); + const signature1 = KeyPair.sign(keyPair1, payload, mijinTestSignSchema); + const signature2 = KeyPair.sign(keyPair2, payload, mijinTestSignSchema); // Assert: expect(signature2).to.not.deep.equal(signature1); }); - it('cannot sign unsupported data type', () => { - // Arrange: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[0]); - - // Assert: - expect(() => { - KeyPair.sign(keyPair, {}); - }).to.throw('unsupported data type'); - }); }); describe('verify', () => { @@ -404,10 +398,10 @@ describe('key pair', () => { // Arrange: const keyPair = randomKeyPair(); const payload = Crypto.randomBytes(100); - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified).to.equal(true); @@ -418,10 +412,10 @@ describe('key pair', () => { const keyPair1 = randomKeyPair(); const keyPair2 = randomKeyPair(); const payload = Crypto.randomBytes(100); - const signature = KeyPair.sign(keyPair1, payload); + const signature = KeyPair.sign(keyPair1, payload, mijinTestSignSchema); // Act: - const isVerified = KeyPair.verify(keyPair2.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair2.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified).to.equal(false); @@ -433,11 +427,11 @@ describe('key pair', () => { const payload = Crypto.randomBytes(100); for (let i = 0; i < Signature_Size; i += 4) { - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); signature[i] ^= 0xFF; // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified, `signature modified at ${i}`).to.equal(false); @@ -450,11 +444,11 @@ describe('key pair', () => { const payload = Crypto.randomBytes(44); for (let i = 0; i < payload.length; i += 4) { - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); payload[i] ^= 0xFF; // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified, `payload modified at ${i}`).to.equal(false); @@ -468,10 +462,10 @@ describe('key pair', () => { keyPair.publicKey[keyPair.publicKey.length - 1] = 1; const payload = Crypto.randomBytes(100); - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified).to.equal(false); @@ -481,14 +475,14 @@ describe('key pair', () => { // Arrange: const keyPair = randomKeyPair(); const payload = Crypto.randomBytes(100); - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); for (let i = 0; i < keyPair.publicKey.length; ++i) { keyPair.publicKey[i] ^= 0xFF; } // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified).to.equal(false); @@ -500,10 +494,10 @@ describe('key pair', () => { keyPair.publicKey.fill(0); const payload = Crypto.randomBytes(100); - const signature = KeyPair.sign(keyPair, payload); + const signature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, payload, signature, mijinTestSignSchema); // Assert: expect(isVerified).to.equal(false); @@ -528,15 +522,15 @@ describe('key pair', () => { // Arrange: const keyPair = randomKeyPair(); const payload = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); - const canonicalSignature = KeyPair.sign(keyPair, payload); + const canonicalSignature = KeyPair.sign(keyPair, payload, mijinTestSignSchema); // this is signature with group order added to 'encodedS' part of signature const nonCanonicalSignature = canonicalSignature.slice(); scalarAddGroupOrder(nonCanonicalSignature.subarray(32)); // Act: - const isCanonicalVerified = KeyPair.verify(keyPair.publicKey, payload, canonicalSignature); - const isNonCanonicalVerified = KeyPair.verify(keyPair.privateKey, payload, nonCanonicalSignature); + const isCanonicalVerified = KeyPair.verify(keyPair.publicKey, payload, canonicalSignature, mijinTestSignSchema); + const isNonCanonicalVerified = KeyPair.verify(keyPair.privateKey, payload, nonCanonicalSignature, mijinTestSignSchema); // Assert: expect(isCanonicalVerified).to.equal(true); @@ -573,14 +567,14 @@ describe('key pair', () => { for (let i = 0; i < Private_Keys.length; ++i) { // Arrange: const inputData = dataTransform(Input_Data[i]); - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[i]); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[i], mijinTestSignSchema); // Act: - const signature = KeyPair.sign(keyPair, inputData); + const signature = KeyPair.sign(keyPair, inputData, mijinTestSignSchema); // Assert: const message = `signing with ${Private_Keys[i]}`; - expect(convert.uint8ToHex(signature), message).equal(Expected_Signatures[i]); + expect(Convert.uint8ToHex(signature), message).equal(Expected_Signatures[i]); } } @@ -591,7 +585,7 @@ describe('key pair', () => { it('can sign test vectors as binary', () => { // Assert: - assertCanSignTestVectors((data) => convert.hexToUint8(data)); + assertCanSignTestVectors((data) => Convert.hexToUint8(data)); }); function assertCanVerifyTestVectors(dataTransform) { @@ -602,11 +596,11 @@ describe('key pair', () => { for (let i = 0; i < Private_Keys.length; ++i) { // Arrange: const inputData = dataTransform(Input_Data[i]); - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[i]); - const signature = KeyPair.sign(keyPair, inputData); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Keys[i], mijinTestSignSchema); + const signature = KeyPair.sign(keyPair, inputData, mijinTestSignSchema); // Act: - const isVerified = KeyPair.verify(keyPair.publicKey, inputData, signature); + const isVerified = KeyPair.verify(keyPair.publicKey, inputData, signature, mijinTestSignSchema); // Assert: const message = `verifying with ${Private_Keys[i]}`; @@ -621,7 +615,7 @@ describe('key pair', () => { it('can verify test vectors as binary', () => { // Assert: - assertCanVerifyTestVectors((data) => convert.hexToUint8(data)); + assertCanVerifyTestVectors((data) => Convert.hexToUint8(data)); }); }); @@ -636,7 +630,7 @@ describe('key pair', () => { // Act: expect(() => { - KeyPair.deriveSharedKey(keyPair, publicKey, salt); + KeyPair.deriveSharedKey(keyPair, publicKey, salt, mijinTestSignSchema); }) .to.throw('salt has unexpected size'); }); @@ -648,8 +642,8 @@ describe('key pair', () => { const salt = Crypto.randomBytes(Salt_Size); // Act: - const sharedKey1 = KeyPair.deriveSharedKey(keyPair1, keyPair2.publicKey, salt); - const sharedKey2 = KeyPair.deriveSharedKey(keyPair2, keyPair1.publicKey, salt); + const sharedKey1 = KeyPair.deriveSharedKey(keyPair1, keyPair2.publicKey, salt, mijinTestSignSchema); + const sharedKey2 = KeyPair.deriveSharedKey(keyPair2, keyPair1.publicKey, salt, mijinTestSignSchema); // Assert: expect(sharedKey1).to.deep.equal(sharedKey2); @@ -663,8 +657,8 @@ describe('key pair', () => { const salt = Crypto.randomBytes(Salt_Size); // Act: - const sharedKey1 = KeyPair.deriveSharedKey(keyPair, publicKey1, salt); - const sharedKey2 = KeyPair.deriveSharedKey(keyPair, publicKey2, salt); + const sharedKey1 = KeyPair.deriveSharedKey(keyPair, publicKey1, salt, mijinTestSignSchema); + const sharedKey2 = KeyPair.deriveSharedKey(keyPair, publicKey2, salt, mijinTestSignSchema); // Assert: expect(sharedKey1).to.not.deep.equal(sharedKey2); @@ -672,15 +666,16 @@ describe('key pair', () => { it('can derive deterministic shared key from well known inputs', () => { // Arrange: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString('8F545C2816788AB41D352F236D80DBBCBC34705B5F902EFF1F1D88327C7C1300'); - const publicKey = convert.hexToUint8('BF684FB1A85A8C8091EE0442EDDB22E51683802AFA0C0E7C6FE3F3E3E87A8D72'); - const salt = convert.hexToUint8('422C39DF16AAE42A74A5597D6EE2D59CFB4EEB6B3F26D98425B9163A03DAA3B5'); + const keyPair = KeyPair.createKeyPairFromPrivateKeyString( + '8F545C2816788AB41D352F236D80DBBCBC34705B5F902EFF1F1D88327C7C1300', mijinTestSignSchema); + const publicKey = Convert.hexToUint8('BF684FB1A85A8C8091EE0442EDDB22E51683802AFA0C0E7C6FE3F3E3E87A8D72'); + const salt = Convert.hexToUint8('422C39DF16AAE42A74A5597D6EE2D59CFB4EEB6B3F26D98425B9163A03DAA3B5'); // Act: - const sharedKey = KeyPair.deriveSharedKey(keyPair, publicKey, salt); + const sharedKey = KeyPair.deriveSharedKey(keyPair, publicKey, salt, mijinTestSignSchema); // Assert: - expect(convert.uint8ToHex(sharedKey)).to.equal('007FD607264C64C7BB83509E7CFA96E0FEAF34A373CDA75FACAA4DE9E141257B'); + expect(Convert.uint8ToHex(sharedKey)).to.equal('007FD607264C64C7BB83509E7CFA96E0FEAF34A373CDA75FACAA4DE9E141257B'); }); }); @@ -725,12 +720,13 @@ describe('key pair', () => { for (let i = 0; i < Nis1_Private_Key.length; ++i) { // Arrange: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Nis1_Private_Key[i], SignSchema.KECCAK_REVERSED_KEY); + const privateKey = Convert.uint8ToHex(Convert.hexToUint8Reverse(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, SignSchema.KECCAK_REVERSED_KEY)); + const sharedKey = Convert.uint8ToHex(KeyPair.deriveSharedKey(keyPair, publicKey, salt, nis1TestSignSchema)); // Assert: const message = ` from ${Nis1_Private_Key[i]}`; @@ -780,12 +776,12 @@ describe('key pair', () => { for (let i = 0; i < Private_Key.length; ++i) { // Arrange: - const keyPair = KeyPair.createKeyPairFromPrivateKeyString(Private_Key[i]); + 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)); + const sharedKey = Convert.uint8ToHex(KeyPair.deriveSharedKey(keyPair, publicKey, salt, mijinTestSignSchema)); // Assert: const message = ` from ${Private_Key[i]}`; diff --git a/test/core/crypto/sha3Hasher.spec.ts b/test/core/crypto/sha3Hasher.spec.ts index 4f9b731238..2b44ae163a 100644 --- a/test/core/crypto/sha3Hasher.spec.ts +++ b/test/core/crypto/sha3Hasher.spec.ts @@ -14,8 +14,10 @@ * limitations under the License. */ import {expect} from 'chai'; +import { SignSchema } from '../../../src/core/crypto'; import {SHA3Hasher as sha3Hasher} from '../../../src/core/crypto/SHA3Hasher'; import {Convert as convert} from '../../../src/core/format'; +import { NetworkType } from '../../../src/model/blockchain/NetworkType'; describe('hasher', () => { const inputs = [ @@ -24,7 +26,7 @@ describe('hasher', () => { '41FB', '1F877C', 'C1ECFDFC', - '9F2FCC7C90DE090D6B87CD7E9718C1EA6CB21118FC2D5DE9F97E5DB6AC1E9C10' + '9F2FCC7C90DE090D6B87CD7E9718C1EA6CB21118FC2D5DE9F97E5DB6AC1E9C10', ]; function addSha3Tests(length, expectedOutputs) { @@ -41,7 +43,7 @@ describe('hasher', () => { // Act: const hash = new Uint8Array(length); - sha3Hasher.func(hash, inputBuffer, length); + sha3Hasher.func(hash, inputBuffer, length, SignSchema.SHA3); // Assert: expect(convert.uint8ToHex(hash), `hashing ${inputHex}`).equal(expectedHash); @@ -60,7 +62,7 @@ describe('hasher', () => { const inputBuffer = convert.hexToUint8(inputHex); const expectedHash = expectedOutputs[i]; - const hasher = sha3Hasher.createHasher(length); + const hasher = sha3Hasher.createHasher(length, SignSchema.SHA3); hasher.reset(); // Act: hash the input in two parts @@ -80,7 +82,7 @@ describe('hasher', () => { const inputHex = inputs[3]; const expectedHash = expectedOutputs[3]; - const hasher = sha3Hasher.createHasher(length); + const hasher = sha3Hasher.createHasher(length, SignSchema.SHA3); hasher.reset(); // Act: @@ -95,7 +97,7 @@ describe('hasher', () => { it('cannot hash unsupported data type', () => { // Arrange: - const hasher = sha3Hasher.createHasher(length); + const hasher = sha3Hasher.createHasher(length, SignSchema.SHA3); hasher.reset(); // Act: @@ -107,7 +109,7 @@ describe('hasher', () => { const inputHex = inputs[3]; const expectedHash = expectedOutputs[3]; - const hasher = sha3Hasher.createHasher(length); + const hasher = sha3Hasher.createHasher(length, SignSchema.SHA3); hasher.reset(); hasher.update('ABCD'); @@ -124,6 +126,24 @@ describe('hasher', () => { }); } + describe('Resolve SignSchema', () => { + it('Should return Keccak schema', () => { + let signSchema = sha3Hasher.resolveSignSchema(NetworkType.MAIN_NET); + expect(signSchema).to.be.equal(SignSchema.KECCAK); + + signSchema = sha3Hasher.resolveSignSchema(NetworkType.TEST_NET); + expect(signSchema).to.be.equal(SignSchema.KECCAK); + }); + + it('Should return SHA3 schema', () => { + let signSchema = sha3Hasher.resolveSignSchema(NetworkType.MIJIN); + expect(signSchema).to.be.equal(SignSchema.SHA3); + + signSchema = sha3Hasher.resolveSignSchema(NetworkType.MIJIN_TEST); + expect(signSchema).to.be.equal(SignSchema.SHA3); + }); + }); + describe('sha3 256', () => { // https://github.com/gvanas/KeccakCodePackage/blob/master/TestVectors/ShortMsgKAT_SHA3-256.txt addSha3Tests(32, [ @@ -132,19 +152,25 @@ describe('hasher', () => { '39F31B6E653DFCD9CAED2602FD87F61B6254F581312FB6EEEC4D7148FA2E72AA', 'BC22345E4BD3F792A341CF18AC0789F1C9C966712A501B19D1B6632CCD408EC5', 'C5859BE82560CC8789133F7C834A6EE628E351E504E601E8059A0667FF62C124', - '2F1A5F7159E34EA19CDDC70EBF9B81F1A66DB40615D7EAD3CC1F1B954D82A3AF' + '2F1A5F7159E34EA19CDDC70EBF9B81F1A66DB40615D7EAD3CC1F1B954D82A3AF', ]); }); describe('sha3 512', () => { // https://github.com/gvanas/KeccakCodePackage/blob/master/TestVectors/ShortMsgKAT_SHA3-512.txt addSha3Tests(64, [ - 'A69F73CCA23A9AC5C8B567DC185A756E97C982164FE25859E0D1DCC1475C80A615B2123AF1F5F94C11E3E9402C3AC558F500199D95B6D3E301758586281DCD26', - '3939FCC8B57B63612542DA31A834E5DCC36E2EE0F652AC72E02624FA2E5ADEECC7DD6BB3580224B4D6138706FC6E80597B528051230B00621CC2B22999EAA205', - 'AA092865A40694D91754DBC767B5202C546E226877147A95CB8B4C8F8709FE8CD6905256B089DA37896EA5CA19D2CD9AB94C7192FC39F7CD4D598975A3013C69', - 'CB20DCF54955F8091111688BECCEF48C1A2F0D0608C3A575163751F002DB30F40F2F671834B22D208591CFAF1F5ECFE43C49863A53B3225BDFD7C6591BA7658B', - 'D4B4BDFEF56B821D36F4F70AB0D231B8D0C9134638FD54C46309D14FADA92A2840186EED5415AD7CF3969BDFBF2DAF8CCA76ABFE549BE6578C6F4143617A4F1A', - 'B087C90421AEBF87911647DE9D465CBDA166B672EC47CCD4054A7135A1EF885E7903B52C3F2C3FE722B1C169297A91B82428956A02C631A2240F12162C7BC726' + 'A69F73CCA23A9AC5C8B567DC185A756E97C982164FE25859E0D1DCC1475C80A615' + + 'B2123AF1F5F94C11E3E9402C3AC558F500199D95B6D3E301758586281DCD26', + '3939FCC8B57B63612542DA31A834E5DCC36E2EE0F652AC72E02624FA2E5ADEECC7' + + 'DD6BB3580224B4D6138706FC6E80597B528051230B00621CC2B22999EAA205', + 'AA092865A40694D91754DBC767B5202C546E226877147A95CB8B4C8F8709FE8CD69' + + '05256B089DA37896EA5CA19D2CD9AB94C7192FC39F7CD4D598975A3013C69', + 'CB20DCF54955F8091111688BECCEF48C1A2F0D0608C3A575163751F002DB30F40F2' + + 'F671834B22D208591CFAF1F5ECFE43C49863A53B3225BDFD7C6591BA7658B', + 'D4B4BDFEF56B821D36F4F70AB0D231B8D0C9134638FD54C46309D14FADA92A28401' + + '86EED5415AD7CF3969BDFBF2DAF8CCA76ABFE549BE6578C6F4143617A4F1A', + 'B087C90421AEBF87911647DE9D465CBDA166B672EC47CCD4054A7135A1EF885E7903' + + 'B52C3F2C3FE722B1C169297A91B82428956A02C631A2240F12162C7BC726', ]); }); }); diff --git a/test/core/format/RawAddress.spec.ts b/test/core/format/RawAddress.spec.ts index 4c5dc899ec..da80fc96f5 100644 --- a/test/core/format/RawAddress.spec.ts +++ b/test/core/format/RawAddress.spec.ts @@ -14,7 +14,6 @@ * limitations under the License. */ import {expect} from 'chai'; -import { SignSchema } from '../../../src/core/crypto'; import { Convert as convert, RawAddress as address, @@ -22,8 +21,6 @@ import { import { NetworkType } from '../../../src/model/model'; const Address_Decoded_Size = 25; -const Network_Mijin_Identifier = 0x60; -const Network_Public_Test_Identifier = 0x98; describe('address', () => { describe('stringToAddress', () => { @@ -43,7 +40,7 @@ describe('address', () => { const decoded = address.stringToAddress(encoded); // Assert: - expect(address.isValidAddress(decoded)).to.equal(true); + expect(address.isValidAddress(decoded, NetworkType.MIJIN_TEST)).to.equal(true); expect(convert.uint8ToHex(decoded)).to.equal(expectedHex); }); @@ -51,7 +48,7 @@ describe('address', () => { // Assert: assertCannotCreateAddress( 'NC5J5DI2URIC4H3T3IMXQS25PWQWZIPEV6EV7LASABCDEFGH', - 'NC5J5DI2URIC4H3T3IMXQS25PWQWZIPEV6EV7LASABCDEFGH does not represent a valid encoded address' + 'NC5J5DI2URIC4H3T3IMXQS25PWQWZIPEV6EV7LASABCDEFGH does not represent a valid encoded address', ); }); @@ -84,25 +81,25 @@ describe('address', () => { const publicKey = convert.hexToUint8('3485D98EFD7EB07ADAFCFD1A157D89DE2796A95E780813C0258AF3F5F84ED8CB'); // Act: - const decoded = address.publicKeyToAddress(publicKey, Network_Mijin_Identifier); + const decoded = address.publicKeyToAddress(publicKey, NetworkType.MIJIN); // Assert: - expect(decoded[0]).to.equal(Network_Mijin_Identifier); - expect(address.isValidAddress(decoded)).to.equal(true); + expect(decoded[0]).to.equal(NetworkType.MIJIN); + expect(address.isValidAddress(decoded, NetworkType.MIJIN)).to.equal(true); expect(convert.uint8ToHex(decoded)).to.equal(expectedHex); }); it('can create address from public key for custom network', () => { // Arrange: - const expectedHex = '9823BB7C3C089D996585466380EDBDC19D495918484BF7E997'; + const expectedHex = '9023BB7C3C089D996585466380EDBDC19D495918486F4F86A7'; const publicKey = convert.hexToUint8('3485D98EFD7EB07ADAFCFD1A157D89DE2796A95E780813C0258AF3F5F84ED8CB'); // Act: - const decoded = address.publicKeyToAddress(publicKey, Network_Public_Test_Identifier); + const decoded = address.publicKeyToAddress(publicKey, NetworkType.MIJIN_TEST); // Assert: - expect(decoded[0]).to.equal(Network_Public_Test_Identifier); - expect(address.isValidAddress(decoded)).to.equal(true); + expect(decoded[0]).to.equal(NetworkType.MIJIN_TEST); + expect(address.isValidAddress(decoded, NetworkType.MIJIN_TEST)).to.equal(true); expect(convert.uint8ToHex(decoded)).to.equal(expectedHex); }); @@ -111,11 +108,11 @@ describe('address', () => { const publicKey = convert.hexToUint8('3485D98EFD7EB07ADAFCFD1A157D89DE2796A95E780813C0258AF3F5F84ED8CB'); // Act: - const decoded1 = address.publicKeyToAddress(publicKey, Network_Mijin_Identifier); - const decoded2 = address.publicKeyToAddress(publicKey, Network_Mijin_Identifier); + const decoded1 = address.publicKeyToAddress(publicKey, NetworkType.MIJIN_TEST); + const decoded2 = address.publicKeyToAddress(publicKey, NetworkType.MIJIN_TEST); // Assert: - expect(address.isValidAddress(decoded1)).to.equal(true); + expect(address.isValidAddress(decoded1, NetworkType.MIJIN_TEST)).to.equal(true); expect(decoded1).to.deep.equal(decoded2); }); @@ -125,12 +122,12 @@ describe('address', () => { const publicKey2 = convert.hexToUint8('b4f12e7c9f6946091e2cb8b6d3a12b50d17ccbbf646386ea27ce2946a7423dcf'); // Act: - const decoded1 = address.publicKeyToAddress(publicKey1, Network_Mijin_Identifier); - const decoded2 = address.publicKeyToAddress(publicKey2, Network_Mijin_Identifier); + const decoded1 = address.publicKeyToAddress(publicKey1, NetworkType.MIJIN_TEST); + const decoded2 = address.publicKeyToAddress(publicKey2, NetworkType.MIJIN_TEST); // Assert: - expect(address.isValidAddress(decoded1)).to.equal(true); - expect(address.isValidAddress(decoded2)).to.equal(true); + expect(address.isValidAddress(decoded1, NetworkType.MIJIN_TEST)).to.equal(true); + expect(address.isValidAddress(decoded2, NetworkType.MIJIN_TEST)).to.equal(true); expect(decoded1).to.not.deep.equal(decoded2); }); @@ -139,12 +136,12 @@ describe('address', () => { const publicKey = convert.hexToUint8('b4f12e7c9f6946091e2cb8b6d3a12b50d17ccbbf646386ea27ce2946a7423dcf'); // Act: - const decoded1 = address.publicKeyToAddress(publicKey, Network_Mijin_Identifier); - const decoded2 = address.publicKeyToAddress(publicKey, Network_Public_Test_Identifier); + const decoded1 = address.publicKeyToAddress(publicKey, NetworkType.MIJIN_TEST); + const decoded2 = address.publicKeyToAddress(publicKey, NetworkType.TEST_NET); // Assert: - expect(address.isValidAddress(decoded1)).to.equal(true); - expect(address.isValidAddress(decoded2)).to.equal(true); + expect(address.isValidAddress(decoded1, NetworkType.MIJIN_TEST)).to.equal(true); + expect(address.isValidAddress(decoded2, NetworkType.TEST_NET)).to.equal(true); expect(decoded1).to.not.deep.equal(decoded2); }); @@ -154,11 +151,11 @@ describe('address', () => { const publicKey = convert.hexToUint8('3485D98EFD7EB07ADAFCFD1A157D89DE2796A95E780813C0258AF3F5F84ED8CB'); // Act: - const decoded = address.publicKeyToAddress(publicKey, Network_Public_Test_Identifier, 1); + const decoded = address.publicKeyToAddress(publicKey, NetworkType.TEST_NET); // Assert: - expect(decoded[0]).to.equal(Network_Public_Test_Identifier); - expect(address.isValidAddress(decoded, SignSchema.KECCAK_REVERSED_KEY)).to.equal(true); + expect(decoded[0]).to.equal(NetworkType.TEST_NET); + expect(address.isValidAddress(decoded, NetworkType.TEST_NET)).to.equal(true); expect(convert.uint8ToHex(decoded)).to.equal(keccakHex); expect(convert.uint8ToHex(decoded)).not.to.equal(nonKeccakHex); }); @@ -171,7 +168,7 @@ describe('address', () => { const decoded = convert.hexToUint8(validHex); // Assert: - expect(address.isValidAddress(decoded)).to.equal(true); + expect(address.isValidAddress(decoded, NetworkType.MIJIN_TEST)).to.equal(true); }); it('returns false for address with invalid checksum', () => { @@ -181,7 +178,7 @@ describe('address', () => { decoded[Address_Decoded_Size - 1] ^= 0xff; // ruin checksum // Assert: - expect(address.isValidAddress(decoded)).to.equal(false); + expect(address.isValidAddress(decoded, NetworkType.MIJIN_TEST)).to.equal(false); }); it('returns false for address with invalid hash', () => { @@ -191,7 +188,7 @@ describe('address', () => { decoded[5] ^= 0xff; // ruin ripemd160 hash // Assert: - expect(address.isValidAddress(decoded)).to.equal(false); + expect(address.isValidAddress(decoded, NetworkType.MIJIN_TEST)).to.equal(false); }); }); @@ -201,7 +198,7 @@ describe('address', () => { const encoded = 'NAR3W7B4BCOZSZMFIZRYB3N5YGOUSWIYJCJ6HDFG'; // Assert: - expect(address.isValidEncodedAddress(encoded)).to.equal(true); + expect(address.isValidEncodedAddress(encoded, NetworkType.MIJIN_TEST)).to.equal(true); }); it('returns false for invalid encoded address', () => { @@ -209,7 +206,7 @@ describe('address', () => { const encoded = 'NAR3W7B4BCOZSZMFIZRYB3N5YGOUSWIYJCJ6HDFH'; // Assert: - expect(address.isValidEncodedAddress(encoded)).to.equal(false); + expect(address.isValidEncodedAddress(encoded, NetworkType.MIJIN_TEST)).to.equal(false); }); it('returns false for encoded address with wrong length', () => { @@ -217,7 +214,7 @@ describe('address', () => { const encoded = 'NAR3W7B4BCOZSZMFIZRYB3N5YGOUSWIYJCJ6HDFGABC'; // Assert: - expect(address.isValidEncodedAddress(encoded)).to.equal(false); + expect(address.isValidEncodedAddress(encoded, NetworkType.MIJIN_TEST)).to.equal(false); }); it('adding leading or trailing white space invalidates encoded address', () => { @@ -225,9 +222,9 @@ describe('address', () => { const encoded = 'NAR3W7B4BCOZSZMFIZRYB3N5YGOUSWIYJCJ6HDFG'; // Assert: - expect(address.isValidEncodedAddress(` \t ${encoded}`)).to.equal(false); - expect(address.isValidEncodedAddress(`${encoded} \t `)).to.equal(false); - expect(address.isValidEncodedAddress(` \t ${encoded} \t `)).to.equal(false); + expect(address.isValidEncodedAddress(` \t ${encoded}`, NetworkType.MIJIN_TEST)).to.equal(false); + expect(address.isValidEncodedAddress(`${encoded} \t `, NetworkType.MIJIN_TEST)).to.equal(false); + expect(address.isValidEncodedAddress(` \t ${encoded} \t `, NetworkType.MIJIN_TEST)).to.equal(false); }); }); @@ -263,7 +260,7 @@ describe('address', () => { // Act: const result = address.addressToString( - address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.MAIN_NET, SignSchema.KECCAK_REVERSED_KEY)); + address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.MAIN_NET)); // Assert: const message = ` from ${publicKeyHex}`; @@ -304,48 +301,7 @@ describe('address', () => { // Act: const result = address.addressToString( - address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.TEST_NET, SignSchema.KECCAK_REVERSED_KEY)); - - // Assert: - const message = ` from ${publicKeyHex}`; - expect(result.toUpperCase(), `public ${message}`).equal(expectedAddress.toUpperCase()); - } - }); - }); - - /** - * @see https://raw.githubusercontent.com/nemtech/test-vectors/master/1.test-address-nis1.json - */ - describe('NIS1 test vector [MIJIN] - PublicKey to Address', () => { - it('can create Address from NIS public Key', () => { - // Arrange: - const Public_Keys = [ - 'c5f54ba980fcbb657dbaaa42700539b207873e134d2375efeab5f1ab52f87844', - '96eb2a145211b1b7ab5f0d4b14f8abc8d695c7aee31a3cfc2d4881313c68eea3', - '2d8425e4ca2d8926346c7a7ca39826acd881a8639e81bd68820409c6e30d142a', - '4feed486777ed38e44c489c7c4e93a830e4c4a907fa19a174e630ef0f6ed0409', - '83ee32e4e145024d29bca54f71fa335a98b3e68283f1a3099c4d4ae113b53e54', - ]; - - const Addresses = [ - 'MDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5LDT7JHT', - 'MABHFGE5ORQD3LE4O6B7JUFN47ECOFBFAQ4XDSJH', - 'MAVOZX4HDVOAR4W6K4WJHWPD3MOFU27DFEVDXFMY', - 'MBZ6JK5YOCU6UPSSZ5D3G27UHAPHTY5HDSWBYUNP', - 'MCQW2P5DNZ5BBXQVGS367DQ4AHC3RXOEVHTXSIR6', - ]; - - // Sanity: - expect(Public_Keys.length).equal(Addresses.length); - - for (let i = 0; i < Public_Keys.length; ++i) { - // Arrange: - const publicKeyHex = Public_Keys[i]; - const expectedAddress = Addresses[i]; - - // Act: - const result = address.addressToString( - address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.MIJIN, SignSchema.KECCAK_REVERSED_KEY)); + address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.TEST_NET)); // Assert: const message = ` from ${publicKeyHex}`; @@ -369,11 +325,11 @@ describe('address', () => { ]; const Addresses = [ - 'NDIPRQMB3HT7A6ZKV7HOHJQM7JHX6H3FN7DBVFHB', - 'NC65QJI4OWTUFJNQ2IDVOMUTE7IDI2EGEFP5QME7', - 'NCBC4VAQBVSB4J5J2PTFM7OUY5CYDL33VW7RKW7K', - 'NBLW3CQPBGPCFAXG4XM5GDEVLPESCPDPFO7NUCOM', - 'NA5RDU36TKBTW4KVSSPD7PT5YTUMD7OIJFMMIEEQ', + 'MDIPRQMB3HT7A6ZKV7HOHJQM7JHX6H3FN5YHHZMD', + 'MC65QJI4OWTUFJNQ2IDVOMUTE7IDI2EGEEZ6ADFH', + 'MCBC4VAQBVSB4J5J2PTFM7OUY5CYDL33VUHV7FNU', + 'MBLW3CQPBGPCFAXG4XM5GDEVLPESCPDPFN4NBABW', + 'MA5RDU36TKBTW4KVSSPD7PT5YTUMD7OIJEMAYYMV', ]; // Sanity: @@ -386,7 +342,7 @@ describe('address', () => { // Act: const result = address.addressToString( - address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.MAIN_NET)); + address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.MIJIN)); // Assert: const message = ` from ${publicKeyHex}`; @@ -410,11 +366,11 @@ describe('address', () => { ]; const Addresses = [ - 'TDIPRQMB3HT7A6ZKV7HOHJQM7JHX6H3FN6Q33DFP', - 'TC65QJI4OWTUFJNQ2IDVOMUTE7IDI2EGEHM5K3P3', - 'TCBC4VAQBVSB4J5J2PTFM7OUY5CYDL33VXFNZO4N', - 'TBLW3CQPBGPCFAXG4XM5GDEVLPESCPDPFOEM2TCJ', - 'TA5RDU36TKBTW4KVSSPD7PT5YTUMD7OIJFLBWZH3', + 'SDIPRQMB3HT7A6ZKV7HOHJQM7JHX6H3FN5EIRD3D', + 'SC65QJI4OWTUFJNQ2IDVOMUTE7IDI2EGEGTDOMI3', + 'SCBC4VAQBVSB4J5J2PTFM7OUY5CYDL33VVLQRCX6', + 'SBLW3CQPBGPCFAXG4XM5GDEVLPESCPDPFNJYN46J', + 'SA5RDU36TKBTW4KVSSPD7PT5YTUMD7OIJGV24AZM', ]; // Sanity: @@ -427,7 +383,7 @@ describe('address', () => { // Act: const result = address.addressToString( - address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.TEST_NET)); + address.publicKeyToAddress(convert.hexToUint8(publicKeyHex), NetworkType.MIJIN_TEST)); // Assert: const message = ` from ${publicKeyHex}`; diff --git a/test/core/utils/TransactionMapping.spec.ts b/test/core/utils/TransactionMapping.spec.ts index ff1d6d7212..c50ebb60fb 100644 --- a/test/core/utils/TransactionMapping.spec.ts +++ b/test/core/utils/TransactionMapping.spec.ts @@ -25,9 +25,9 @@ import { AccountRestrictionType } from '../../../src/model/account/AccountRestri import { Address } from '../../../src/model/account/Address'; import { PublicAccount } from '../../../src/model/account/PublicAccount'; import { NetworkType } from '../../../src/model/blockchain/NetworkType'; +import { MosaicFlags } from '../../../src/model/mosaic/MosaicFlags'; import { MosaicId } from '../../../src/model/mosaic/MosaicId'; import { MosaicNonce } from '../../../src/model/mosaic/MosaicNonce'; -import { MosaicProperties } from '../../../src/model/mosaic/MosaicProperties'; import { MosaicRestrictionType } from '../../../src/model/mosaic/MosaicRestrictionType'; import { MosaicSupplyChangeAction } from '../../../src/model/mosaic/MosaicSupplyChangeAction'; import { NetworkCurrencyMosaic } from '../../../src/model/mosaic/NetworkCurrencyMosaic'; @@ -190,12 +190,9 @@ describe('TransactionMapping - createFromPayload', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); @@ -203,12 +200,12 @@ describe('TransactionMapping - createFromPayload', () => { const transaction = TransactionMapping.createFromPayload(signedTransaction.payload) as MosaicDefinitionTransaction; - expect(transaction.mosaicProperties.duration!.lower).to.be.equal(1000); - expect(transaction.mosaicProperties.duration!.higher).to.be.equal(0); - expect(transaction.mosaicProperties.divisibility).to.be.equal(3); - expect(transaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.restrictable).to.be.equal(false); + expect(transaction.duration!.lower).to.be.equal(1000); + expect(transaction.duration!.higher).to.be.equal(0); + expect(transaction.divisibility).to.be.equal(3); + expect(transaction.flags.supplyMutable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.flags.restrictable).to.be.equal(false); }); @@ -217,12 +214,9 @@ describe('TransactionMapping - createFromPayload', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); @@ -230,10 +224,10 @@ describe('TransactionMapping - createFromPayload', () => { const transaction = TransactionMapping.createFromPayload(signedTransaction.payload) as MosaicDefinitionTransaction; - expect(transaction.mosaicProperties.divisibility).to.be.equal(3); - expect(transaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.restrictable).to.be.equal(false); + expect(transaction.divisibility).to.be.equal(3); + expect(transaction.flags.supplyMutable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.flags.restrictable).to.be.equal(false); }); @@ -242,12 +236,9 @@ describe('TransactionMapping - createFromPayload', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); @@ -255,10 +246,10 @@ describe('TransactionMapping - createFromPayload', () => { const transaction = TransactionMapping.createFromPayload(signedTransaction.payload) as MosaicDefinitionTransaction; - expect(transaction.mosaicProperties.divisibility).to.be.equal(3); - expect(transaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); + expect(transaction.divisibility).to.be.equal(3); + expect(transaction.flags.supplyMutable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); }); @@ -267,12 +258,9 @@ describe('TransactionMapping - createFromPayload', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); @@ -280,10 +268,10 @@ describe('TransactionMapping - createFromPayload', () => { const transaction = TransactionMapping.createFromPayload(signedTransaction.payload) as MosaicDefinitionTransaction; - expect(transaction.mosaicProperties.divisibility).to.be.equal(3); - expect(transaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); + expect(transaction.divisibility).to.be.equal(3); + expect(transaction.flags.supplyMutable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); }); @@ -292,12 +280,9 @@ describe('TransactionMapping - createFromPayload', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); @@ -305,10 +290,10 @@ describe('TransactionMapping - createFromPayload', () => { const transaction = TransactionMapping.createFromPayload(signedTransaction.payload) as MosaicDefinitionTransaction; - expect(transaction.mosaicProperties.divisibility).to.be.equal(3); - expect(transaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); + expect(transaction.divisibility).to.be.equal(3); + expect(transaction.flags.supplyMutable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); }); @@ -839,13 +824,9 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () => Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - restrictable: false, - divisibility: 3, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); @@ -853,10 +834,10 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () => TransactionMapping.createFromDTO(mosaicDefinitionTransaction.toJSON()) as MosaicDefinitionTransaction; expect(transaction.type).to.be.equal(TransactionType.MOSAIC_DEFINITION); - expect(transaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.transferable).to.be.equal(false); - expect(transaction.mosaicProperties.divisibility).to.be.equal(3); + expect(transaction.flags.supplyMutable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.flags.transferable).to.be.equal(false); + expect(transaction.divisibility).to.be.equal(3); }); diff --git a/test/infrastructure/SerializeTransactionToJSON.spec.ts b/test/infrastructure/SerializeTransactionToJSON.spec.ts index 393e64e52c..70fde2bda5 100644 --- a/test/infrastructure/SerializeTransactionToJSON.spec.ts +++ b/test/infrastructure/SerializeTransactionToJSON.spec.ts @@ -23,9 +23,9 @@ import { AccountRestrictionType } from '../../src/model/account/AccountRestricti import { Address } from '../../src/model/account/Address'; import { PublicAccount } from '../../src/model/account/PublicAccount'; import { NetworkType } from '../../src/model/blockchain/NetworkType'; +import { MosaicFlags } from '../../src/model/mosaic/MosaicFlags'; import { MosaicId } from '../../src/model/mosaic/MosaicId'; import { MosaicNonce } from '../../src/model/mosaic/MosaicNonce'; -import { MosaicProperties } from '../../src/model/mosaic/MosaicProperties'; import { MosaicSupplyChangeAction } from '../../src/model/mosaic/MosaicSupplyChangeAction'; import { NetworkCurrencyMosaic } from '../../src/model/mosaic/NetworkCurrencyMosaic'; import { AliasAction } from '../../src/model/namespace/AliasAction'; @@ -174,13 +174,9 @@ describe('SerializeTransactionToJSON', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 5, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, true), + 5, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); @@ -198,12 +194,9 @@ describe('SerializeTransactionToJSON', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(true, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); diff --git a/test/model/account/Account.spec.ts b/test/model/account/Account.spec.ts index d1413d7a3d..d88fb53916 100644 --- a/test/model/account/Account.spec.ts +++ b/test/model/account/Account.spec.ts @@ -15,9 +15,9 @@ */ import {expect} from 'chai'; -import { SignSchema } from '../../../src/core/crypto'; import {Account} from '../../../src/model/account/Account'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import { Convert } from '../../../src/core/format/Convert'; describe('Account', () => { const accountInformation = { @@ -35,13 +35,16 @@ describe('Account', () => { /** * @see https://raw.githubusercontent.com/nemtech/test-vectors/master/1.test-keys-nis1.json + * @see https://raw.githubusercontent.com/nemtech/test-vectors/master/1.test-address-nis1.json */ it('should be created via private key using NIS1 schema', () => { - const account = Account.createFromPrivateKey('575dbb3062267eff57c970a336ebbc8fbcfe12c5bd3ed7bc11eb0481d7704ced', - NetworkType.MIJIN_TEST, SignSchema.KECCAK_REVERSED_KEY); + const privateKey = + Convert.uint8ToHex(Convert.hexToUint8Reverse('575dbb3062267eff57c970a336ebbc8fbcfe12c5bd3ed7bc11eb0481d7704ced')); + const account = Account.createFromPrivateKey(privateKey, + NetworkType.TEST_NET); expect(account.publicKey.toUpperCase()).to.be. equal('c5f54ba980fcbb657dbaaa42700539b207873e134d2375efeab5f1ab52f87844'.toUpperCase()); - expect(account.address.plain()).to.be.equal('SDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5JGDTV3S'); + expect(account.address.plain()).to.be.equal('TDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5KZPFMK2'); }); it('should throw exception when the private key is not valid', () => { @@ -58,7 +61,7 @@ describe('Account', () => { }); it('should generate a new account using NIS1 schema', () => { - const account = Account.generateNewAccount(NetworkType.MIJIN_TEST, SignSchema.KECCAK_REVERSED_KEY); + const account = Account.generateNewAccount(NetworkType.TEST_NET); expect(account.publicKey).to.not.be.equal(undefined); expect(account.privateKey).to.not.be.equal(undefined); expect(account.address).to.not.be.equal(undefined); @@ -90,24 +93,22 @@ describe('Account', () => { it('utf-8 - NIS1', () => { const account = Account.createFromPrivateKey( 'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47', - NetworkType.MIJIN_TEST, - SignSchema.KECCAK_REVERSED_KEY, + NetworkType.TEST_NET, ); const publicAccount = account.publicAccount; - const signed = account.signData('catapult rocks!', SignSchema.KECCAK_REVERSED_KEY); - expect(publicAccount.verifySignature('catapult rocks!', signed, SignSchema.KECCAK_REVERSED_KEY)) + const signed = account.signData('catapult rocks!'); + expect(publicAccount.verifySignature('catapult rocks!', signed)) .to.be.true; }); it('hexa - NIS1', () => { const account = Account.createFromPrivateKey( 'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47', - NetworkType.MIJIN_TEST, - SignSchema.KECCAK_REVERSED_KEY, + NetworkType.TEST_NET, ); const publicAccount = account.publicAccount; - const signed = account.signData('0xAA', SignSchema.KECCAK_REVERSED_KEY); - expect(publicAccount.verifySignature('0xAA', signed, SignSchema.KECCAK_REVERSED_KEY)) + const signed = account.signData('0xAA'); + expect(publicAccount.verifySignature('0xAA', signed)) .to.be.true; }); }); diff --git a/test/model/account/Address.spec.ts b/test/model/account/Address.spec.ts index f2533eaed0..08122676db 100644 --- a/test/model/account/Address.spec.ts +++ b/test/model/account/Address.spec.ts @@ -17,7 +17,6 @@ import {expect} from 'chai'; import {Address} from '../../../src/model/account/Address'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; -import { SignSchema } from '../../../src/core/crypto'; describe('Address', () => { const publicKey = 'c2f93346e27ce6ad1a9f8f5e3066f8326593a406bdf357acb041e2f9ab402efe'.toUpperCase(); @@ -41,30 +40,21 @@ describe('Address', () => { it('createComplete an address given publicKey + NetworkType.MAIN_NET', () => { const address = Address.createFromPublicKey(publicKey, NetworkType.MAIN_NET); - expect(address.plain()).to.be.equal('NCTVW23D2MN5VE4AQ4TZIDZENGNOZXPRPQUJ2ZML'); + expect(address.plain()).not.to.be.equal('NDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5JFXJ4R4'); expect(address.networkType).to.be.equal(NetworkType.MAIN_NET); }); it('createComplete an address given publicKey + NetworkType.TEST_NET', () => { const address = Address.createFromPublicKey(publicKey, NetworkType.TEST_NET); - expect(address.plain()).to.be.equal('TCTVW23D2MN5VE4AQ4TZIDZENGNOZXPRPSDRSFRF'); + expect(address.plain()).not.to.be.equal('TDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5KZPFMK2'); expect(address.networkType).to.be.equal(NetworkType.TEST_NET); }); - /** - * @see https://raw.githubusercontent.com/nemtech/test-vectors/master/1.test-address-nis1.json - */ - it('createComplete an address given publicKey + NetworkType.MIJIN using NIS1 schema', () => { - const address = Address.createFromPublicKey(NIS_PublicKey, NetworkType.MIJIN, SignSchema.KECCAK_REVERSED_KEY); - expect(address.plain()).to.be.equal('MDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5LDT7JHT'); - expect(address.networkType).to.be.equal(NetworkType.MIJIN); - }); - /** * @see https://raw.githubusercontent.com/nemtech/test-vectors/master/1.test-address-nis1.json */ it('createComplete an address given publicKey + NetworkType.MAIN_NET using NIS1 schema', () => { - const address = Address.createFromPublicKey(NIS_PublicKey, NetworkType.MAIN_NET, SignSchema.KECCAK_REVERSED_KEY); + const address = Address.createFromPublicKey(NIS_PublicKey, NetworkType.MAIN_NET); expect(address.plain()).to.be.equal('NDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5JFXJ4R4'); expect(address.networkType).to.be.equal(NetworkType.MAIN_NET); }); @@ -73,7 +63,7 @@ describe('Address', () => { * @see https://raw.githubusercontent.com/nemtech/test-vectors/master/1.test-address-nis1.json */ it('createComplete an address given publicKey + NetworkType.TEST_NET using NIS1 schema', () => { - const address = Address.createFromPublicKey(NIS_PublicKey, NetworkType.TEST_NET, SignSchema.KECCAK_REVERSED_KEY); + const address = Address.createFromPublicKey(NIS_PublicKey, NetworkType.TEST_NET); expect(address.plain()).to.be.equal('TDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5KZPFMK2'); expect(address.networkType).to.be.equal(NetworkType.TEST_NET); }); diff --git a/test/model/account/PublicAccount.spec.ts b/test/model/account/PublicAccount.spec.ts index 5f75d0f345..ce53356177 100644 --- a/test/model/account/PublicAccount.spec.ts +++ b/test/model/account/PublicAccount.spec.ts @@ -15,11 +15,9 @@ */ import { expect } from 'chai'; -import { SignSchema } from '../../../src/core/crypto'; -import { Convert } from '../../../src/core/format/Convert'; +import { Account } from '../../../src/model/account/Account'; import { PublicAccount } from '../../../src/model/account/PublicAccount'; import { NetworkType } from '../../../src/model/blockchain/NetworkType'; -import { Account } from '../../../src/model/account/Account'; describe('PublicAccount', () => { const publicKey = 'b4f12e7c9f6946091e2cb8b6d3a12b50d17ccbbf646386ea27ce2946a7423dcf'; @@ -35,8 +33,8 @@ describe('PublicAccount', () => { */ it('should createComplete a public account from public key using NIS1', () => { const publicAccount = PublicAccount.createFromPublicKey('c5f54ba980fcbb657dbaaa42700539b207873e134d2375efeab5f1ab52f87844', - NetworkType.MIJIN, SignSchema.KECCAK_REVERSED_KEY); - expect(publicAccount.address.plain()).to.be.equal('MDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5LDT7JHT'); + NetworkType.MAIN_NET); + expect(publicAccount.address.plain()).to.be.equal('NDD2CT6LQLIYQ56KIXI3ENTM6EK3D44P5JFXJ4R4'); }); }); @@ -57,12 +55,11 @@ describe('Signature verification', () => { // Arrange:' const account = Account.createFromPrivateKey( 'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47', - NetworkType.MIJIN_TEST, - SignSchema.KECCAK_REVERSED_KEY, + NetworkType.TEST_NET, ); const publicAccount = account.publicAccount; - const signed = account.signData('catapult rocks!', SignSchema.KECCAK_REVERSED_KEY); - expect(publicAccount.verifySignature('catapult rocks!', signed, SignSchema.KECCAK_REVERSED_KEY)) + const signed = account.signData('catapult rocks!'); + expect(publicAccount.verifySignature('catapult rocks!', signed)) .to.be.true; }); diff --git a/test/model/mosaic/MosaicAmountView.spec.ts b/test/model/mosaic/MosaicAmountView.spec.ts index da500af5a2..04544aed14 100644 --- a/test/model/mosaic/MosaicAmountView.spec.ts +++ b/test/model/mosaic/MosaicAmountView.spec.ts @@ -17,9 +17,9 @@ import {expect} from 'chai'; import {PublicAccount} from '../../../src/model/account/PublicAccount'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import {MosaicFlags} from '../../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../../src/model/mosaic/MosaicId'; import {MosaicInfo} from '../../../src/model/mosaic/MosaicInfo'; -import {MosaicProperties} from '../../../src/model/mosaic/MosaicProperties'; import {UInt64} from '../../../src/model/UInt64'; import {MosaicAmountView} from '../../../src/service/MosaicAmountView'; @@ -34,13 +34,9 @@ describe('MosaicAmountView', () => { new UInt64([1, 0]), // height PublicAccount.createFromPublicKey('B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF', NetworkType.MIJIN_TEST), 1, // revision - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, true), + 3, + UInt64.fromUint(1000), ); }); diff --git a/test/model/mosaic/MosaicFlags.spec.ts b/test/model/mosaic/MosaicFlags.spec.ts new file mode 100644 index 0000000000..d8e5f71567 --- /dev/null +++ b/test/model/mosaic/MosaicFlags.spec.ts @@ -0,0 +1,75 @@ +/* + * Copyright 2018 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 {deepEqual} from 'assert'; +import {expect} from 'chai'; +import {MosaicFlags} from '../../../src/model/mosaic/MosaicFlags'; +import {UInt64} from '../../../src/model/UInt64'; + +describe('MosaicFlags', () => { + + it('should createComplete an MosaicFlags object with constructor', () => { + const propertiesDTO = { + flags: 7, + divisibility: 3, + duration: '1000', + }; + + const mosaicFlags = new MosaicFlags(7); + + expect(mosaicFlags.supplyMutable).to.be.equal(true); + expect(mosaicFlags.transferable).to.be.equal(true); + expect(mosaicFlags.restrictable).to.be.equal(true); + }); + + it('should createComplete an mosaicFlags object with static method', () => { + const duration = UInt64.fromUint(1000); + + const mosaicFlags = MosaicFlags.create(false, false, false); + + expect(mosaicFlags.supplyMutable).to.be.equal(false); + expect(mosaicFlags.transferable).to.be.equal(false); + expect(mosaicFlags.restrictable).to.be.equal(false); + }); + + it('should return corredt flags value', () => { + let mosaicFlags = MosaicFlags.create(false, false, false); + expect(mosaicFlags.getValue()).to.be.equal(0); + + mosaicFlags = MosaicFlags.create(true, false, false); + expect(mosaicFlags.getValue()).to.be.equal(1); + + mosaicFlags = MosaicFlags.create(false, true, false); + expect(mosaicFlags.getValue()).to.be.equal(2); + + mosaicFlags = MosaicFlags.create(false, false, true); + expect(mosaicFlags.getValue()).to.be.equal(4); + + mosaicFlags = MosaicFlags.create(true, true, true); + expect(mosaicFlags.getValue()).to.be.equal(7); + + mosaicFlags = MosaicFlags.create(true, false, true); + expect(mosaicFlags.getValue()).to.be.equal(5); + + mosaicFlags = MosaicFlags.create(true, true, false); + expect(mosaicFlags.getValue()).to.be.equal(3); + }); + + it('should return corredt flags json object', () => { + const mosaicFlags = MosaicFlags.create(true, true, true); + expect(mosaicFlags.toDTO().flags).to.be.equal(7); + }); +}); diff --git a/test/model/mosaic/MosaicInfo.spec.ts b/test/model/mosaic/MosaicInfo.spec.ts index 71bb380bc0..10f2069328 100644 --- a/test/model/mosaic/MosaicInfo.spec.ts +++ b/test/model/mosaic/MosaicInfo.spec.ts @@ -18,13 +18,13 @@ import {deepEqual} from 'assert'; import {expect} from 'chai'; import {PublicAccount} from '../../../src/model/account/PublicAccount'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import {MosaicFlags} from '../../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../../src/model/mosaic/MosaicId'; import {MosaicInfo} from '../../../src/model/mosaic/MosaicInfo'; -import {MosaicProperties} from '../../../src/model/mosaic/MosaicProperties'; import {UInt64} from '../../../src/model/UInt64'; describe('MosaicInfo', () => { - let mosaicInfoDTO = { + const mosaicInfoDTO = { meta: { id: '59FDA0733F17CF0001772CBC', }, @@ -53,11 +53,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - new MosaicProperties( - mosaicInfoDTO.mosaic.flags, - mosaicInfoDTO.mosaic.divisibility, - UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - ), + new MosaicFlags(mosaicInfoDTO.mosaic.flags), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); deepEqual(mosaicInfo.id, mosaicInfoDTO.mosaic.mosaicId); @@ -79,10 +77,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - new MosaicProperties( - mosaicInfoDTO.mosaic.flags, - mosaicInfoDTO.mosaic.divisibility, - UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), + new MosaicFlags(mosaicInfoDTO.mosaic.flags), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration, ), ); @@ -105,12 +102,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - MosaicProperties.create({ - supplyMutable: true, - transferable: false, - divisibility: mosaicInfoDTO.mosaic.divisibility, - duration: UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - }), + MosaicFlags.create(true, false, false), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ) ; expect(mosaicInfo.isSupplyMutable()).to.be.equal(true); @@ -123,12 +117,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: mosaicInfoDTO.mosaic.divisibility, - duration: UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - }), + MosaicFlags.create(false, false, false), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); expect(mosaicInfo.isSupplyMutable()).to.be.equal(false); }); @@ -142,12 +133,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - MosaicProperties.create({ - supplyMutable: false, - transferable: true, - divisibility: mosaicInfoDTO.mosaic.divisibility, - duration: UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - }), + MosaicFlags.create(false, true, false), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); expect(mosaicInfo.isTransferable()).to.be.equal(true); }); @@ -159,12 +147,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: mosaicInfoDTO.mosaic.divisibility, - duration: UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - }), + MosaicFlags.create(false, false, false), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); expect(mosaicInfo.isTransferable()).to.be.equal(false); }); @@ -178,13 +163,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: mosaicInfoDTO.mosaic.divisibility, - restrictable: true, - duration: UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - }), + MosaicFlags.create(false, false, true), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); expect(mosaicInfo.isRestrictable()).to.be.equal(true); }); @@ -196,12 +177,9 @@ describe('MosaicInfo', () => { mosaicInfoDTO.mosaic.height, mosaicInfoDTO.mosaic.owner, mosaicInfoDTO.mosaic.revision, - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: mosaicInfoDTO.mosaic.divisibility, - duration: UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - }), + MosaicFlags.create(false, false, false), + mosaicInfoDTO.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), ); expect(mosaicInfo.isRestrictable()).to.be.equal(false); }); diff --git a/test/model/mosaic/MosaicProperties.spec.ts b/test/model/mosaic/MosaicProperties.spec.ts deleted file mode 100644 index 6e93b785c0..0000000000 --- a/test/model/mosaic/MosaicProperties.spec.ts +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2018 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 {deepEqual} from 'assert'; -import {expect} from 'chai'; -import {MosaicProperties} from '../../../src/model/mosaic/MosaicProperties'; -import {UInt64} from '../../../src/model/UInt64'; - -describe('MosaicProperties', () => { - - it('should createComplete an MosaicProperties object with constructor', () => { - const propertiesDTO = { - flags: 7, - divisibility: 3, - duration: '1000', - }; - - const mosaicProperties = new MosaicProperties( - propertiesDTO.flags, - propertiesDTO.divisibility, - UInt64.fromNumericString(propertiesDTO.duration), - ); - - expect(mosaicProperties.divisibility).to.be.equal(propertiesDTO.divisibility); - deepEqual(mosaicProperties.duration.toString(), propertiesDTO.duration); - - expect(mosaicProperties.supplyMutable).to.be.equal(true); - expect(mosaicProperties.transferable).to.be.equal(true); - }); - - it('should createComplete an MosaicProperties object with static method', () => { - const duration = UInt64.fromUint(1000); - - const mosaicProperties = MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 10, - duration, - }); - - expect(mosaicProperties.divisibility).to.be.equal(10); - deepEqual(mosaicProperties.duration, duration); - - expect(mosaicProperties.supplyMutable).to.be.equal(false); - expect(mosaicProperties.transferable).to.be.equal(false); - expect(mosaicProperties.restrictable).to.be.equal(false); - }); - - it('should createComplete an MosaicProperties object without duration', () => { - const mosaicProperties = MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 10, - duration: UInt64.fromUint(0), - }); - - expect(mosaicProperties.divisibility).to.be.equal(10); - deepEqual(mosaicProperties.duration.toDTO(), [0, 0]); - - expect(mosaicProperties.supplyMutable).to.be.equal(false); - expect(mosaicProperties.transferable).to.be.equal(false); - expect(mosaicProperties.restrictable).to.be.equal(false); - }); -}); diff --git a/test/model/mosaic/MosaicView.spec.ts b/test/model/mosaic/MosaicView.spec.ts index 90a010ad22..5d19187a0b 100644 --- a/test/model/mosaic/MosaicView.spec.ts +++ b/test/model/mosaic/MosaicView.spec.ts @@ -17,9 +17,9 @@ import {expect} from 'chai'; import {PublicAccount} from '../../../src/model/account/PublicAccount'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import { MosaicFlags } from '../../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../../src/model/mosaic/MosaicId'; import {MosaicInfo} from '../../../src/model/mosaic/MosaicInfo'; -import {MosaicProperties} from '../../../src/model/mosaic/MosaicProperties'; import {UInt64} from '../../../src/model/UInt64'; import {MosaicView} from '../../../src/service/MosaicView'; @@ -34,13 +34,9 @@ describe('MosaicView', () => { new UInt64([1, 0]), // height PublicAccount.createFromPublicKey('B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF', NetworkType.MIJIN_TEST), 1, // revision - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 2, - restrictable: true, - duration: UInt64.fromUint(1000), - })); + MosaicFlags.create(true, true, true), + 2, + UInt64.fromUint(1000)); }); it('should createComplete a Mosaic View', () => { diff --git a/test/model/receipt/Receipt.spec.ts b/test/model/receipt/Receipt.spec.ts index b17b1ef31c..20f07c685d 100644 --- a/test/model/receipt/Receipt.spec.ts +++ b/test/model/receipt/Receipt.spec.ts @@ -162,7 +162,7 @@ describe('Receipt', () => { it('should createComplete a balance transfer receipt - Mosaic Rental Fee', () => { const receiptDTO = { version: 1, - type: 4941, + type: 4685, senderPublicKey: account.publicKey, recipientAddress: '9103B60AAF2762688300000000000000000000000000000000', mosaicId: '941299B2B7E1291C', diff --git a/test/model/transaction/AggregateTransaction.spec.ts b/test/model/transaction/AggregateTransaction.spec.ts index 6757737c1e..7927419e8b 100644 --- a/test/model/transaction/AggregateTransaction.spec.ts +++ b/test/model/transaction/AggregateTransaction.spec.ts @@ -22,9 +22,9 @@ import {Account} from '../../../src/model/account/Account'; import {Address} from '../../../src/model/account/Address'; import {PublicAccount} from '../../../src/model/account/PublicAccount'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import {MosaicFlags} from '../../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../../src/model/mosaic/MosaicId'; import {MosaicNonce} from '../../../src/model/mosaic/MosaicNonce'; -import {MosaicProperties} from '../../../src/model/mosaic/MosaicProperties'; import {MosaicSupplyChangeAction} from '../../../src/model/mosaic/MosaicSupplyChangeAction'; import { NetworkCurrencyMosaic } from '../../../src/model/mosaic/NetworkCurrencyMosaic'; import {AggregateTransaction} from '../../../src/model/transaction/AggregateTransaction'; @@ -147,13 +147,9 @@ describe('AggregateTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, true), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); diff --git a/test/model/transaction/EncryptedMessage.spec.ts b/test/model/transaction/EncryptedMessage.spec.ts index 2e25fb253d..87975d8cde 100644 --- a/test/model/transaction/EncryptedMessage.spec.ts +++ b/test/model/transaction/EncryptedMessage.spec.ts @@ -15,7 +15,6 @@ */ import {expect} from 'chai'; -import { SignSchema } from '../../../src/core/crypto'; import {Account} from '../../../src/model/account/Account'; import { Deadline, NetworkCurrencyMosaic, NetworkType, TransferTransaction } from '../../../src/model/model'; import {EncryptedMessage} from '../../../src/model/transaction/EncryptedMessage'; @@ -34,9 +33,9 @@ describe('EncryptedMessage', () => { NetworkType.MIJIN_TEST); sender_nis = Account.createFromPrivateKey('2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C108', - NetworkType.MIJIN_TEST, SignSchema.KECCAK_REVERSED_KEY); + NetworkType.TEST_NET); recipient_nis = Account.createFromPrivateKey('B72F2950498111BADF276D6D9D5E345F04E0D5C9B8342DA983C3395B4CF18F08', - NetworkType.MIJIN_TEST, SignSchema.KECCAK_REVERSED_KEY); + NetworkType.TEST_NET); }); it('should create a encrypted message from a DTO', () => { @@ -45,15 +44,15 @@ describe('EncryptedMessage', () => { }); it('should return encrypted message dto', () => { - const encryptedMessage = sender.encryptMessage('test transaction', recipient.publicAccount); - const plainMessage = recipient.decryptMessage(encryptedMessage, sender.publicAccount); + const encryptedMessage = sender.encryptMessage('test transaction', recipient.publicAccount, NetworkType.MIJIN_TEST); + const plainMessage = recipient.decryptMessage(encryptedMessage, sender.publicAccount, NetworkType.MIJIN_TEST); expect(plainMessage.payload).to.be.equal('test transaction'); }); it('should decrypt message from raw encrypted message payload', () => { - const encryptedMessage = sender.encryptMessage('Testing simple transfer', recipient.publicAccount); + const encryptedMessage = sender.encryptMessage('Testing simple transfer', recipient.publicAccount, NetworkType.MIJIN_TEST); const payload = encryptedMessage.payload; - const plainMessage = recipient.decryptMessage(new EncryptedMessage(payload), sender.publicAccount); + const plainMessage = recipient.decryptMessage(new EncryptedMessage(payload), sender.publicAccount, NetworkType.MIJIN_TEST); expect(plainMessage.payload).to.be.equal('Testing simple transfer'); }); @@ -63,20 +62,20 @@ describe('EncryptedMessage', () => { Deadline.create(), recipient.address, [NetworkCurrencyMosaic.createAbsolute(1)], - sender.encryptMessage('Testing simple transfer', recipient.publicAccount), + sender.encryptMessage('Testing simple transfer', recipient.publicAccount, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); const signedTransaction = transferTransaction.signWith(sender, generationHash); const encryptMessage = EncryptedMessage .createFromPayload(signedTransaction.payload.substring(298, signedTransaction.payload.length - 32)); - const plainMessage = recipient.decryptMessage(encryptMessage, sender.publicAccount); + const plainMessage = recipient.decryptMessage(encryptMessage, sender.publicAccount, NetworkType.MIJIN_TEST); expect(plainMessage.payload).to.be.equal('Testing simple transfer'); }); it('should encrypt and decrypt message using NIS1 schema', () => { - const encryptedMessage = sender_nis.encryptMessage('Testing simple transfer', recipient_nis.publicAccount, SignSchema.KECCAK_REVERSED_KEY); + const encryptedMessage = sender_nis.encryptMessage('Testing simple transfer', recipient_nis.publicAccount, NetworkType.TEST_NET); const payload = encryptedMessage.payload; - const plainMessage = recipient_nis.decryptMessage(new EncryptedMessage(payload), sender_nis.publicAccount, SignSchema.KECCAK_REVERSED_KEY); + const plainMessage = recipient_nis.decryptMessage(new EncryptedMessage(payload), sender_nis.publicAccount, NetworkType.TEST_NET); expect(plainMessage.payload).to.be.equal('Testing simple transfer'); }); diff --git a/test/model/transaction/MosaicDefinitionTransaction.spec.ts b/test/model/transaction/MosaicDefinitionTransaction.spec.ts index afa46f015a..fb93b54f56 100644 --- a/test/model/transaction/MosaicDefinitionTransaction.spec.ts +++ b/test/model/transaction/MosaicDefinitionTransaction.spec.ts @@ -17,9 +17,9 @@ import {expect} from 'chai'; import {Account} from '../../../src/model/account/Account'; import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import {MosaicFlags} from '../../../src/model/mosaic/MosaicFlags'; import {MosaicId} from '../../../src/model/mosaic/MosaicId'; import {MosaicNonce} from '../../../src/model/mosaic/MosaicNonce'; -import {MosaicProperties} from '../../../src/model/mosaic/MosaicProperties'; import {Deadline} from '../../../src/model/transaction/Deadline'; import {MosaicDefinitionTransaction} from '../../../src/model/transaction/MosaicDefinitionTransaction'; import {UInt64} from '../../../src/model/UInt64'; @@ -37,13 +37,9 @@ describe('MosaicDefinitionTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, true), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); @@ -56,13 +52,9 @@ describe('MosaicDefinitionTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, true), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, new UInt64([1, 0]), ); @@ -76,22 +68,18 @@ describe('MosaicDefinitionTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - restrictable: true, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, true), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); - expect(mosaicDefinitionTransaction.mosaicProperties.duration!.lower).to.be.equal(1000); - expect(mosaicDefinitionTransaction.mosaicProperties.duration!.higher).to.be.equal(0); - expect(mosaicDefinitionTransaction.mosaicProperties.divisibility).to.be.equal(3); - expect(mosaicDefinitionTransaction.mosaicProperties.supplyMutable).to.be.equal(true); - expect(mosaicDefinitionTransaction.mosaicProperties.transferable).to.be.equal(true); - expect(mosaicDefinitionTransaction.mosaicProperties.restrictable).to.be.equal(true); + expect(mosaicDefinitionTransaction.duration!.lower).to.be.equal(1000); + expect(mosaicDefinitionTransaction.duration!.higher).to.be.equal(0); + expect(mosaicDefinitionTransaction.divisibility).to.be.equal(3); + expect(mosaicDefinitionTransaction.flags.supplyMutable).to.be.equal(true); + expect(mosaicDefinitionTransaction.flags.transferable).to.be.equal(true); + expect(mosaicDefinitionTransaction.flags.restrictable).to.be.equal(true); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); @@ -108,21 +96,18 @@ describe('MosaicDefinitionTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); - expect(mosaicDefinitionTransaction.mosaicProperties.duration!.lower).to.be.equal(1000); - expect(mosaicDefinitionTransaction.mosaicProperties.duration!.higher).to.be.equal(0); - expect(mosaicDefinitionTransaction.mosaicProperties.divisibility).to.be.equal(3); - expect(mosaicDefinitionTransaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(mosaicDefinitionTransaction.mosaicProperties.transferable).to.be.equal(false); - expect(mosaicDefinitionTransaction.mosaicProperties.restrictable).to.be.equal(false); + expect(mosaicDefinitionTransaction.duration!.lower).to.be.equal(1000); + expect(mosaicDefinitionTransaction.duration!.higher).to.be.equal(0); + expect(mosaicDefinitionTransaction.divisibility).to.be.equal(3); + expect(mosaicDefinitionTransaction.flags.supplyMutable).to.be.equal(false); + expect(mosaicDefinitionTransaction.flags.transferable).to.be.equal(false); + expect(mosaicDefinitionTransaction.flags.restrictable).to.be.equal(false); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); @@ -139,12 +124,9 @@ describe('MosaicDefinitionTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: true, - transferable: true, - divisibility: 3, - duration: UInt64.fromUint(1000), - }), + MosaicFlags.create(true, true, false), + 3, + UInt64.fromUint(1000), NetworkType.MIJIN_TEST, ); expect(mosaicDefinitionTransaction.size).to.be.equal(144); @@ -157,19 +139,16 @@ describe('MosaicDefinitionTransaction', () => { Deadline.create(), new MosaicNonce(new Uint8Array([0xE6, 0xDE, 0x84, 0xB8])), // nonce new MosaicId(UInt64.fromUint(1).toDTO()), // ID - MosaicProperties.create({ - supplyMutable: false, - transferable: false, - divisibility: 3, - duration: UInt64.fromUint(0), - }), + MosaicFlags.create(false, false, false), + 3, + UInt64.fromUint(0), NetworkType.MIJIN_TEST, ); - expect(mosaicDefinitionTransaction.mosaicProperties.divisibility).to.be.equal(3); - expect(mosaicDefinitionTransaction.mosaicProperties.supplyMutable).to.be.equal(false); - expect(mosaicDefinitionTransaction.mosaicProperties.transferable).to.be.equal(false); - expect(mosaicDefinitionTransaction.mosaicProperties.restrictable).to.be.equal(false); + expect(mosaicDefinitionTransaction.divisibility).to.be.equal(3); + expect(mosaicDefinitionTransaction.flags.supplyMutable).to.be.equal(false); + expect(mosaicDefinitionTransaction.flags.transferable).to.be.equal(false); + expect(mosaicDefinitionTransaction.flags.restrictable).to.be.equal(false); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash);