Skip to content

Commit 7952d24

Browse files
authored
Merge pull request #262 from rg911/task/JAV-3_model_enhancement
JAV-3 [#244 #242]Transaction models enhancement
2 parents cea9709 + 358af54 commit 7952d24

15 files changed

+143
-74
lines changed

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 4 additions & 7 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

@@ -419,7 +419,6 @@ describe('TransactionHttp', () => {
419419
const mosaicGlobalRestrictionTransaction = MosaicGlobalRestrictionTransaction.create(
420420
Deadline.create(),
421421
mosaicId,
422-
new MosaicId(UInt64.fromUint(0).toDTO()),
423422
UInt64.fromUint(60641),
424423
UInt64.fromUint(0),
425424
MosaicRestrictionType.NONE,
@@ -453,7 +452,6 @@ describe('TransactionHttp', () => {
453452
const mosaicGlobalRestrictionTransaction = MosaicGlobalRestrictionTransaction.create(
454453
Deadline.create(),
455454
mosaicId,
456-
new MosaicId(UInt64.fromUint(0).toDTO()),
457455
UInt64.fromUint(60641),
458456
UInt64.fromUint(0),
459457
MosaicRestrictionType.GE,
@@ -494,7 +492,6 @@ describe('TransactionHttp', () => {
494492
mosaicId,
495493
UInt64.fromUint(60641),
496494
account3.address,
497-
UInt64.fromHex('FFFFFFFFFFFFFFFF'),
498495
UInt64.fromUint(2),
499496
NetworkType.MIJIN_TEST,
500497
);

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/MosaicAddressRestrictionTransaction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ export class MosaicAddressRestrictionTransaction extends Transaction {
5454
* @param mosaicId - The mosaic id ex: new MosaicId([481110499, 231112638]).
5555
* @param restrictionKey - The restriction key.
5656
* @param targetAddress - The affected unresolved address.
57-
* @param previousRestrictionValue - The previous restriction value.
5857
* @param newRestrictionValue - The new restriction value.
5958
* @param networkType - The network type.
59+
* @param previousRestrictionValue - (Optional) The previous restriction value.
6060
* @param maxFee - (Optional) Max fee defined by the sender
6161
* @returns {MosaicAddressRestrictionTransaction}
6262
*/
6363
public static create(deadline: Deadline,
6464
mosaicId: MosaicId,
6565
restrictionKey: UInt64,
6666
targetAddress: Address,
67-
previousRestrictionValue: UInt64,
6867
newRestrictionValue: UInt64,
6968
networkType: NetworkType,
69+
previousRestrictionValue: UInt64 = UInt64.fromHex('FFFFFFFFFFFFFFFF'),
7070
maxFee: UInt64 = new UInt64([0, 0])): MosaicAddressRestrictionTransaction {
7171
return new MosaicAddressRestrictionTransaction(networkType,
7272
TransactionVersion.MOSAIC_ADDRESS_RESTRICTION,
@@ -143,9 +143,9 @@ export class MosaicAddressRestrictionTransaction extends Transaction {
143143
new MosaicId(builder.getMosaicId().unresolvedMosaicId),
144144
new UInt64(builder.getRestrictionKey()),
145145
Address.createFromEncoded(Convert.uint8ToHex(builder.getTargetAddress().unresolvedAddress)),
146-
new UInt64(builder.getPreviousRestrictionValue()),
147146
new UInt64(builder.getNewRestrictionValue()),
148147
networkType,
148+
new UInt64(builder.getPreviousRestrictionValue()),
149149
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicAddressRestrictionTransactionBuilder).fee.amount),
150150
);
151151
return isEmbedded ?

src/model/transaction/MosaicGlobalRestrictionTransaction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,25 @@ export class MosaicGlobalRestrictionTransaction extends Transaction {
5454
*
5555
* @param deadline - The deadline to include the transaction.
5656
* @param mosaicId - The mosaic id ex: new MosaicId([481110499, 231112638]).
57-
* @param referenceMosaicId - The mosaic id providing the restriction key.
5857
* @param restrictionKey - The restriction key.
5958
* @param previousRestrictionValue - The previous restriction value.
6059
* @param previousRestrictionType - The previous restriction type.
6160
* @param newRestrictionValue - The new restriction value.
6261
* @param previousRestrictionType - The previous restriction tpye.
6362
* @param networkType - The network type.
63+
* @param referenceMosaicId - (Optional) The mosaic id providing the restriction key.
6464
* @param maxFee - (Optional) Max fee defined by the sender
6565
* @returns {MosaicGlobalRestrictionTransaction}
6666
*/
6767
public static create(deadline: Deadline,
6868
mosaicId: MosaicId,
69-
referenceMosaicId: MosaicId,
7069
restrictionKey: UInt64,
7170
previousRestrictionValue: UInt64,
7271
previousRestrictionType: MosaicRestrictionType,
7372
newRestrictionValue: UInt64,
7473
newRestrictionType: MosaicRestrictionType,
7574
networkType: NetworkType,
75+
referenceMosaicId: MosaicId = new MosaicId(UInt64.fromUint(0).toDTO()),
7676
maxFee: UInt64 = new UInt64([0, 0])): MosaicGlobalRestrictionTransaction {
7777
return new MosaicGlobalRestrictionTransaction(networkType,
7878
TransactionVersion.MOSAIC_GLOBAL_RESTRICTION,
@@ -158,13 +158,13 @@ export class MosaicGlobalRestrictionTransaction extends Transaction {
158158
isEmbedded ? Deadline.create() : Deadline.createFromDTO(
159159
(builder as MosaicGlobalRestrictionTransactionBuilder).getDeadline().timestamp),
160160
new MosaicId(builder.getMosaicId().unresolvedMosaicId),
161-
new MosaicId(builder.getReferenceMosaicId().unresolvedMosaicId),
162161
new UInt64(builder.getRestrictionKey()),
163162
new UInt64(builder.getPreviousRestrictionValue()),
164163
builder.getPreviousRestrictionType().valueOf(),
165164
new UInt64(builder.getNewRestrictionValue()),
166165
builder.getNewRestrictionType().valueOf(),
167166
networkType,
167+
new MosaicId(builder.getReferenceMosaicId().unresolvedMosaicId),
168168
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as MosaicGlobalRestrictionTransactionBuilder).fee.amount),
169169
);
170170
return isEmbedded ?

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
}

0 commit comments

Comments
 (0)