Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions e2e/infrastructure/MosaicHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 12 additions & 19 deletions e2e/infrastructure/TransactionHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand All @@ -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(),
Expand Down
29 changes: 15 additions & 14 deletions src/core/crypto/Crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
36 changes: 12 additions & 24 deletions src/core/crypto/KeyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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);
}
}
27 changes: 21 additions & 6 deletions src/core/crypto/SHA3Hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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));
Expand All @@ -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: () => {
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/core/crypto/SignSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
1 change: 1 addition & 0 deletions src/core/crypto/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading