Skip to content

Commit e51d704

Browse files
committed
updated transaction mapping
1 parent 9198be6 commit e51d704

File tree

8 files changed

+120
-13
lines changed

8 files changed

+120
-13
lines changed

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,17 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
218218
);
219219
} else if (transactionDTO.type === TransactionType.SECRET_LOCK) {
220220
const recipientAddress = transactionDTO.recipientAddress;
221+
const mosaicId = UnresolvedMapping.toUnresolvedMosaic(transactionDTO.mosaicId);
221222
return new SecretLockTransaction(
222223
extractNetworkType(transactionDTO.version),
223224
extractTransactionVersion(transactionDTO.version),
224225
Deadline.createFromDTO(transactionDTO.deadline),
225226
UInt64.fromNumericString(transactionDTO.maxFee || '0'),
226-
new Mosaic(new MosaicId(transactionDTO.mosaicId), UInt64.fromNumericString(transactionDTO.amount)),
227+
new Mosaic(mosaicId, UInt64.fromNumericString(transactionDTO.amount)),
227228
UInt64.fromNumericString(transactionDTO.duration),
228229
transactionDTO.hashAlgorithm,
229230
transactionDTO.secret,
230-
typeof recipientAddress === 'object' && recipientAddress.hasOwnProperty('address') ?
231-
Address.createFromRawAddress(recipientAddress.address) : Address.createFromEncoded(recipientAddress),
231+
extractRecipient(recipientAddress),
232232
transactionDTO.signature,
233233
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,
234234
extractNetworkType(transactionDTO.version)) : undefined,
@@ -243,8 +243,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
243243
UInt64.fromNumericString(transactionDTO.maxFee || '0'),
244244
transactionDTO.hashAlgorithm,
245245
transactionDTO.secret,
246-
typeof recipientAddress === 'object' && recipientAddress.hasOwnProperty('address') ?
247-
Address.createFromRawAddress(recipientAddress.address) : Address.createFromEncoded(recipientAddress),
246+
extractRecipient(recipientAddress),
248247
transactionDTO.proof,
249248
transactionDTO.signature,
250249
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,

src/infrastructure/transaction/SerializeTransactionToJSON.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const SerializeTransactionToJSON = (transaction: Transaction): any => {
144144
return jsonObject;
145145
case TransactionType.SECRET_LOCK:
146146
return {
147-
mosaicId: (transaction as SecretLockTransaction).mosaic.id.id,
147+
mosaicId: (transaction as SecretLockTransaction).mosaic.id.id.toHex(),
148148
amount: (transaction as SecretLockTransaction).mosaic.amount.toString(),
149149
duration: (transaction as SecretLockTransaction).duration.toString(),
150150
hashAlgorithm: (transaction as SecretLockTransaction).hashType,

src/model/transaction/SecretLockTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class SecretLockTransaction extends Transaction {
206206
new BlockDurationDto(this.duration.toDTO()),
207207
this.hashType.valueOf(),
208208
new Hash256Dto(this.getSecretByte()),
209-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress)),
209+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
210210
);
211211
return transactionBuilder.serialize();
212212
}
@@ -225,7 +225,7 @@ export class SecretLockTransaction extends Transaction {
225225
new BlockDurationDto(this.duration.toDTO()),
226226
this.hashType.valueOf(),
227227
new Hash256Dto(this.getSecretByte()),
228-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress)),
228+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
229229
);
230230
return transactionBuilder.serialize();
231231
}

src/model/transaction/SecretProofTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class SecretProofTransaction extends Transaction {
183183
new TimestampDto(this.deadline.toDTO()),
184184
this.hashType.valueOf(),
185185
new Hash256Dto(this.getSecretByte()),
186-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress)),
186+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
187187
this.getProofByte(),
188188
);
189189
return transactionBuilder.serialize();
@@ -200,7 +200,7 @@ export class SecretProofTransaction extends Transaction {
200200
TransactionType.SECRET_PROOF.valueOf(),
201201
this.hashType.valueOf(),
202202
new Hash256Dto(this.getSecretByte()),
203-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress)),
203+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
204204
this.getProofByte(),
205205
);
206206
return transactionBuilder.serialize();

test/core/utils/TransactionMapping.spec.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { TransactionType } from '../../../src/model/transaction/TransactionType'
6666
import { TransferTransaction } from '../../../src/model/transaction/TransferTransaction';
6767
import { UInt64 } from '../../../src/model/UInt64';
6868
import { TestingAccount } from '../../conf/conf.spec';
69+
import { Mosaic } from '../../../src/model/model';
6970

7071
describe('TransactionMapping - createFromPayload', () => {
7172
let account: Account;
@@ -361,7 +362,7 @@ describe('TransactionMapping - createFromPayload', () => {
361362
expect(transaction.duration.equals(UInt64.fromUint(100))).to.be.equal(true);
362363
expect(transaction.hashType).to.be.equal(0);
363364
expect(transaction.secret).to.be.equal('9B3155B37159DA50AA52D5967C509B410F5A36A3B1E31ECB5AC76675D79B4A5E');
364-
expect(transaction.recipientAddress.plain()).to.be.equal(recipientAddress.plain());
365+
expect((transaction.recipientAddress as Address).plain()).to.be.equal(recipientAddress.plain());
365366

366367
});
367368

@@ -382,7 +383,7 @@ describe('TransactionMapping - createFromPayload', () => {
382383
expect(secretProofTransaction.hashType).to.be.equal(0);
383384
expect(secretProofTransaction.secret).to.be.equal('9b3155b37159da50aa52d5967c509b410f5a36a3b1e31ecb5ac76675d79b4a5e' );
384385
expect(secretProofTransaction.proof).to.be.equal(proof);
385-
expect(secretProofTransaction.recipientAddress.plain()).to.be.equal(account.address.plain());
386+
expect((secretProofTransaction.recipientAddress as Address).plain()).to.be.equal(account.address.plain());
386387

387388
});
388389

@@ -902,6 +903,51 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>
902903

903904
});
904905

906+
it('should create SecretLockTransaction - Address alias', () => {
907+
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
908+
const recipientAddress = new NamespaceId('test');
909+
const secretLockTransaction = SecretLockTransaction.create(
910+
Deadline.create(),
911+
NetworkCurrencyMosaic.createAbsolute(10),
912+
UInt64.fromUint(100),
913+
HashType.Op_Sha3_256,
914+
sha3_256.create().update(Convert.hexToUint8(proof)).hex(),
915+
recipientAddress,
916+
NetworkType.MIJIN_TEST,
917+
);
918+
919+
const transaction =
920+
TransactionMapping.createFromDTO(secretLockTransaction.toJSON()) as SecretLockTransaction;
921+
922+
expect(transaction.type).to.be.equal(TransactionType.SECRET_LOCK);
923+
expect(transaction.hashType).to.be.equal(HashType.Op_Sha3_256);
924+
expect((transaction.recipientAddress as NamespaceId).id.toHex()).to.be.equal(recipientAddress.toHex());
925+
926+
});
927+
928+
it('should create SecretLockTransaction - resolved Mosaic', () => {
929+
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
930+
const recipientAddress = Address.createFromRawAddress('SDBDG4IT43MPCW2W4CBBCSJJT42AYALQN7A4VVWL');
931+
const mosaicId = new NamespaceId('test');
932+
const secretLockTransaction = SecretLockTransaction.create(
933+
Deadline.create(),
934+
new Mosaic(new MosaicId([1, 1]), UInt64.fromUint(10)),
935+
UInt64.fromUint(100),
936+
HashType.Op_Sha3_256,
937+
sha3_256.create().update(Convert.hexToUint8(proof)).hex(),
938+
recipientAddress,
939+
NetworkType.MIJIN_TEST,
940+
);
941+
942+
const transaction =
943+
TransactionMapping.createFromDTO(secretLockTransaction.toJSON()) as SecretLockTransaction;
944+
945+
expect(transaction.type).to.be.equal(TransactionType.SECRET_LOCK);
946+
expect(transaction.hashType).to.be.equal(HashType.Op_Sha3_256);
947+
expect(transaction.mosaic.id.toHex()).to.be.equal((new MosaicId([1, 1])).toHex());
948+
949+
});
950+
905951
it('should create SecretProofTransaction', () => {
906952
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
907953
const secretProofTransaction = SecretProofTransaction.create(
@@ -924,6 +970,29 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>
924970

925971
});
926972

973+
it('should create SecretProofTransaction - Address alias', () => {
974+
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
975+
const recipientAddress = new NamespaceId('test');
976+
const secretProofTransaction = SecretProofTransaction.create(
977+
Deadline.create(),
978+
HashType.Op_Sha3_256,
979+
sha3_256.create().update(Convert.hexToUint8(proof)).hex(),
980+
recipientAddress,
981+
proof,
982+
NetworkType.MIJIN_TEST,
983+
);
984+
985+
const transaction =
986+
TransactionMapping.createFromDTO(secretProofTransaction.toJSON()) as SecretProofTransaction;
987+
988+
expect(transaction.type).to.be.equal(TransactionType.SECRET_PROOF);
989+
expect(transaction.hashType).to.be.equal(HashType.Op_Sha3_256);
990+
expect(transaction.secret).to.be.equal(sha3_256.create().update(Convert.hexToUint8(proof)).hex());
991+
expect(transaction.proof).to.be.equal(proof);
992+
expect((transaction.recipientAddress as NamespaceId).id.toHex()).to.be.equal(recipientAddress.toHex());
993+
994+
});
995+
927996
it('should create ModifyMultiSigTransaction', () => {
928997
const modifyMultisigAccountTransaction = MultisigAccountModificationTransaction.create(
929998
Deadline.create(),

test/core/utils/UnresolvedMapping.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { expect } from 'chai';
1717
import { Convert, RawAddress } from '../../../src/core/format';
1818
import { UnresolvedMapping } from '../../../src/core/utils/UnresolvedMapping';
1919
import { Address } from '../../../src/model/account/Address';
20+
import { NetworkType } from "../../../src/model/blockchain/NetworkType";
2021
import { MosaicId } from '../../../src/model/mosaic/MosaicId';
2122
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
22-
import { NetworkType } from "../../../src/model/blockchain/NetworkType";
2323

2424
describe('UnresolvedMapping', () => {
2525
let mosaicId: MosaicId;

test/model/transaction/SecretLockTransaction.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Account } from '../../../src/model/account/Account';
2222
import {Address} from '../../../src/model/account/Address';
2323
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
2424
import {NetworkCurrencyMosaic} from '../../../src/model/mosaic/NetworkCurrencyMosaic';
25+
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
2526
import {Deadline} from '../../../src/model/transaction/Deadline';
2627
import {HashType} from '../../../src/model/transaction/HashType';
2728
import {SecretLockTransaction} from '../../../src/model/transaction/SecretLockTransaction';
@@ -245,4 +246,24 @@ describe('SecretLockTransaction', () => {
245246
expect(Convert.hexToUint8(secretLockTransaction.serialize()).length).to.be.equal(secretLockTransaction.size);
246247
});
247248
});
249+
250+
it('should be created with alias address', () => {
251+
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
252+
const recipientAddress = new NamespaceId('test');
253+
const secretLockTransaction = SecretLockTransaction.create(
254+
Deadline.create(),
255+
NetworkCurrencyMosaic.createAbsolute(10),
256+
UInt64.fromUint(100),
257+
HashType.Op_Sha3_256,
258+
sha3_256.create().update(convert.hexToUint8(proof)).hex(),
259+
recipientAddress,
260+
NetworkType.MIJIN_TEST,
261+
);
262+
deepEqual(secretLockTransaction.mosaic.id.id, NetworkCurrencyMosaic.NAMESPACE_ID.id);
263+
expect(secretLockTransaction.mosaic.amount.equals(UInt64.fromUint(10))).to.be.equal(true);
264+
expect(secretLockTransaction.duration.equals(UInt64.fromUint(100))).to.be.equal(true);
265+
expect(secretLockTransaction.hashType).to.be.equal(0);
266+
expect(secretLockTransaction.secret).to.be.equal('9b3155b37159da50aa52d5967c509b410f5a36a3b1e31ecb5ac76675d79b4a5e');
267+
expect(secretLockTransaction.recipientAddress).to.be.equal(recipientAddress);
268+
});
248269
});

test/model/transaction/SecretProofTransaction.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {keccak_256, sha3_256} from 'js-sha3';
1919
import {Convert, Convert as convert} from '../../../src/core/format';
2020
import { Account } from '../../../src/model/account/Account';
2121
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
22+
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
2223
import {Deadline} from '../../../src/model/transaction/Deadline';
2324
import {HashType} from '../../../src/model/transaction/HashType';
2425
import {SecretProofTransaction} from '../../../src/model/transaction/SecretProofTransaction';
@@ -212,4 +213,21 @@ describe('SecretProofTransaction', () => {
212213
'009B3155B37159DA50AA52D5967C509B410F5A36A3B1E31ECB5AC76675D79B4A5E90A75B6B63D31BDA938' +
213214
'08727940F24699AECDDF17C568508BA2000B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7');
214215
});
216+
217+
it('should be created with alias address', () => {
218+
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
219+
const recipientAddress = new NamespaceId('test');
220+
const secretProofTransaction = SecretProofTransaction.create(
221+
Deadline.create(),
222+
HashType.Op_Sha3_256,
223+
sha3_256.create().update(convert.hexToUint8(proof)).hex(),
224+
recipientAddress,
225+
proof,
226+
NetworkType.MIJIN_TEST,
227+
);
228+
expect(secretProofTransaction.hashType).to.be.equal(0);
229+
expect(secretProofTransaction.secret).to.be.equal('9b3155b37159da50aa52d5967c509b410f5a36a3b1e31ecb5ac76675d79b4a5e' );
230+
expect(secretProofTransaction.proof).to.be.equal(proof);
231+
expect(secretProofTransaction.recipientAddress).to.be.equal(recipientAddress);
232+
});
215233
});

0 commit comments

Comments
 (0)