Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/infrastructure/transaction/CreateTransactionFromDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,17 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
);
} else if (transactionDTO.type === TransactionType.SECRET_LOCK) {
const recipientAddress = transactionDTO.recipientAddress;
const mosaicId = UnresolvedMapping.toUnresolvedMosaic(transactionDTO.mosaicId);
return new SecretLockTransaction(
extractNetworkType(transactionDTO.version),
extractTransactionVersion(transactionDTO.version),
Deadline.createFromDTO(transactionDTO.deadline),
UInt64.fromNumericString(transactionDTO.maxFee || '0'),
new Mosaic(new MosaicId(transactionDTO.mosaicId), UInt64.fromNumericString(transactionDTO.amount)),
new Mosaic(mosaicId, UInt64.fromNumericString(transactionDTO.amount)),
UInt64.fromNumericString(transactionDTO.duration),
transactionDTO.hashAlgorithm,
transactionDTO.secret,
typeof recipientAddress === 'object' && recipientAddress.hasOwnProperty('address') ?
Address.createFromRawAddress(recipientAddress.address) : Address.createFromEncoded(recipientAddress),
extractRecipient(recipientAddress),
transactionDTO.signature,
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,
extractNetworkType(transactionDTO.version)) : undefined,
Expand All @@ -243,8 +243,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
UInt64.fromNumericString(transactionDTO.maxFee || '0'),
transactionDTO.hashAlgorithm,
transactionDTO.secret,
typeof recipientAddress === 'object' && recipientAddress.hasOwnProperty('address') ?
Address.createFromRawAddress(recipientAddress.address) : Address.createFromEncoded(recipientAddress),
extractRecipient(recipientAddress),
transactionDTO.proof,
transactionDTO.signature,
transactionDTO.signerPublicKey ? PublicAccount.createFromPublicKey(transactionDTO.signerPublicKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const SerializeTransactionToJSON = (transaction: Transaction): any => {
return jsonObject;
case TransactionType.SECRET_LOCK:
return {
mosaicId: (transaction as SecretLockTransaction).mosaic.id.id,
mosaicId: (transaction as SecretLockTransaction).mosaic.id.id.toHex(),
amount: (transaction as SecretLockTransaction).mosaic.amount.toString(),
duration: (transaction as SecretLockTransaction).duration.toString(),
hashAlgorithm: (transaction as SecretLockTransaction).hashType,
Expand Down
18 changes: 10 additions & 8 deletions src/model/transaction/SecretLockTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
import { Convert, Convert as convert, RawAddress } from '../../core/format';
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
import { BlockDurationDto } from '../../infrastructure/catbuffer/BlockDurationDto';
import { EmbeddedSecretLockTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedSecretLockTransactionBuilder';
Expand All @@ -30,6 +31,7 @@ import { PublicAccount } from '../account/PublicAccount';
import { NetworkType } from '../blockchain/NetworkType';
import { Mosaic } from '../mosaic/Mosaic';
import { MosaicId } from '../mosaic/MosaicId';
import { NamespaceId } from '../namespace/NamespaceId';
import { UInt64 } from '../UInt64';
import { Deadline } from './Deadline';
import { HashType, HashTypeLengthValidator } from './HashType';
Expand All @@ -49,7 +51,7 @@ export class SecretLockTransaction extends Transaction {
* @param duration - The funds lock duration.
* @param hashType - The hash algorithm secret is generated with.
* @param secret - The proof hashed.
* @param recipientAddress - The recipient address of the funds.
* @param recipientAddress - The unresolved recipient address of the funds.
* @param networkType - The network type.
* @param maxFee - (Optional) Max fee defined by the sender
*
Expand All @@ -60,7 +62,7 @@ export class SecretLockTransaction extends Transaction {
duration: UInt64,
hashType: HashType,
secret: string,
recipientAddress: Address,
recipientAddress: Address | NamespaceId,
networkType: NetworkType,
maxFee: UInt64 = new UInt64([0, 0])): SecretLockTransaction {
return new SecretLockTransaction(
Expand Down Expand Up @@ -111,9 +113,9 @@ export class SecretLockTransaction extends Transaction {
*/
public readonly secret: string,
/**
* The recipientAddress of the funds.
* The unresolved recipientAddress of the funds.
*/
public readonly recipientAddress: Address,
public readonly recipientAddress: Address | NamespaceId,
signature?: string,
signer?: PublicAccount,
transactionInfo?: TransactionInfo) {
Expand All @@ -139,13 +141,13 @@ export class SecretLockTransaction extends Transaction {
isEmbedded ? Deadline.create() : Deadline.createFromDTO(
(builder as SecretLockTransactionBuilder).getDeadline().timestamp),
new Mosaic(
new MosaicId(builder.getMosaic().mosaicId.unresolvedMosaicId),
UnresolvedMapping.toUnresolvedMosaic(new UInt64(builder.getMosaic().mosaicId.unresolvedMosaicId).toHex()),
new UInt64(builder.getMosaic().amount.amount),
),
new UInt64(builder.getDuration().blockDuration),
builder.getHashAlgorithm().valueOf(),
Convert.uint8ToHex(builder.getSecret().hash256),
Address.createFromEncoded(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
UnresolvedMapping.toUnresolvedAddress(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
networkType,
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as SecretLockTransactionBuilder).fee.amount),
);
Expand Down Expand Up @@ -204,7 +206,7 @@ export class SecretLockTransaction extends Transaction {
new BlockDurationDto(this.duration.toDTO()),
this.hashType.valueOf(),
new Hash256Dto(this.getSecretByte()),
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
);
return transactionBuilder.serialize();
}
Expand All @@ -223,7 +225,7 @@ export class SecretLockTransaction extends Transaction {
new BlockDurationDto(this.duration.toDTO()),
this.hashType.valueOf(),
new Hash256Dto(this.getSecretByte()),
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
);
return transactionBuilder.serialize();
}
Expand Down
12 changes: 7 additions & 5 deletions src/model/transaction/SecretProofTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { Convert, Convert as convert, RawAddress } from '../../core/format';
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
import { EmbeddedSecretProofTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedSecretProofTransactionBuilder';
import { Hash256Dto } from '../../infrastructure/catbuffer/Hash256Dto';
Expand All @@ -26,6 +27,7 @@ import { UnresolvedAddressDto } from '../../infrastructure/catbuffer/UnresolvedA
import { Address } from '../account/Address';
import { PublicAccount } from '../account/PublicAccount';
import { NetworkType } from '../blockchain/NetworkType';
import { NamespaceId } from '../namespace/NamespaceId';
import { UInt64 } from '../UInt64';
import { Deadline } from './Deadline';
import { HashType, HashTypeLengthValidator } from './HashType';
Expand Down Expand Up @@ -53,7 +55,7 @@ export class SecretProofTransaction extends Transaction {
public static create(deadline: Deadline,
hashType: HashType,
secret: string,
recipientAddress: Address,
recipientAddress: Address | NamespaceId,
proof: string,
networkType: NetworkType,
maxFee: UInt64 = new UInt64([0, 0])): SecretProofTransaction {
Expand Down Expand Up @@ -88,7 +90,7 @@ export class SecretProofTransaction extends Transaction {
maxFee: UInt64,
public readonly hashType: HashType,
public readonly secret: string,
public readonly recipientAddress: Address,
public readonly recipientAddress: Address | NamespaceId,
public readonly proof: string,
signature?: string,
signer?: PublicAccount,
Expand Down Expand Up @@ -116,7 +118,7 @@ export class SecretProofTransaction extends Transaction {
(builder as SecretProofTransactionBuilder).getDeadline().timestamp),
builder.getHashAlgorithm().valueOf(),
Convert.uint8ToHex(builder.getSecret().hash256),
Address.createFromEncoded(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
UnresolvedMapping.toUnresolvedAddress(Convert.uint8ToHex(builder.getRecipientAddress().unresolvedAddress)),
Convert.uint8ToHex(builder.getProof()),
networkType,
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as SecretProofTransactionBuilder).fee.amount),
Expand Down Expand Up @@ -181,7 +183,7 @@ export class SecretProofTransaction extends Transaction {
new TimestampDto(this.deadline.toDTO()),
this.hashType.valueOf(),
new Hash256Dto(this.getSecretByte()),
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
this.getProofByte(),
);
return transactionBuilder.serialize();
Expand All @@ -198,7 +200,7 @@ export class SecretProofTransaction extends Transaction {
TransactionType.SECRET_PROOF.valueOf(),
this.hashType.valueOf(),
new Hash256Dto(this.getSecretByte()),
new UnresolvedAddressDto(RawAddress.stringToAddress(this.recipientAddress.plain())),
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
this.getProofByte(),
);
return transactionBuilder.serialize();
Expand Down
4 changes: 3 additions & 1 deletion src/service/MetadataTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class MetadataTransactionService {
* @param senderPublicAccount - sender (signer) public account
* @param targetId - Target Id (MosaicId | NamespaceId)
* @param maxFee - Max fee
* @return {AccountMetadataTransaction | MosaicMetadataTransaction | NamespaceMetadataTransaction}
*/
public createMetadataTransaction(deadline: Deadline,
networkType: NetworkType,
Expand All @@ -64,7 +65,8 @@ export class MetadataTransactionService {
value: string,
senderPublicAccount: PublicAccount,
targetId?: MosaicId | NamespaceId,
maxFee: UInt64 = new UInt64([0, 0])): Observable<Transaction> {
maxFee: UInt64 = new UInt64([0, 0])):
Observable<AccountMetadataTransaction | MosaicMetadataTransaction | NamespaceMetadataTransaction> {
switch (metadataType) {
case MetadataType.Account:
return this.createAccountMetadataTransaction(
Expand Down
73 changes: 71 additions & 2 deletions test/core/utils/TransactionMapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { TransactionType } from '../../../src/model/transaction/TransactionType'
import { TransferTransaction } from '../../../src/model/transaction/TransferTransaction';
import { UInt64 } from '../../../src/model/UInt64';
import { TestingAccount } from '../../conf/conf.spec';
import { Mosaic } from '../../../src/model/model';

describe('TransactionMapping - createFromPayload', () => {
let account: Account;
Expand Down Expand Up @@ -361,7 +362,7 @@ describe('TransactionMapping - createFromPayload', () => {
expect(transaction.duration.equals(UInt64.fromUint(100))).to.be.equal(true);
expect(transaction.hashType).to.be.equal(0);
expect(transaction.secret).to.be.equal('9B3155B37159DA50AA52D5967C509B410F5A36A3B1E31ECB5AC76675D79B4A5E');
expect(transaction.recipientAddress.plain()).to.be.equal(recipientAddress.plain());
expect((transaction.recipientAddress as Address).plain()).to.be.equal(recipientAddress.plain());

});

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

});

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

});

it('should create SecretLockTransaction - Address alias', () => {
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
const recipientAddress = new NamespaceId('test');
const secretLockTransaction = SecretLockTransaction.create(
Deadline.create(),
NetworkCurrencyMosaic.createAbsolute(10),
UInt64.fromUint(100),
HashType.Op_Sha3_256,
sha3_256.create().update(Convert.hexToUint8(proof)).hex(),
recipientAddress,
NetworkType.MIJIN_TEST,
);

const transaction =
TransactionMapping.createFromDTO(secretLockTransaction.toJSON()) as SecretLockTransaction;

expect(transaction.type).to.be.equal(TransactionType.SECRET_LOCK);
expect(transaction.hashType).to.be.equal(HashType.Op_Sha3_256);
expect((transaction.recipientAddress as NamespaceId).id.toHex()).to.be.equal(recipientAddress.toHex());

});

it('should create SecretLockTransaction - resolved Mosaic', () => {
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
const recipientAddress = Address.createFromRawAddress('SDBDG4IT43MPCW2W4CBBCSJJT42AYALQN7A4VVWL');
const mosaicId = new NamespaceId('test');
const secretLockTransaction = SecretLockTransaction.create(
Deadline.create(),
new Mosaic(new MosaicId([1, 1]), UInt64.fromUint(10)),
UInt64.fromUint(100),
HashType.Op_Sha3_256,
sha3_256.create().update(Convert.hexToUint8(proof)).hex(),
recipientAddress,
NetworkType.MIJIN_TEST,
);

const transaction =
TransactionMapping.createFromDTO(secretLockTransaction.toJSON()) as SecretLockTransaction;

expect(transaction.type).to.be.equal(TransactionType.SECRET_LOCK);
expect(transaction.hashType).to.be.equal(HashType.Op_Sha3_256);
expect(transaction.mosaic.id.toHex()).to.be.equal((new MosaicId([1, 1])).toHex());

});

it('should create SecretProofTransaction', () => {
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
const secretProofTransaction = SecretProofTransaction.create(
Expand All @@ -924,6 +970,29 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>

});

it('should create SecretProofTransaction - Address alias', () => {
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
const recipientAddress = new NamespaceId('test');
const secretProofTransaction = SecretProofTransaction.create(
Deadline.create(),
HashType.Op_Sha3_256,
sha3_256.create().update(Convert.hexToUint8(proof)).hex(),
recipientAddress,
proof,
NetworkType.MIJIN_TEST,
);

const transaction =
TransactionMapping.createFromDTO(secretProofTransaction.toJSON()) as SecretProofTransaction;

expect(transaction.type).to.be.equal(TransactionType.SECRET_PROOF);
expect(transaction.hashType).to.be.equal(HashType.Op_Sha3_256);
expect(transaction.secret).to.be.equal(sha3_256.create().update(Convert.hexToUint8(proof)).hex());
expect(transaction.proof).to.be.equal(proof);
expect((transaction.recipientAddress as NamespaceId).id.toHex()).to.be.equal(recipientAddress.toHex());

});

it('should create ModifyMultiSigTransaction', () => {
const modifyMultisigAccountTransaction = MultisigAccountModificationTransaction.create(
Deadline.create(),
Expand Down
2 changes: 1 addition & 1 deletion test/core/utils/UnresolvedMapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { expect } from 'chai';
import { Convert, RawAddress } from '../../../src/core/format';
import { UnresolvedMapping } from '../../../src/core/utils/UnresolvedMapping';
import { Address } from '../../../src/model/account/Address';
import { NetworkType } from "../../../src/model/blockchain/NetworkType";
import { MosaicId } from '../../../src/model/mosaic/MosaicId';
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
import { NetworkType } from "../../../src/model/blockchain/NetworkType";

describe('UnresolvedMapping', () => {
let mosaicId: MosaicId;
Expand Down
21 changes: 21 additions & 0 deletions test/model/transaction/SecretLockTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Account } from '../../../src/model/account/Account';
import {Address} from '../../../src/model/account/Address';
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
import {NetworkCurrencyMosaic} from '../../../src/model/mosaic/NetworkCurrencyMosaic';
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
import {Deadline} from '../../../src/model/transaction/Deadline';
import {HashType} from '../../../src/model/transaction/HashType';
import {SecretLockTransaction} from '../../../src/model/transaction/SecretLockTransaction';
Expand Down Expand Up @@ -245,4 +246,24 @@ describe('SecretLockTransaction', () => {
expect(Convert.hexToUint8(secretLockTransaction.serialize()).length).to.be.equal(secretLockTransaction.size);
});
});

it('should be created with alias address', () => {
const proof = 'B778A39A3663719DFC5E48C9D78431B1E45C2AF9DF538782BF199C189DABEAC7';
const recipientAddress = new NamespaceId('test');
const secretLockTransaction = SecretLockTransaction.create(
Deadline.create(),
NetworkCurrencyMosaic.createAbsolute(10),
UInt64.fromUint(100),
HashType.Op_Sha3_256,
sha3_256.create().update(convert.hexToUint8(proof)).hex(),
recipientAddress,
NetworkType.MIJIN_TEST,
);
deepEqual(secretLockTransaction.mosaic.id.id, NetworkCurrencyMosaic.NAMESPACE_ID.id);
expect(secretLockTransaction.mosaic.amount.equals(UInt64.fromUint(10))).to.be.equal(true);
expect(secretLockTransaction.duration.equals(UInt64.fromUint(100))).to.be.equal(true);
expect(secretLockTransaction.hashType).to.be.equal(0);
expect(secretLockTransaction.secret).to.be.equal('9b3155b37159da50aa52d5967c509b410f5a36a3b1e31ecb5ac76675d79b4a5e');
expect(secretLockTransaction.recipientAddress).to.be.equal(recipientAddress);
});
});
Loading