Skip to content

Commit 3c87571

Browse files
authored
LockHashAlgorithm (#522)
* Fixed #518 - Added LockHashUtils - Removed Op_Keccak_256 algorithm - Renamed HashType to LockHashAlgorithm - Renamed model field name to be aligned with Catbuffer * Fixed #521 * - Added Hash method with algorithm input - Added unit tests - Added export
1 parent b667bce commit 3c87571

File tree

16 files changed

+363
-319
lines changed

16 files changed

+363
-319
lines changed

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 30 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { CosignatureSignedTransaction } from '../../src/model/transaction/Cosign
5454
import { CosignatureTransaction } from '../../src/model/transaction/CosignatureTransaction';
5555
import { Deadline } from '../../src/model/transaction/Deadline';
5656
import { HashLockTransaction } from '../../src/model/transaction/HashLockTransaction';
57-
import { HashType } from '../../src/model/transaction/HashType';
57+
import { LockHashAlgorithm } from '../../src/model/transaction/LockHashAlgorithm';
5858
import { LinkAction } from '../../src/model/transaction/LinkAction';
5959
import { LockFundsTransaction } from '../../src/model/transaction/LockFundsTransaction';
6060
import { MosaicAddressRestrictionTransaction } from '../../src/model/transaction/MosaicAddressRestrictionTransaction';
@@ -71,6 +71,8 @@ import { TransactionType } from '../../src/model/transaction/TransactionType';
7171
import { TransferTransaction } from '../../src/model/transaction/TransferTransaction';
7272
import { UInt64 } from '../../src/model/UInt64';
7373
import { IntegrationTestHelper } from './IntegrationTestHelper';
74+
import { NetworkCurrencyPublic } from '../../src/model/mosaic/NetworkCurrencyPublic';
75+
import { LockHashUtils } from '../../src/core/utils/LockHashUtils';
7476

7577
describe('TransactionHttp', () => {
7678
let transactionHash;
@@ -873,7 +875,7 @@ describe('TransactionHttp', () => {
873875
Deadline.create(),
874876
NetworkCurrencyLocal.createAbsolute(10),
875877
UInt64.fromUint(100),
876-
HashType.Op_Sha3_256,
878+
LockHashAlgorithm.Op_Sha3_256,
877879
sha3_256.create().update(Crypto.randomBytes(20)).hex(),
878880
account2.address,
879881
networkType, helper.maxFee,
@@ -882,7 +884,7 @@ describe('TransactionHttp', () => {
882884
return helper.announce(signedTransaction).then((transaction: SecretLockTransaction) => {
883885
expect(transaction.mosaic, 'Mosaic').not.to.be.undefined;
884886
expect(transaction.duration, 'Duration').not.to.be.undefined;
885-
expect(transaction.hashType, 'HashType').not.to.be.undefined;
887+
expect(transaction.hashAlgorithm, 'HashAlgorithm').not.to.be.undefined;
886888
expect(transaction.secret, 'Secret').not.to.be.undefined;
887889
expect(transaction.recipientAddress, 'RecipientAddress').not.to.be.undefined;
888890
});
@@ -895,7 +897,7 @@ describe('TransactionHttp', () => {
895897
Deadline.create(),
896898
NetworkCurrencyLocal.createAbsolute(10),
897899
UInt64.fromUint(100),
898-
HashType.Op_Sha3_256,
900+
LockHashAlgorithm.Op_Sha3_256,
899901
sha3_256.create().update(Crypto.randomBytes(20)).hex(),
900902
account2.address,
901903
networkType, helper.maxFee,
@@ -907,40 +909,7 @@ describe('TransactionHttp', () => {
907909
return helper.announce(aggregateSecretLockTransaction.signWith(account, generationHash));
908910
});
909911
});
910-
describe('HashType: Keccak_256', () => {
911912

912-
it('standalone', () => {
913-
const secretLockTransaction = SecretLockTransaction.create(
914-
Deadline.create(),
915-
NetworkCurrencyLocal.createAbsolute(10),
916-
UInt64.fromUint(100),
917-
HashType.Op_Keccak_256,
918-
sha3_256.create().update(Crypto.randomBytes(20)).hex(),
919-
account2.address,
920-
networkType, helper.maxFee,
921-
);
922-
return helper.announce(secretLockTransaction.signWith(account, generationHash));
923-
});
924-
});
925-
describe('HashType: Keccak_256', () => {
926-
927-
it('aggregate', () => {
928-
const secretLockTransaction = SecretLockTransaction.create(
929-
Deadline.create(),
930-
NetworkCurrencyLocal.createAbsolute(10),
931-
UInt64.fromUint(100),
932-
HashType.Op_Keccak_256,
933-
sha3_256.create().update(Crypto.randomBytes(20)).hex(),
934-
account2.address,
935-
networkType, helper.maxFee,
936-
);
937-
const aggregateSecretLockTransaction = AggregateTransaction.createComplete(Deadline.create(),
938-
[secretLockTransaction.toAggregate(account.publicAccount)],
939-
networkType,
940-
[], helper.maxFee);
941-
return helper.announce(aggregateSecretLockTransaction.signWith(account, generationHash));
942-
});
943-
});
944913
describe('HashType: Op_Hash_160', () => {
945914

946915
it('standalone', () => {
@@ -950,7 +919,7 @@ describe('TransactionHttp', () => {
950919
Deadline.create(),
951920
NetworkCurrencyLocal.createAbsolute(10),
952921
UInt64.fromUint(100),
953-
HashType.Op_Hash_160,
922+
LockHashAlgorithm.Op_Hash_160,
954923
secret,
955924
account2.address,
956925
networkType, helper.maxFee,
@@ -967,7 +936,7 @@ describe('TransactionHttp', () => {
967936
Deadline.create(),
968937
NetworkCurrencyLocal.createAbsolute(10),
969938
UInt64.fromUint(100),
970-
HashType.Op_Hash_160,
939+
LockHashAlgorithm.Op_Hash_160,
971940
secret,
972941
account2.address,
973942
networkType, helper.maxFee,
@@ -986,7 +955,7 @@ describe('TransactionHttp', () => {
986955
Deadline.create(),
987956
NetworkCurrencyLocal.createAbsolute(10),
988957
UInt64.fromUint(100),
989-
HashType.Op_Hash_256,
958+
LockHashAlgorithm.Op_Hash_256,
990959
sha3_256.create().update(Crypto.randomBytes(20)).hex(),
991960
account2.address,
992961
networkType, helper.maxFee,
@@ -1001,7 +970,7 @@ describe('TransactionHttp', () => {
1001970
Deadline.create(),
1002971
NetworkCurrencyLocal.createAbsolute(10),
1003972
UInt64.fromUint(100),
1004-
HashType.Op_Hash_256,
973+
LockHashAlgorithm.Op_Hash_256,
1005974
sha3_256.create().update(Crypto.randomBytes(20)).hex(),
1006975
account2.address,
1007976
networkType, helper.maxFee,
@@ -1018,14 +987,14 @@ describe('TransactionHttp', () => {
1018987

1019988
it('standalone', () => {
1020989
const secretSeed = Crypto.randomBytes(20);
1021-
const secret = sha3_256.create().update(secretSeed).hex();
990+
const secret = LockHashUtils.Op_Sha3_256(secretSeed);
1022991
const proof = convert.uint8ToHex(secretSeed);
1023992

1024993
const secretLockTransaction = SecretLockTransaction.create(
1025994
Deadline.create(1, ChronoUnit.HOURS),
1026-
NetworkCurrencyLocal.createAbsolute(10),
995+
NetworkCurrencyPublic.createAbsolute(10),
1027996
UInt64.fromUint(11),
1028-
HashType.Op_Sha3_256,
997+
LockHashAlgorithm.Op_Sha3_256,
1029998
secret,
1030999
account2.address,
10311000
networkType, helper.maxFee,
@@ -1036,7 +1005,7 @@ describe('TransactionHttp', () => {
10361005
return helper.announce(signedSecretLockTx).then(() => {
10371006
const secretProofTransaction = SecretProofTransaction.create(
10381007
Deadline.create(),
1039-
HashType.Op_Sha3_256,
1008+
LockHashAlgorithm.Op_Sha3_256,
10401009
secret,
10411010
account2.address,
10421011
proof,
@@ -1046,7 +1015,7 @@ describe('TransactionHttp', () => {
10461015
return helper.announce(signedTx).then((transaction: SecretProofTransaction) => {
10471016
expect(transaction.secret, 'Secret').not.to.be.undefined;
10481017
expect(transaction.recipientAddress, 'RecipientAddress').not.to.be.undefined;
1049-
expect(transaction.hashType, 'HashType').not.to.be.undefined;
1018+
expect(transaction.hashAlgorithm, 'HashAlgorithm').not.to.be.undefined;
10501019
expect(transaction.proof, 'Proof').not.to.be.undefined;
10511020
});
10521021
});
@@ -1063,7 +1032,7 @@ describe('TransactionHttp', () => {
10631032
Deadline.create(),
10641033
NetworkCurrencyLocal.createAbsolute(10),
10651034
UInt64.fromUint(100),
1066-
HashType.Op_Sha3_256,
1035+
LockHashAlgorithm.Op_Sha3_256,
10671036
secret,
10681037
account2.address,
10691038
networkType, helper.maxFee,
@@ -1072,7 +1041,7 @@ describe('TransactionHttp', () => {
10721041
return helper.announce(secretLockTransaction.signWith(account, generationHash)).then(() => {
10731042
const secretProofTransaction = SecretProofTransaction.create(
10741043
Deadline.create(),
1075-
HashType.Op_Sha3_256,
1044+
LockHashAlgorithm.Op_Sha3_256,
10761045
secret,
10771046
account2.address,
10781047
proof,
@@ -1086,80 +1055,18 @@ describe('TransactionHttp', () => {
10861055

10871056
});
10881057
});
1089-
describe('SecretProofTransaction - HashType: Op_Keccak_256', () => {
1090-
1091-
it('standalone', () => {
1092-
const secretSeed = Crypto.randomBytes(20);
1093-
const secret = keccak_256.create().update(secretSeed).hex();
1094-
const proof = convert.uint8ToHex(secretSeed);
1095-
const secretLockTransaction = SecretLockTransaction.create(
1096-
Deadline.create(),
1097-
NetworkCurrencyLocal.createAbsolute(10),
1098-
UInt64.fromUint(100),
1099-
HashType.Op_Keccak_256,
1100-
secret,
1101-
account2.address,
1102-
networkType, helper.maxFee,
1103-
);
1104-
1105-
return helper.announce(secretLockTransaction.signWith(account, generationHash)).then(() => {
1106-
const secretProofTransaction = SecretProofTransaction.create(
1107-
Deadline.create(),
1108-
HashType.Op_Keccak_256,
1109-
secret,
1110-
account2.address,
1111-
proof,
1112-
networkType, helper.maxFee,
1113-
);
1114-
return helper.announce(secretProofTransaction.signWith(account2, generationHash));
1115-
});
1116-
});
1117-
});
1118-
describe('SecretProofTransaction - HashType: Op_Keccak_256', () => {
1119-
1120-
it('aggregate', () => {
1121-
const secretSeed = Crypto.randomBytes(20);
1122-
const secret = keccak_256.create().update(secretSeed).hex();
1123-
const proof = convert.uint8ToHex(secretSeed);
1124-
const secretLockTransaction = SecretLockTransaction.create(
1125-
Deadline.create(),
1126-
NetworkCurrencyLocal.createAbsolute(10),
1127-
UInt64.fromUint(100),
1128-
HashType.Op_Keccak_256,
1129-
secret,
1130-
account2.address,
1131-
networkType, helper.maxFee,
1132-
);
1133-
return helper.announce(secretLockTransaction.signWith(account, generationHash)).then(() => {
1134-
const secretProofTransaction = SecretProofTransaction.create(
1135-
Deadline.create(),
1136-
HashType.Op_Keccak_256,
1137-
secret,
1138-
account2.address,
1139-
proof,
1140-
networkType, helper.maxFee,
1141-
);
1142-
const aggregateSecretProofTransaction = AggregateTransaction.createComplete(Deadline.create(),
1143-
[secretProofTransaction.toAggregate(account2.publicAccount)],
1144-
networkType,
1145-
[], helper.maxFee);
1146-
return helper.announce(aggregateSecretProofTransaction.signWith(account2, generationHash));
1147-
});
1148-
});
1149-
});
11501058
describe('SecretProofTransaction - HashType: Op_Hash_160', () => {
11511059

11521060
it('standalone', () => {
11531061
const randomBytes = secureRandom.randomBuffer(32);
11541062
const secretSeed = randomBytes.toString('hex');
1155-
const hash = sha256(Buffer.from(secretSeed, 'hex'));
1156-
const secret = new ripemd160().update(Buffer.from(hash, 'hex')).digest('hex');
1063+
const secret = LockHashUtils.Op_Hash_160(randomBytes);
11571064
const proof = secretSeed;
11581065
const secretLockTransaction = SecretLockTransaction.create(
11591066
Deadline.create(),
1160-
NetworkCurrencyLocal.createAbsolute(10),
1067+
NetworkCurrencyPublic.createAbsolute(10),
11611068
UInt64.fromUint(100),
1162-
HashType.Op_Hash_160,
1069+
LockHashAlgorithm.Op_Hash_160,
11631070
secret,
11641071
account2.address,
11651072
networkType, helper.maxFee,
@@ -1168,7 +1075,7 @@ describe('TransactionHttp', () => {
11681075
return helper.announce(secretLockTransaction.signWith(account, generationHash)).then(() => {
11691076
const secretProofTransaction = SecretProofTransaction.create(
11701077
Deadline.create(),
1171-
HashType.Op_Hash_160,
1078+
LockHashAlgorithm.Op_Hash_160,
11721079
secret,
11731080
account2.address,
11741081
proof,
@@ -1191,14 +1098,14 @@ describe('TransactionHttp', () => {
11911098
Deadline.create(),
11921099
NetworkCurrencyLocal.createAbsolute(10),
11931100
UInt64.fromUint(100),
1194-
HashType.Op_Hash_160,
1101+
LockHashAlgorithm.Op_Hash_160,
11951102
secret,
11961103
account2.address,
11971104
networkType, helper.maxFee,
11981105
);
11991106
const secretProofTransaction = SecretProofTransaction.create(
12001107
Deadline.create(),
1201-
HashType.Op_Hash_160,
1108+
LockHashAlgorithm.Op_Hash_160,
12021109
secret,
12031110
account2.address,
12041111
proof,
@@ -1219,21 +1126,22 @@ describe('TransactionHttp', () => {
12191126
const randomBytes = secureRandom.randomBuffer(32);
12201127
const secretSeed = randomBytes.toString('hex');
12211128
const hash = sha256(Buffer.from(secretSeed, 'hex'));
1222-
const secret = sha256(Buffer.from(hash, 'hex'));
1129+
// const secret = sha256(Buffer.from(hash, 'hex'));
1130+
const secret = LockHashUtils.Op_Hash_256(randomBytes);
12231131
const proof = secretSeed;
12241132
const secretLockTransaction = SecretLockTransaction.create(
12251133
Deadline.create(),
1226-
NetworkCurrencyLocal.createAbsolute(10),
1134+
NetworkCurrencyPublic.createAbsolute(1),
12271135
UInt64.fromUint(100),
1228-
HashType.Op_Hash_256,
1136+
LockHashAlgorithm.Op_Hash_256,
12291137
secret,
12301138
account2.address,
12311139
networkType, helper.maxFee,
12321140
);
12331141

12341142
const secretProofTransaction = SecretProofTransaction.create(
12351143
Deadline.create(),
1236-
HashType.Op_Hash_256,
1144+
LockHashAlgorithm.Op_Hash_256,
12371145
secret,
12381146
account2.address,
12391147
proof,
@@ -1256,14 +1164,14 @@ describe('TransactionHttp', () => {
12561164
Deadline.create(),
12571165
NetworkCurrencyLocal.createAbsolute(10),
12581166
UInt64.fromUint(100),
1259-
HashType.Op_Hash_256,
1167+
LockHashAlgorithm.Op_Hash_256,
12601168
secret,
12611169
account2.address,
12621170
networkType, helper.maxFee,
12631171
);
12641172
const secretProofTransaction = SecretProofTransaction.create(
12651173
Deadline.create(),
1266-
HashType.Op_Hash_256,
1174+
LockHashAlgorithm.Op_Hash_256,
12671175
secret,
12681176
account2.address,
12691177
proof,

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"coveralls": "^3.0.9",
4949
"gulp": "^4.0.2",
5050
"gulp-typescript": "^5.0.1",
51+
"kind-of": ">=6.0.3",
5152
"mocha": "^4.0.1",
5253
"nyc": "^15.0.0",
5354
"secure-random": "^1.1.1",

0 commit comments

Comments
 (0)