Skip to content

Commit e64f2ad

Browse files
author
Grégory Saive
authored
Merge pull request #80 from rg911/task/g52_delegated_harvesting
Task/g52 delegated harvesting
2 parents 6fffd51 + 58e87c3 commit e64f2ad

File tree

9 files changed

+223
-12
lines changed

9 files changed

+223
-12
lines changed

src/infrastructure/AccountHttp.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {from as observableFrom, Observable} from 'rxjs';
1919
import {map, mergeMap} from 'rxjs/operators';
2020
import {AccountInfo} from '../model/account/AccountInfo';
2121
import { AccountPropertiesInfo } from '../model/account/AccountPropertiesInfo';
22-
import { AccountProperty } from '../model/account/AccountProperty';
2322
import {Address} from '../model/account/Address';
2423
import {MultisigAccountGraphInfo} from '../model/account/MultisigAccountGraphInfo';
2524
import {MultisigAccountInfo} from '../model/account/MultisigAccountInfo';

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717
import {Address} from '../../model/account/Address';
1818
import {PublicAccount} from '../../model/account/PublicAccount';
1919
import {NetworkType} from '../../model/blockchain/NetworkType';
20-
import { AccountPropertyModification,
21-
ModifyAccountPropertyAddressTransaction,
22-
ModifyAccountPropertyEntityTypeTransaction,
23-
ModifyAccountPropertyMosaicTransaction } from '../../model/model';
2420
import {Mosaic} from '../../model/mosaic/Mosaic';
2521
import {MosaicId} from '../../model/mosaic/MosaicId';
2622
import {MosaicProperties} from '../../model/mosaic/MosaicProperties';
2723
import {NamespaceId} from '../../model/namespace/NamespaceId';
24+
import { AccountLinkTransaction } from '../../model/transaction/AccountLinkTransaction';
25+
import {AccountPropertyModification} from '../../model/transaction/AccountPropertyModification';
2826
import {AddressAliasTransaction} from '../../model/transaction/AddressAliasTransaction';
2927
import {AggregateTransaction} from '../../model/transaction/AggregateTransaction';
3028
import {AggregateTransactionCosignature} from '../../model/transaction/AggregateTransactionCosignature';
3129
import {AggregateTransactionInfo} from '../../model/transaction/AggregateTransactionInfo';
3230
import {Deadline} from '../../model/transaction/Deadline';
31+
import { LinkAction } from '../../model/transaction/LinkAction';
3332
import {LockFundsTransaction} from '../../model/transaction/LockFundsTransaction';
33+
import {ModifyAccountPropertyAddressTransaction} from '../../model/transaction/ModifyAccountPropertyAddressTransaction';
34+
import {ModifyAccountPropertyEntityTypeTransaction} from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction';
35+
import {ModifyAccountPropertyMosaicTransaction} from '../../model/transaction/ModifyAccountPropertyMosaicTransaction';
3436
import {ModifyMultisigAccountTransaction} from '../../model/transaction/ModifyMultisigAccountTransaction';
3537
import {MosaicAliasTransaction} from '../../model/transaction/MosaicAliasTransaction';
3638
import {MosaicDefinitionTransaction} from '../../model/transaction/MosaicDefinitionTransaction';
@@ -303,6 +305,18 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
303305
PublicAccount.createFromPublicKey(transactionDTO.signer, extractNetworkType(transactionDTO.version)),
304306
transactionInfo,
305307
);
308+
} else if (transactionDTO.type === TransactionType.LINK_ACCOUNT) {
309+
return new AccountLinkTransaction(
310+
extractNetworkType(transactionDTO.version),
311+
extractTransactionVersion(transactionDTO.version),
312+
Deadline.createFromDTO(transactionDTO.deadline),
313+
new UInt64(transactionDTO.fee || [0, 0]),
314+
transactionDTO.remoteAccountKey,
315+
transactionDTO.linkAction,
316+
transactionDTO.signature,
317+
PublicAccount.createFromPublicKey(transactionDTO.signer, extractNetworkType(transactionDTO.version)),
318+
transactionInfo,
319+
);
306320
}
307321

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

src/model/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export * from './namespace/NamespaceType';
6161
export * from './namespace/AliasActionType';
6262

6363
// Transaction
64+
export * from './transaction/AccountLinkTransaction';
6465
export * from './transaction/AccountPropertyTransaction';
6566
export * from './transaction/ModifyAccountPropertyAddressTransaction';
6667
export * from './transaction/ModifyAccountPropertyEntityTypeTransaction';
@@ -77,6 +78,7 @@ export * from './transaction/Deadline';
7778
export * from './transaction/HashLockTransaction';
7879
export * from './transaction/HashType';
7980
export * from './transaction/InnerTransaction';
81+
export * from './transaction/LinkAction';
8082
export * from './transaction/LockFundsTransaction';
8183
export * from './transaction/Message';
8284
export * from './transaction/ModifyMultisigAccountTransaction';
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 { AccountLinkTransaction as AccountLinkTransactionLibrary, VerifiableTransaction } from 'nem2-library';
18+
import { PublicAccount } from '../account/PublicAccount';
19+
import { NetworkType } from '../blockchain/NetworkType';
20+
import { UInt64 } from '../UInt64';
21+
import { Deadline } from './Deadline';
22+
import { LinkAction } from './LinkAction';
23+
import { Transaction } from './Transaction';
24+
import { TransactionInfo } from './TransactionInfo';
25+
import { TransactionType } from './TransactionType';
26+
import { TransactionVersion } from './TransactionVersion';
27+
28+
/**
29+
* Announce an AccountLinkTransaction to delegate the account importance to a proxy account.
30+
* By doing so, you can enable delegated harvesting
31+
*/
32+
export class AccountLinkTransaction extends Transaction {
33+
/**
34+
* Create a link account transaction object
35+
* @param deadline - The deadline to include the transaction.
36+
* @param remoteAccountKey - The public key of the remote account.
37+
* @param linkAction - The account link action.
38+
* @returns {AccountLinkTransaction}
39+
*/
40+
public static create(deadline: Deadline,
41+
remoteAccountKey: string,
42+
linkAction: LinkAction,
43+
networkType: NetworkType): AccountLinkTransaction {
44+
return new AccountLinkTransaction(networkType,
45+
TransactionVersion.LINK_ACCOUNT,
46+
deadline,
47+
new UInt64([0, 0]),
48+
remoteAccountKey,
49+
linkAction);
50+
}
51+
52+
/**
53+
* @param networkType
54+
* @param version
55+
* @param deadline
56+
* @param fee
57+
* @param remoteAccountKey
58+
* @param linkAction
59+
* @param signature
60+
* @param signer
61+
* @param transactionInfo
62+
*/
63+
constructor(networkType: NetworkType,
64+
version: number,
65+
deadline: Deadline,
66+
fee: UInt64,
67+
/**
68+
* The public key of the remote account.
69+
*/
70+
public readonly remoteAccountKey: string,
71+
/**
72+
* The account link action.
73+
*/
74+
public readonly linkAction: LinkAction,
75+
signature?: string,
76+
signer?: PublicAccount,
77+
transactionInfo?: TransactionInfo) {
78+
super(TransactionType.LINK_ACCOUNT, networkType, version, deadline, fee, signature, signer, transactionInfo);
79+
}
80+
81+
/**
82+
* @internal
83+
* @returns {VerifiableTransaction}
84+
*/
85+
protected buildTransaction(): VerifiableTransaction {
86+
return new AccountLinkTransactionLibrary.Builder()
87+
.addDeadline(this.deadline.toDTO())
88+
.addFee(this.fee.toDTO())
89+
.addVersion(this.versionToDTO())
90+
.addRemoteAccountKey(this.remoteAccountKey)
91+
.addLinkAction(this.linkAction)
92+
.build();
93+
}
94+
95+
}

src/model/transaction/AccountPropertyTransaction.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Address } from '../account/Address';
18+
import { PropertyModificationType } from '../account/PropertyModificationType';
19+
import { PropertyType } from '../account/PropertyType';
1720
import { NetworkType } from '../blockchain/NetworkType';
18-
import { AccountPropertyModification,
19-
Address,
20-
ModifyAccountPropertyAddressTransaction,
21-
ModifyAccountPropertyEntityTypeTransaction,
22-
ModifyAccountPropertyMosaicTransaction,
23-
PropertyModificationType, PropertyType } from '../model';
2421
import { MosaicId } from '../mosaic/MosaicId';
22+
import { AccountPropertyModification } from './AccountPropertyModification';
2523
import { Deadline } from './Deadline';
26-
import { TransactionType } from './TransactionType';
24+
import { ModifyAccountPropertyAddressTransaction } from './ModifyAccountPropertyAddressTransaction';
25+
import { ModifyAccountPropertyEntityTypeTransaction } from './ModifyAccountPropertyEntityTypeTransaction';
26+
import { ModifyAccountPropertyMosaicTransaction } from './ModifyAccountPropertyMosaicTransaction';
2727

2828
export class AccountPropertyTransaction {
2929
/**

src/model/transaction/LinkAction.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
export enum LinkAction {
18+
Link = 0,
19+
Unlink = 1,
20+
}

src/model/transaction/TransactionType.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,10 @@ export class TransactionType {
107107
* @type {number}
108108
*/
109109
public static readonly MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE = 0x4350;
110+
111+
/**
112+
* Link account transaction type
113+
* @type {number}
114+
*/
115+
public static readonly LINK_ACCOUNT = 0x414C;
110116
}

src/model/transaction/TransactionVersion.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,10 @@ export class TransactionVersion {
115115
* @type {number}
116116
*/
117117
public static readonly MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE = 1;
118+
119+
/**
120+
* Link account transaction version
121+
* @type {number}
122+
*/
123+
public static readonly LINK_ACCOUNT = 2;
118124
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 { AccountLinkTransaction } from '../../../src/model/transaction/AccountLinkTransaction';
21+
import { Deadline } from '../../../src/model/transaction/Deadline';
22+
import { LinkAction } from '../../../src/model/transaction/LinkAction';
23+
import { TestingAccount } from '../../conf/conf.spec';
24+
25+
describe('AccountLinkTransaction', () => {
26+
let account: Account;
27+
28+
before(() => {
29+
account = TestingAccount;
30+
});
31+
32+
it('should create an AccountLinkTransaction object with link action', () => {
33+
const accountLinkTransaction = AccountLinkTransaction.create(
34+
Deadline.create(),
35+
account.publicKey,
36+
LinkAction.Link,
37+
NetworkType.MIJIN_TEST,
38+
);
39+
40+
expect(accountLinkTransaction.linkAction).to.be.equal(0);
41+
expect(accountLinkTransaction.remoteAccountKey).to.be.equal(account.publicKey);
42+
43+
const signedTransaction = accountLinkTransaction.signWith(account);
44+
45+
expect(signedTransaction.payload.substring(
46+
240,
47+
signedTransaction.payload.length,
48+
)).to.be.equal('C2F93346E27CE6AD1A9F8F5E3066F8326593A406BDF357ACB041E2F9AB402EFE00');
49+
});
50+
51+
it('should create an AccountLinkTransaction object with unlink action', () => {
52+
const accountLinkTransaction = AccountLinkTransaction.create(
53+
Deadline.create(),
54+
account.publicKey,
55+
LinkAction.Unlink,
56+
NetworkType.MIJIN_TEST,
57+
);
58+
59+
expect(accountLinkTransaction.linkAction).to.be.equal(1);
60+
expect(accountLinkTransaction.remoteAccountKey).to.be.equal(account.publicKey);
61+
62+
const signedTransaction = accountLinkTransaction.signWith(account);
63+
64+
expect(signedTransaction.payload.substring(
65+
240,
66+
signedTransaction.payload.length,
67+
)).to.be.equal('C2F93346E27CE6AD1A9F8F5E3066F8326593A406BDF357ACB041E2F9AB402EFE01');
68+
});
69+
});

0 commit comments

Comments
 (0)