Skip to content

Commit 9578659

Browse files
authored
Merge pull request #332 from NEMStudios/task/g323_alias_in_secret_lock
Task/g323 alias in secret lock
2 parents cd31511 + e51d704 commit 9578659

File tree

9 files changed

+136
-23
lines changed

9 files changed

+136
-23
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: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { Convert, Convert as convert, RawAddress } from '../../core/format';
17+
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
1718
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
1819
import { BlockDurationDto } from '../../infrastructure/catbuffer/BlockDurationDto';
1920
import { EmbeddedSecretLockTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedSecretLockTransactionBuilder';
@@ -30,6 +31,7 @@ import { PublicAccount } from '../account/PublicAccount';
3031
import { NetworkType } from '../blockchain/NetworkType';
3132
import { Mosaic } from '../mosaic/Mosaic';
3233
import { MosaicId } from '../mosaic/MosaicId';
34+
import { NamespaceId } from '../namespace/NamespaceId';
3335
import { UInt64 } from '../UInt64';
3436
import { Deadline } from './Deadline';
3537
import { HashType, HashTypeLengthValidator } from './HashType';
@@ -49,7 +51,7 @@ export class SecretLockTransaction extends Transaction {
4951
* @param duration - The funds lock duration.
5052
* @param hashType - The hash algorithm secret is generated with.
5153
* @param secret - The proof hashed.
52-
* @param recipientAddress - The recipient address of the funds.
54+
* @param recipientAddress - The unresolved recipient address of the funds.
5355
* @param networkType - The network type.
5456
* @param maxFee - (Optional) Max fee defined by the sender
5557
*
@@ -60,7 +62,7 @@ export class SecretLockTransaction extends Transaction {
6062
duration: UInt64,
6163
hashType: HashType,
6264
secret: string,
63-
recipientAddress: Address,
65+
recipientAddress: Address | NamespaceId,
6466
networkType: NetworkType,
6567
maxFee: UInt64 = new UInt64([0, 0])): SecretLockTransaction {
6668
return new SecretLockTransaction(
@@ -111,9 +113,9 @@ export class SecretLockTransaction extends Transaction {
111113
*/
112114
public readonly secret: string,
113115
/**
114-
* The recipientAddress of the funds.
116+
* The unresolved recipientAddress of the funds.
115117
*/
116-
public readonly recipientAddress: Address,
118+
public readonly recipientAddress: Address | NamespaceId,
117119
signature?: string,
118120
signer?: PublicAccount,
119121
transactionInfo?: TransactionInfo) {
@@ -139,13 +141,13 @@ export class SecretLockTransaction extends Transaction {
139141
isEmbedded ? Deadline.create() : Deadline.createFromDTO(
140142
(builder as SecretLockTransactionBuilder).getDeadline().timestamp),
141143
new Mosaic(
142-
new MosaicId(builder.getMosaic().mosaicId.unresolvedMosaicId),
144+
UnresolvedMapping.toUnresolvedMosaic(new UInt64(builder.getMosaic().mosaicId.unresolvedMosaicId).toHex()),
143145
new UInt64(builder.getMosaic().amount.amount),
144146
),
145147
new UInt64(builder.getDuration().blockDuration),
146148
builder.getHashAlgorithm().valueOf(),
147149
Convert.uint8ToHex(builder.getSecret().hash256),
148-
Address.createFromEncoded(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
150+
UnresolvedMapping.toUnresolvedAddress(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
149151
networkType,
150152
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as SecretLockTransactionBuilder).fee.amount),
151153
);
@@ -204,7 +206,7 @@ export class SecretLockTransaction extends Transaction {
204206
new BlockDurationDto(this.duration.toDTO()),
205207
this.hashType.valueOf(),
206208
new Hash256Dto(this.getSecretByte()),
207-
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
209+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
208210
);
209211
return transactionBuilder.serialize();
210212
}
@@ -223,7 +225,7 @@ export class SecretLockTransaction extends Transaction {
223225
new BlockDurationDto(this.duration.toDTO()),
224226
this.hashType.valueOf(),
225227
new Hash256Dto(this.getSecretByte()),
226-
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
228+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
227229
);
228230
return transactionBuilder.serialize();
229231
}

src/model/transaction/SecretProofTransaction.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { Convert, Convert as convert, RawAddress } from '../../core/format';
18+
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
1819
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
1920
import { EmbeddedSecretProofTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedSecretProofTransactionBuilder';
2021
import { Hash256Dto } from '../../infrastructure/catbuffer/Hash256Dto';
@@ -26,6 +27,7 @@ import { UnresolvedAddressDto } from '../../infrastructure/catbuffer/UnresolvedA
2627
import { Address } from '../account/Address';
2728
import { PublicAccount } from '../account/PublicAccount';
2829
import { NetworkType } from '../blockchain/NetworkType';
30+
import { NamespaceId } from '../namespace/NamespaceId';
2931
import { UInt64 } from '../UInt64';
3032
import { Deadline } from './Deadline';
3133
import { HashType, HashTypeLengthValidator } from './HashType';
@@ -53,7 +55,7 @@ export class SecretProofTransaction extends Transaction {
5355
public static create(deadline: Deadline,
5456
hashType: HashType,
5557
secret: string,
56-
recipientAddress: Address,
58+
recipientAddress: Address | NamespaceId,
5759
proof: string,
5860
networkType: NetworkType,
5961
maxFee: UInt64 = new UInt64([0, 0])): SecretProofTransaction {
@@ -88,7 +90,7 @@ export class SecretProofTransaction extends Transaction {
8890
maxFee: UInt64,
8991
public readonly hashType: HashType,
9092
public readonly secret: string,
91-
public readonly recipientAddress: Address,
93+
public readonly recipientAddress: Address | NamespaceId,
9294
public readonly proof: string,
9395
signature?: string,
9496
signer?: PublicAccount,
@@ -116,7 +118,7 @@ export class SecretProofTransaction extends Transaction {
116118
(builder as SecretProofTransactionBuilder).getDeadline().timestamp),
117119
builder.getHashAlgorithm().valueOf(),
118120
Convert.uint8ToHex(builder.getSecret().hash256),
119-
Address.createFromEncoded(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
121+
UnresolvedMapping.toUnresolvedAddress(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
120122
Convert.uint8ToHex(builder.getProof()),
121123
networkType,
122124
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as SecretProofTransactionBuilder).fee.amount),
@@ -181,7 +183,7 @@ export class SecretProofTransaction extends Transaction {
181183
new TimestampDto(this.deadline.toDTO()),
182184
this.hashType.valueOf(),
183185
new Hash256Dto(this.getSecretByte()),
184-
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
186+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
185187
this.getProofByte(),
186188
);
187189
return transactionBuilder.serialize();
@@ -198,7 +200,7 @@ export class SecretProofTransaction extends Transaction {
198200
TransactionType.SECRET_PROOF.valueOf(),
199201
this.hashType.valueOf(),
200202
new Hash256Dto(this.getSecretByte()),
201-
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
203+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
202204
this.getProofByte(),
203205
);
204206
return transactionBuilder.serialize();

src/service/MetadataTransactionService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class MetadataTransactionService {
5555
* @param senderPublicAccount - sender (signer) public account
5656
* @param targetId - Target Id (MosaicId | NamespaceId)
5757
* @param maxFee - Max fee
58+
* @return {AccountMetadataTransaction | MosaicMetadataTransaction | NamespaceMetadataTransaction}
5859
*/
5960
public createMetadataTransaction(deadline: Deadline,
6061
networkType: NetworkType,
@@ -64,7 +65,8 @@ export class MetadataTransactionService {
6465
value: string,
6566
senderPublicAccount: PublicAccount,
6667
targetId?: MosaicId | NamespaceId,
67-
maxFee: UInt64 = new UInt64([0, 0])): Observable<Transaction> {
68+
maxFee: UInt64 = new UInt64([0, 0])):
69+
Observable<AccountMetadataTransaction | MosaicMetadataTransaction | NamespaceMetadataTransaction> {
6870
switch (metadataType) {
6971
case MetadataType.Account:
7072
return this.createAccountMetadataTransaction(

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
});

0 commit comments

Comments
 (0)