Skip to content

Commit f97a24b

Browse files
committed
JAV-3 [Github #242] Changed metadata value to string format
1 parent 9a128e1 commit f97a24b

File tree

11 files changed

+133
-59
lines changed

11 files changed

+133
-59
lines changed

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ describe('TransactionHttp', () => {
219219
Deadline.create(),
220220
account.publicKey,
221221
UInt64.fromUint(5),
222-
1,
223-
new Uint8Array(11),
222+
10,
223+
Convert.uint8ToUtf8(new Uint8Array(10)),
224224
NetworkType.MIJIN_TEST,
225225
);
226226

@@ -264,7 +264,7 @@ describe('TransactionHttp', () => {
264264
UInt64.fromUint(5),
265265
mosaicId,
266266
10,
267-
new Uint8Array(10),
267+
Convert.uint8ToUtf8(new Uint8Array(10)),
268268
NetworkType.MIJIN_TEST,
269269
);
270270

@@ -376,7 +376,7 @@ describe('TransactionHttp', () => {
376376
UInt64.fromUint(5),
377377
namespaceId,
378378
10,
379-
new Uint8Array(10),
379+
Convert.uint8ToUtf8(new Uint8Array(10)),
380380
NetworkType.MIJIN_TEST,
381381
);
382382

src/core/format/Convert.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ export class Convert {
174174
return result;
175175
}
176176

177+
/**
178+
* Convert UTF-8 string to Uint8Array
179+
* @param {string} input - An string with UTF-8 encoding
180+
* @return {Uint8Array}
181+
*/
182+
public static utf8ToUint8 = (input: string): Uint8Array => {
183+
const hex = Convert.utf8ToHex(Convert.rstr2utf8(input));
184+
return Convert.hexToUint8(hex);
185+
}
186+
187+
/**
188+
* Convert Uint8Array to string with UTF-8 encoding
189+
* @param {Uint8Array} input - An UTF-8 string
190+
* @return {string}
191+
*/
192+
public static uint8ToUtf8 = (input: Uint8Array): string => {
193+
// return new TextDecoder().decode(input);
194+
const hex = Convert.uint8ToHex(input);
195+
return Convert.decodeHex(hex);
196+
}
197+
177198
/**
178199
* @internal decode hex to uft8 string
179200
* @param hex - Hex input

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
385385
transactionDTO.targetPublicKey,
386386
UInt64.fromNumericString(transactionDTO.scopedMetadataKey),
387387
transactionDTO.valueSizeDelta,
388-
convert.hexToUint8(transactionDTO.value),
388+
convert.decodeHex(transactionDTO.value),
389389
transactionDTO.signature,
390390
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,
391391
extractNetworkType(transactionDTO.version)) : undefined,
@@ -401,7 +401,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
401401
UInt64.fromNumericString(transactionDTO.scopedMetadataKey),
402402
new MosaicId(transactionDTO.targetMosaicId),
403403
transactionDTO.valueSizeDelta,
404-
convert.hexToUint8(transactionDTO.value),
404+
convert.decodeHex(transactionDTO.value),
405405
transactionDTO.signature,
406406
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,
407407
extractNetworkType(transactionDTO.version)) : undefined,
@@ -417,7 +417,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
417417
UInt64.fromNumericString(transactionDTO.scopedMetadataKey),
418418
NamespaceId.createFromEncoded(transactionDTO.targetNamespaceId),
419419
transactionDTO.valueSizeDelta,
420-
convert.hexToUint8(transactionDTO.value),
420+
convert.decodeHex(transactionDTO.value),
421421
transactionDTO.signature,
422422
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,
423423
extractNetworkType(transactionDTO.version)) : undefined,

src/model/transaction/AccountMetadataTransaction.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export class AccountMetadataTransaction extends Transaction {
4141
* @param targetPublicKey - Public key of the target account.
4242
* @param scopedMetadataKey - Metadata key scoped to source, target and type.
4343
* @param valueSizeDelta - Change in value size in bytes.
44-
* @param value - Difference between the previous value and new value.
44+
* @param value - String value with UTF-8 encoding
45+
* Difference between the previous value and new value.
4546
* You can calculate value as xor(previous-value, new-value).
4647
* If there is no previous value, use directly the new value.
4748
* @param maxFee - (Optional) Max fee defined by the sender
@@ -51,7 +52,7 @@ export class AccountMetadataTransaction extends Transaction {
5152
targetPublicKey: string,
5253
scopedMetadataKey: UInt64,
5354
valueSizeDelta: number,
54-
value: Uint8Array,
55+
value: string,
5556
networkType: NetworkType,
5657
maxFee: UInt64 = new UInt64([0, 0])): AccountMetadataTransaction {
5758
return new AccountMetadataTransaction(networkType,
@@ -94,9 +95,10 @@ export class AccountMetadataTransaction extends Transaction {
9495
*/
9596
public readonly valueSizeDelta: number,
9697
/**
98+
* String value with UTF-8 encoding.
9799
* Difference between the previous value and new value.
98100
*/
99-
public readonly value: Uint8Array,
101+
public readonly value: string,
100102
signature?: string,
101103
signer?: PublicAccount,
102104
transactionInfo?: TransactionInfo) {
@@ -123,7 +125,7 @@ export class AccountMetadataTransaction extends Transaction {
123125
Convert.uint8ToHex(builder.getTargetPublicKey().key),
124126
new UInt64(builder.getScopedMetadataKey()),
125127
builder.getValueSizeDelta(),
126-
builder.getValue(),
128+
Convert.uint8ToUtf8(builder.getValue()),
127129
networkType,
128130
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountMetadataTransactionBuilder).fee.amount),
129131
);
@@ -167,7 +169,7 @@ export class AccountMetadataTransaction extends Transaction {
167169
new KeyDto(Convert.hexToUint8(this.targetPublicKey)),
168170
this.scopedMetadataKey.toDTO(),
169171
this.valueSizeDelta,
170-
this.value,
172+
Convert.utf8ToUint8(this.value),
171173
);
172174
return transactionBuilder.serialize();
173175
}
@@ -184,7 +186,7 @@ export class AccountMetadataTransaction extends Transaction {
184186
new KeyDto(Convert.hexToUint8(this.targetPublicKey)),
185187
this.scopedMetadataKey.toDTO(),
186188
this.valueSizeDelta,
187-
this.value,
189+
Convert.utf8ToUint8(this.value),
188190
);
189191
return transactionBuilder.serialize();
190192
}

src/model/transaction/MosaicMetadataTransaction.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export class MosaicMetadataTransaction extends Transaction {
4444
* @param scopedMetadataKey - Metadata key scoped to source, target and type.
4545
* @param targetMosaicId - Target mosaic identifier.
4646
* @param valueSizeDelta - Change in value size in bytes.
47-
* @param value - Difference between the previous value and new value.
47+
* @param value - String value with UTF-8 encoding
48+
* Difference between the previous value and new value.
4849
* You can calculate value as xor(previous-value, new-value).
4950
* If there is no previous value, use directly the new value.
5051
* @param maxFee - (Optional) Max fee defined by the sender
@@ -55,7 +56,7 @@ export class MosaicMetadataTransaction extends Transaction {
5556
scopedMetadataKey: UInt64,
5657
targetMosaicId: MosaicId,
5758
valueSizeDelta: number,
58-
value: Uint8Array,
59+
value: string,
5960
networkType: NetworkType,
6061
maxFee: UInt64 = new UInt64([0, 0])): MosaicMetadataTransaction {
6162
return new MosaicMetadataTransaction(networkType,
@@ -104,9 +105,10 @@ export class MosaicMetadataTransaction extends Transaction {
104105
*/
105106
public readonly valueSizeDelta: number,
106107
/**
108+
* String value with UTF-8 encoding.
107109
* Difference between the previous value and new value.
108110
*/
109-
public readonly value: Uint8Array,
111+
public readonly value: string,
110112
signature?: string,
111113
signer?: PublicAccount,
112114
transactionInfo?: TransactionInfo) {
@@ -134,7 +136,7 @@ export class MosaicMetadataTransaction extends Transaction {
134136
new UInt64(builder.getScopedMetadataKey()),
135137
new MosaicId(builder.getTargetMosaicId().unresolvedMosaicId),
136138
builder.getValueSizeDelta(),
137-
builder.getValue(),
139+
Convert.uint8ToUtf8(builder.getValue()),
138140
networkType,
139141
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicMetadataTransactionBuilder).fee.amount),
140142
);
@@ -180,7 +182,7 @@ export class MosaicMetadataTransaction extends Transaction {
180182
this.scopedMetadataKey.toDTO(),
181183
new UnresolvedMosaicIdDto(this.targetMosaicId.id.toDTO()),
182184
this.valueSizeDelta,
183-
this.value,
185+
Convert.utf8ToUint8(this.value),
184186
);
185187
return transactionBuilder.serialize();
186188
}
@@ -198,7 +200,7 @@ export class MosaicMetadataTransaction extends Transaction {
198200
this.scopedMetadataKey.toDTO(),
199201
new UnresolvedMosaicIdDto(this.targetMosaicId.id.toDTO()),
200202
this.valueSizeDelta,
201-
this.value,
203+
Convert.utf8ToUint8(this.value),
202204
);
203205
return transactionBuilder.serialize();
204206
}

src/model/transaction/NamespaceMetadataTransaction.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export class NamespaceMetadataTransaction extends Transaction {
4444
* @param scopedMetadataKey - Metadata key scoped to source, target and type.
4545
* @param targetNamespaceId - Target namespace identifier.
4646
* @param valueSizeDelta - Change in value size in bytes.
47-
* @param value - Difference between the previous value and new value.
47+
* @param value - String value with UTF-8 encoding
48+
* Difference between the previous value and new value.
4849
* You can calculate value as xor(previous-value, new-value).
4950
* If there is no previous value, use directly the new value.
5051
* @param maxFee - (Optional) Max fee defined by the sender
@@ -55,7 +56,7 @@ export class NamespaceMetadataTransaction extends Transaction {
5556
scopedMetadataKey: UInt64,
5657
targetNamespaceId: NamespaceId,
5758
valueSizeDelta: number,
58-
value: Uint8Array,
59+
value: string,
5960
networkType: NetworkType,
6061
maxFee: UInt64 = new UInt64([0, 0])): NamespaceMetadataTransaction {
6162
return new NamespaceMetadataTransaction(networkType,
@@ -104,9 +105,10 @@ export class NamespaceMetadataTransaction extends Transaction {
104105
*/
105106
public readonly valueSizeDelta: number,
106107
/**
108+
* String value with UTF-8 encoding.
107109
* Difference between the previous value and new value.
108110
*/
109-
public readonly value: Uint8Array,
111+
public readonly value: string,
110112
signature?: string,
111113
signer?: PublicAccount,
112114
transactionInfo?: TransactionInfo) {
@@ -135,7 +137,7 @@ export class NamespaceMetadataTransaction extends Transaction {
135137
new UInt64(builder.getScopedMetadataKey()),
136138
new NamespaceId(builder.getTargetNamespaceId().namespaceId),
137139
builder.getValueSizeDelta(),
138-
builder.getValue(),
140+
Convert.uint8ToUtf8(builder.getValue()),
139141
networkType,
140142
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as NamespaceMetadataTransactionBuilder).fee.amount),
141143
);
@@ -181,7 +183,7 @@ export class NamespaceMetadataTransaction extends Transaction {
181183
this.scopedMetadataKey.toDTO(),
182184
new NamespaceIdDto(this.targetNamespaceId.id.toDTO()),
183185
this.valueSizeDelta,
184-
this.value,
186+
Convert.utf8ToUint8(this.value),
185187
);
186188
return transactionBuilder.serialize();
187189
}
@@ -199,7 +201,7 @@ export class NamespaceMetadataTransaction extends Transaction {
199201
this.scopedMetadataKey.toDTO(),
200202
new NamespaceIdDto(this.targetNamespaceId.id.toDTO()),
201203
this.valueSizeDelta,
202-
this.value,
204+
Convert.utf8ToUint8(this.value),
203205
);
204206
return transactionBuilder.serialize();
205207
}

test/core/format/Convert.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,49 @@ describe('convert', () => {
357357
}
358358
});
359359
});
360+
361+
describe('utf8ToUni8', () => {
362+
it('should convert numeric string to Uint8Array', () => {
363+
const actual = '123456789';
364+
// Act:
365+
const uint = convert.utf8ToUint8(actual);
366+
367+
// Assert:
368+
expect(uint.length).to.be.equal(actual.length);
369+
expect(uint[0]).to.be.equal(49);
370+
expect(uint[8]).to.be.equal(57);
371+
});
372+
373+
it('should convert utf string to Uint8Array', () => {
374+
const actual = 'test';
375+
// Act:
376+
const uint = convert.utf8ToUint8(actual);
377+
378+
// Assert:
379+
expect(uint.length).to.be.equal(actual.length);
380+
expect(convert.uint8ToHex(uint)).to.be.equal('74657374');
381+
});
382+
});
383+
384+
describe('uint8ToUtf8', () => {
385+
it('should convert Uint8Array to numericString', () => {
386+
const expected = '123456789';
387+
const actual = convert.utf8ToUint8(expected);
388+
// Act:
389+
const result = convert.uint8ToUtf8(actual);
390+
391+
// Assert:
392+
expect(result).to.be.equal(expected);
393+
});
394+
395+
it('should convert Uint8Array to utf8 string ', () => {
396+
const expected = 'test';
397+
const actual = convert.utf8ToUint8(expected);
398+
// Act:
399+
const result = convert.uint8ToUtf8(actual);
400+
401+
// Assert:
402+
expect(result).to.be.equal(expected);
403+
});
404+
});
360405
});

0 commit comments

Comments
 (0)