Skip to content

Commit 0e584cd

Browse files
committed
Updated tests
Added xor method Fixed hex case issues
1 parent 2c7b644 commit 0e584cd

File tree

6 files changed

+88
-25
lines changed

6 files changed

+88
-25
lines changed

e2e/service/MetadataTransactionService.spec.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
2+
import { Convert } from '../../src/core/format/Convert';
23
import { Listener } from '../../src/infrastructure/Listener';
34
import { MetadataHttp } from '../../src/infrastructure/MetadataHttp';
45
import { TransactionHttp } from '../../src/infrastructure/TransactionHttp';
@@ -225,14 +226,14 @@ describe('MetadataTransactionService', () => {
225226
MetadataType.Mosaic,
226227
targetAccount.publicAccount,
227228
key.toHex(),
228-
newValue + '1',
229+
newValue + 'delta',
229230
targetAccount.publicAccount,
230231
mosaicId,
231232
).subscribe((transaction: MosaicMetadataTransaction) => {
232233
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_METADATA_TRANSACTION);
233234
expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key.toHex());
234-
expect(transaction.valueSizeDelta).to.be.equal(1);
235-
expect(transaction.value).to.be.equal(newValue + '1');
235+
expect(transaction.valueSizeDelta).to.be.equal(5);
236+
expect(transaction.value).to.be.equal(newValue + 'delta');
236237
expect(transaction.targetPublicKey).to.be.equal(targetAccount.publicKey);
237238
expect(transaction.targetMosaicId.toHex()).to.be.equal(mosaicId.toHex());
238239
done();
@@ -247,18 +248,59 @@ describe('MetadataTransactionService', () => {
247248
MetadataType.Namespace,
248249
targetAccount.publicAccount,
249250
key.toHex(),
250-
newValue + '1',
251+
newValue + 'delta',
251252
targetAccount.publicAccount,
252253
namespaceId,
253254
).subscribe((transaction: NamespaceMetadataTransaction) => {
254255
expect(transaction.type).to.be.equal(TransactionType.NAMESPACE_METADATA_TRANSACTION);
255256
expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key.toHex());
256-
expect(transaction.valueSizeDelta).to.be.equal(1);
257-
expect(transaction.value).to.be.equal(newValue + '1');
257+
expect(transaction.valueSizeDelta).to.be.equal(5);
258+
expect(transaction.value).to.be.equal(newValue + 'delta');
258259
expect(transaction.targetPublicKey).to.be.equal(targetAccount.publicKey);
259260
expect(transaction.targetNamespaceId.toHex()).to.be.equal(namespaceId.toHex());
260261
done();
261262
});
262263
});
263264
});
265+
266+
describe('Announce transaction through service', () => {
267+
let listener: Listener;
268+
before (() => {
269+
listener = new Listener(config.apiUrl);
270+
return listener.open();
271+
});
272+
after(() => {
273+
return listener.close();
274+
});
275+
it('should create MosaicMetadataTransaction and announce', (done) => {
276+
const metaDataService = new MetadataTransactionService(metadataHttp);
277+
278+
return metaDataService.createMetadataTransaction(
279+
deadline,
280+
NetworkType.MIJIN_TEST,
281+
MetadataType.Mosaic,
282+
targetAccount.publicAccount,
283+
key.toHex(),
284+
newValue + 'delta',
285+
targetAccount.publicAccount,
286+
mosaicId,
287+
).subscribe((transaction: MosaicMetadataTransaction) => {
288+
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
289+
[transaction.toAggregate(targetAccount.publicAccount)],
290+
NetworkType.MIJIN_TEST,
291+
[],
292+
);
293+
const signedTransaction = aggregateTransaction.signWith(targetAccount, generationHash);
294+
listener.confirmed(targetAccount.address).subscribe(() => {
295+
done();
296+
});
297+
listener.status(targetAccount.address).subscribe((error) => {
298+
console.log('Error:', error);
299+
assert(false);
300+
done();
301+
});
302+
transactionHttp.announce(signedTransaction);
303+
});
304+
});
305+
});
264306
});

src/core/crypto/Crypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export class Crypto {
325325
const salt = new Uint8Array(binPayload.buffer, 0, 32);
326326
const iv = new Uint8Array(binPayload.buffer, 32, 16);
327327
const decoded = Crypto._decode(recipientPrivate, senderPublic, payloadBuffer, iv, salt, signSchema);
328-
return decoded;
328+
return decoded.toUpperCase();
329329
}
330330

331331
/**

src/core/format/Convert.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class Convert {
171171
for (let i = 0; i < rawString.length; i++) {
172172
result += rawString.charCodeAt(i).toString(16).padStart(2, '0');
173173
}
174-
return result;
174+
return result.toUpperCase();
175175
}
176176

177177
/**
@@ -211,4 +211,24 @@ export class Convert {
211211
return str;
212212
}
213213
}
214+
215+
/**
216+
* Generate xor for two byte arrays and return in hex string
217+
* @param value1 - Value 1 bytes
218+
* @param value2 - Value 2 bytes
219+
* @return {string} - delta value in Hex
220+
*/
221+
public static xor(value1: Uint8Array, value2: Uint8Array): string {
222+
const buffer1 = Buffer.from(value1.buffer as ArrayBuffer);
223+
const buffer2 = Buffer.from(value2.buffer as ArrayBuffer);
224+
const length = Math.max(buffer1.length, buffer2.length);
225+
const delta: number[] = [];
226+
for (let i = 0; i < length; ++i) {
227+
const xorBuffer = buffer1[i] ^ buffer2[i];
228+
if (xorBuffer !== 0) {
229+
delta.push(xorBuffer);
230+
}
231+
}
232+
return Convert.uint8ToHex(Uint8Array.from(delta));
233+
}
214234
}

src/service/MetadataTransactionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { catchError, map } from 'rxjs/operators';
1919
import { Convert } from '../core/format/Convert';
2020
import { MetadataHttp } from '../infrastructure/MetadataHttp';
2121
import { Address } from '../model/account/Address';
22+
import { PublicAccount } from '../model/account/PublicAccount';
2223
import { NetworkType } from '../model/blockchain/NetworkType';
2324
import { Metadata } from '../model/metadata/Metadata';
2425
import { MetadataType } from '../model/metadata/MetadataType';
@@ -30,7 +31,6 @@ import { MosaicMetadataTransaction } from '../model/transaction/MosaicMetadataTr
3031
import { NamespaceMetadataTransaction } from '../model/transaction/NamespaceMetadataTransaction';
3132
import { Transaction } from '../model/transaction/Transaction';
3233
import { UInt64 } from '../model/UInt64';
33-
import { PublicAccount } from '../model/account/PublicAccount';
3434

3535
/**
3636
* MetadataTransaction service

test/core/format/Convert.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,23 @@ describe('convert', () => {
223223
const actual = convert.utf8ToHex('test words |@#¢∞¬÷“”≠[]}{–');
224224

225225
// Assert:
226-
expect(actual).to.equal('7465737420776f726473207c4023c2a2e2889ec2acc3b7e2809ce2809de289a05b5d7d7be28093');
226+
expect(actual).to.equal('7465737420776F726473207C4023C2A2E2889EC2ACC3B7E2809CE2809DE289A05B5D7D7BE28093');
227227
});
228228

229229
it('utf8 text to hex', () => {
230230
// Act:
231231
const actual = convert.utf8ToHex('先秦兩漢');
232232

233233
// Assert:
234-
expect(actual).to.equal('e58588e7a7a6e585a9e6bca2');
234+
expect(actual).to.equal('E58588E7A7A6E585A9E6BCA2');
235235
});
236236

237237
it('utf8 text to hex with control char', () => {
238238
// Act:
239239
const actual = convert.utf8ToHex(String.fromCodePoint(0x0f) + ' Hello World!');
240240

241241
// Assert:
242-
expect(actual).to.equal('0f2048656c6c6f20576f726c6421');
242+
expect(actual).to.equal('0F2048656C6C6F20576F726C6421');
243243
});
244244
});
245245

test/service/MetadataTransactionservice.spec.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('MetadataTransactionService', () => {
3939
let metadataTransactionService: MetadataTransactionService;
4040
const key = '85BBEA6CC462B244';
4141
const value = 'TEST';
42+
const deltaValue = 'dalta';
4243
const targetIdHex = '941299B2B7E1291C';
4344

4445
before(() => {
@@ -64,13 +65,13 @@ describe('MetadataTransactionService', () => {
6465
MetadataType.Account,
6566
account.publicAccount,
6667
key,
67-
value + '1',
68+
value + deltaValue,
6869
account.publicAccount)
6970
.subscribe((transaction: AccountMetadataTransaction) => {
7071
expect(transaction.type).to.be.equal(TransactionType.ACCOUNT_METADATA_TRANSACTION);
7172
expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key);
72-
expect(transaction.value).to.be.equal(value + '1');
73-
expect(transaction.valueSizeDelta).to.be.equal(1);
73+
expect(transaction.value).to.be.equal(value + deltaValue);
74+
expect(transaction.valueSizeDelta).to.be.equal(deltaValue.length);
7475
expect(transaction.targetPublicKey).to.be.equal(account.publicKey);
7576
done();
7677
});
@@ -82,15 +83,15 @@ describe('MetadataTransactionService', () => {
8283
MetadataType.Mosaic,
8384
account.publicAccount,
8485
key,
85-
value + '1',
86+
value + deltaValue,
8687
account.publicAccount,
8788
new MosaicId(targetIdHex))
8889
.subscribe((transaction: MosaicMetadataTransaction) => {
8990
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_METADATA_TRANSACTION);
9091
expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key);
91-
expect(transaction.value).to.be.equal(value + '1');
92+
expect(transaction.value).to.be.equal(value + deltaValue);
9293
expect(transaction.targetMosaicId.toHex()).to.be.equal(targetIdHex);
93-
expect(transaction.valueSizeDelta).to.be.equal(1);
94+
expect(transaction.valueSizeDelta).to.be.equal(deltaValue.length);
9495
expect(transaction.targetPublicKey).to.be.equal(account.publicKey);
9596
done();
9697
});
@@ -102,15 +103,15 @@ describe('MetadataTransactionService', () => {
102103
MetadataType.Namespace,
103104
account.publicAccount,
104105
key,
105-
value + '1',
106+
value + deltaValue,
106107
account.publicAccount,
107108
NamespaceId.createFromEncoded(targetIdHex))
108109
.subscribe((transaction: NamespaceMetadataTransaction) => {
109110
expect(transaction.type).to.be.equal(TransactionType.NAMESPACE_METADATA_TRANSACTION);
110111
expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key);
111-
expect(transaction.value).to.be.equal(value + '1');
112+
expect(transaction.value).to.be.equal(value + deltaValue);
112113
expect(transaction.targetNamespaceId.toHex()).to.be.equal(targetIdHex);
113-
expect(transaction.valueSizeDelta).to.be.equal(1);
114+
expect(transaction.valueSizeDelta).to.be.equal(deltaValue.length);
114115
expect(transaction.targetPublicKey).to.be.equal(account.publicKey);
115116
done();
116117
});
@@ -123,7 +124,7 @@ describe('MetadataTransactionService', () => {
123124
99,
124125
account.publicAccount,
125126
key,
126-
value + '1',
127+
value + deltaValue,
127128
account.publicAccount);
128129
}).to.throw(Error, 'Metadata type invalid');
129130
});
@@ -135,7 +136,7 @@ describe('MetadataTransactionService', () => {
135136
MetadataType.Mosaic,
136137
account.publicAccount,
137138
key,
138-
value + '1',
139+
value + deltaValue,
139140
account.publicAccount);
140141
}).to.throw(Error, 'TargetId for MosaicMetadataTransaction is invalid');
141142
});
@@ -147,7 +148,7 @@ describe('MetadataTransactionService', () => {
147148
MetadataType.Namespace,
148149
account.publicAccount,
149150
key,
150-
value + '1',
151+
value + deltaValue,
151152
account.publicAccount);
152153
}).to.throw(Error, 'TargetId for NamespaceMetadataTransaction is invalid');
153154
});

0 commit comments

Comments
 (0)