Skip to content

Commit 4c06905

Browse files
author
Greg S
committed
#55: Added AddressAliasTransaction and AliasTransaction.createForAddress
1 parent b2f7731 commit 4c06905

File tree

6 files changed

+265
-1
lines changed

6 files changed

+265
-1
lines changed

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {Mosaic} from '../../model/mosaic/Mosaic';
2121
import {MosaicId} from '../../model/mosaic/MosaicId';
2222
import {MosaicProperties} from '../../model/mosaic/MosaicProperties';
2323
import {NamespaceId} from '../../model/namespace/NamespaceId';
24+
import {AddressAliasTransaction} from '../../model/transaction/AddressAliasTransaction';
2425
import {AggregateTransaction} from '../../model/transaction/AggregateTransaction';
2526
import {AggregateTransactionCosignature} from '../../model/transaction/AggregateTransactionCosignature';
2627
import {AggregateTransactionInfo} from '../../model/transaction/AggregateTransactionInfo';
@@ -240,6 +241,19 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
240241
PublicAccount.createFromPublicKey(transactionDTO.signer, extractNetworkType(transactionDTO.version)),
241242
transactionInfo,
242243
);
244+
} else if (transactionDTO.type === TransactionType.ADDRESS_ALIAS) {
245+
return new AddressAliasTransaction(
246+
extractNetworkType(transactionDTO.version),
247+
extractTransactionVersion(transactionDTO.version),
248+
Deadline.createFromDTO(transactionDTO.deadline),
249+
new UInt64(transactionDTO.fee),
250+
transactionDTO.actionType,
251+
transactionDTO.namespaceId,
252+
transactionDTO.address,
253+
transactionDTO.signature,
254+
PublicAccount.createFromPublicKey(transactionDTO.signer, extractNetworkType(transactionDTO.version)),
255+
transactionInfo,
256+
);
243257
}
244258

245259
throw new Error('Unimplemented transaction with type ' + transactionDTO.type);

src/model/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export * from './namespace/NamespaceType';
5050
export * from './namespace/AliasActionType';
5151

5252
// Transaction
53+
export * from './transaction/AddressAliasTransaction';
5354
export * from './transaction/AggregateTransaction';
5455
export * from './transaction/AggregateTransactionCosignature';
5556
export * from './transaction/AggregateTransactionInfo';
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { AddressAliasTransaction as AddressAliasTransactionLibrary, VerifiableTransaction } from 'nem2-library';
18+
import { Address } from '../account/Address';
19+
import { PublicAccount } from '../account/PublicAccount';
20+
import { NetworkType } from '../blockchain/NetworkType';
21+
import { AliasActionType } from '../namespace/AliasActionType';
22+
import { NamespaceId } from '../namespace/NamespaceId';
23+
import { UInt64 } from '../UInt64';
24+
import { Deadline } from './Deadline';
25+
import { Transaction } from './Transaction';
26+
import { TransactionInfo } from './TransactionInfo';
27+
import { TransactionType } from './TransactionType';
28+
import { TransactionVersion } from './TransactionVersion';
29+
30+
/**
31+
* In case a mosaic has the flag 'supplyMutable' set to true, the creator of the mosaic can change the supply,
32+
* i.e. increase or decrease the supply.
33+
*/
34+
export class AddressAliasTransaction extends Transaction {
35+
36+
/**
37+
* Create a mosaic supply change transaction object
38+
* @param deadline - The deadline to include the transaction.
39+
* @param actionType - The namespace id.
40+
* @param namespaceId - The namespace id.
41+
* @param mosaicId - The mosaic id.
42+
* @param networkType - The network type.
43+
* @returns {AddressAliasTransaction}
44+
*/
45+
public static create(deadline: Deadline,
46+
actionType: AliasActionType,
47+
namespaceId: NamespaceId,
48+
address: Address,
49+
networkType: NetworkType): AddressAliasTransaction {
50+
return new AddressAliasTransaction(networkType,
51+
TransactionVersion.MOSAIC_ALIAS,
52+
deadline,
53+
new UInt64([0, 0]),
54+
actionType,
55+
namespaceId,
56+
address,
57+
);
58+
}
59+
60+
/**
61+
* @param networkType
62+
* @param version
63+
* @param deadline
64+
* @param fee
65+
* @param actionType
66+
* @param namespaceId
67+
* @param address
68+
* @param signature
69+
* @param signer
70+
* @param transactionInfo
71+
*/
72+
constructor(networkType: NetworkType,
73+
version: number,
74+
deadline: Deadline,
75+
fee: UInt64,
76+
/**
77+
* The alias action type.
78+
*/
79+
public readonly actionType: AliasActionType,
80+
/**
81+
* The namespace id that will be an alias.
82+
*/
83+
public readonly namespaceId: NamespaceId,
84+
/**
85+
* The mosaic id.
86+
*/
87+
public readonly address: Address,
88+
signature?: string,
89+
signer?: PublicAccount,
90+
transactionInfo?: TransactionInfo) {
91+
super(TransactionType.MOSAIC_ALIAS, networkType, version, deadline, fee, signature, signer, transactionInfo);
92+
}
93+
94+
/**
95+
* @internal
96+
* @returns {VerifiableTransaction}
97+
*/
98+
protected buildTransaction(): VerifiableTransaction {
99+
return new AddressAliasTransactionLibrary.Builder()
100+
.addDeadline(this.deadline.toDTO())
101+
.addFee(this.fee.toDTO())
102+
.addVersion(this.versionToDTO())
103+
.addActionType(this.actionType)
104+
.addNamespaceId(this.namespaceId.id.toDTO())
105+
.addAddress(this.address.plain())
106+
.build();
107+
}
108+
109+
}

src/model/transaction/AliasTransaction.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { MosaicId } from '../mosaic/MosaicId';
2222
import { AliasActionType } from '../namespace/AliasActionType';
2323
import { NamespaceId } from '../namespace/NamespaceId';
2424
import { UInt64 } from '../UInt64';
25+
import { AddressAliasTransaction } from './AddressAliasTransaction';
2526
import { Deadline } from './Deadline';
2627
import { MosaicAliasTransaction } from './MosaicAliasTransaction';
2728
import { Transaction } from './Transaction';
@@ -32,7 +33,24 @@ import { TransactionVersion } from './TransactionVersion';
3233
export abstract class AliasTransaction extends Transaction {
3334

3435
/**
35-
* Create a mosaic supply change transaction object
36+
* Create an address alias transaction object
37+
* @param deadline - The deadline to include the transaction.
38+
* @param aliasAction - The namespace id.
39+
* @param namespaceId - The namespace id.
40+
* @param address - The address.
41+
* @param networkType - The network type.
42+
* @returns {AddressAliasTransaction}
43+
*/
44+
public static createForAddress(deadline: Deadline,
45+
aliasAction: AliasActionType,
46+
namespaceId: NamespaceId,
47+
address: Address,
48+
networkType: NetworkType): AliasTransaction {
49+
return AddressAliasTransaction.create(deadline, aliasAction, namespaceId, address, networkType);
50+
}
51+
52+
/**
53+
* Create a mosaic alias transaction object
3654
* @param deadline - The deadline to include the transaction.
3755
* @param aliasAction - The namespace id.
3856
* @param namespaceId - The namespace id.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {expect} from 'chai';
18+
import {Account} from '../../../src/model/account/Account';
19+
import {Address} from '../../../src/model/account/Address';
20+
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
21+
import {MosaicId} from '../../../src/model/mosaic/MosaicId';
22+
import {AliasActionType} from '../../../src/model/namespace/AliasActionType';
23+
import {NamespaceId} from '../../../src/model/namespace/NamespaceId';
24+
import {AddressAliasTransaction} from '../../../src/model/transaction/AddressAliasTransaction';
25+
import {Deadline} from '../../../src/model/transaction/Deadline';
26+
import {UInt64} from '../../../src/model/UInt64';
27+
import {TestingAccount} from '../../conf/conf.spec';
28+
29+
describe('AddressAliasTransaction', () => {
30+
let account: Account;
31+
32+
before(() => {
33+
account = TestingAccount;
34+
});
35+
36+
it('should createComplete an AddressAliasTransaction object and sign it', () => {
37+
const namespaceId = new NamespaceId([33347626, 3779697293]);
38+
const address = Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
39+
const addressAliasTransaction = AddressAliasTransaction.create(
40+
Deadline.create(),
41+
AliasActionType.Link,
42+
namespaceId,
43+
address,
44+
NetworkType.MIJIN_TEST,
45+
);
46+
47+
expect(addressAliasTransaction.actionType).to.be.equal(AliasActionType.Link);
48+
expect(addressAliasTransaction.namespaceId.id.lower).to.be.equal(33347626);
49+
expect(addressAliasTransaction.namespaceId.id.higher).to.be.equal(3779697293);
50+
expect(addressAliasTransaction.address.plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
51+
52+
const signedTransaction = addressAliasTransaction.signWith(account);
53+
54+
console.log(signedTransaction.payload);
55+
56+
expect(signedTransaction.payload.substring(
57+
240,
58+
signedTransaction.payload.length,
59+
)).to.be.equal('002AD8FC018D9A49E19050B9837EFAB4BBE8A4B9BB32D812F9885C00D8FC1650E142');
60+
61+
});
62+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {expect} from 'chai';
18+
import {Account} from '../../../src/model/account/Account';
19+
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
20+
import {MosaicId} from '../../../src/model/mosaic/MosaicId';
21+
import {AliasActionType} from '../../../src/model/namespace/AliasActionType';
22+
import {NamespaceId} from '../../../src/model/namespace/NamespaceId';
23+
import {Deadline} from '../../../src/model/transaction/Deadline';
24+
import {MosaicAliasTransaction} from '../../../src/model/transaction/MosaicAliasTransaction';
25+
import {UInt64} from '../../../src/model/UInt64';
26+
import {TestingAccount} from '../../conf/conf.spec';
27+
28+
describe('MosaicAliasTransaction', () => {
29+
let account: Account;
30+
31+
before(() => {
32+
account = TestingAccount;
33+
});
34+
35+
it('should createComplete an MosaicAliasTransaction object and sign it', () => {
36+
const namespaceId = new NamespaceId([33347626, 3779697293]);
37+
const mosaicId = new MosaicId([2262289484, 3405110546]);
38+
const mosaicAliasTransaction = MosaicAliasTransaction.create(
39+
Deadline.create(),
40+
AliasActionType.Link,
41+
namespaceId,
42+
mosaicId,
43+
NetworkType.MIJIN_TEST,
44+
);
45+
46+
expect(mosaicAliasTransaction.actionType).to.be.equal(AliasActionType.Link);
47+
expect(mosaicAliasTransaction.namespaceId.id.lower).to.be.equal(33347626);
48+
expect(mosaicAliasTransaction.namespaceId.id.higher).to.be.equal(3779697293);
49+
expect(mosaicAliasTransaction.mosaicId.id.lower).to.be.equal(2262289484);
50+
expect(mosaicAliasTransaction.mosaicId.id.higher).to.be.equal(3405110546);
51+
52+
const signedTransaction = mosaicAliasTransaction.signWith(account);
53+
54+
expect(signedTransaction.payload.substring(
55+
240,
56+
signedTransaction.payload.length,
57+
)).to.be.equal('002AD8FC018D9A49E14CCCD78612DDF5CA');
58+
59+
});
60+
});

0 commit comments

Comments
 (0)