-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendTransferTxUsingAddressAlias.ts
More file actions
73 lines (65 loc) · 2.2 KB
/
Copy pathSendTransferTxUsingAddressAlias.ts
File metadata and controls
73 lines (65 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ** USAGE **
//
// $ ts-node SendTransferTransction.ts <private key> <recipient address> <amount> <mosaic id> <message>
//
// amount : amount of mosaic
// mosaic id : mosaic id that you want to send (optional, defalut = NemConst.CURRENCY_MOSAIC_ID )
// message : '-m' + <message>
// alias : '-a' + <alias>
//
// (e.g) $ ts-node SendTransferTx.ts <your private key> SCJPIKELZEMCO6CJIFAHSB4RXPBDKHN6GFVMUM7A 100 -mhello
import * as nem from 'nem2-sdk'
import { NemConst } from './share/NemConst'
import { Util } from './share/Util'
import { TxUtil } from './share/TxUtil'
import { DefaultOptParse } from './share/DefaultOptParse';
const optParse = new DefaultOptParse();
optParse.subscribePrivateKey();
optParse.subscribe(
'amount',
(arg: string) => { return (/^\d+$/).test(arg) },
true
);
optParse.subscribe(
'mosaicId',
(arg: string) => { return (/^-m[0-9A-Fa-f]{16}$/).test(arg) },
false,
(arg: string) => { return arg.slice(2) },
NemConst.CURRENCY_MOSAIC_ID
);
optParse.subscribe(
'addressAlias',
(arg: string) => { return (/^-a\w+$/).test(arg) },
true,
(arg: string) => { return arg.slice(2) }
);
optParse.subscribe(
'message',
(arg: string) => { return (/^-M.+$/).test(arg) },
false,
(arg: string) => { return arg.slice(2) }
);
const option = optParse.parse();
// create TransferTx //
const netType = NemConst.NETWORK_TYPE;
const sender = nem.Account.createFromPrivateKey(option.get('privateKey'), netType);
const recipientAliasId = option.get('addressAlias');
const recipient = new nem.NamespaceId(recipientAliasId);
// calc modaic id
const amount = parseInt(option.get('amount'));
const mosaicId = option.get('mosaicId');
const mosaic = new nem.Mosaic(new nem.MosaicId(mosaicId), nem.UInt64.fromUint(amount));
const mosaics = [mosaic];
const msg = option.get('message') ? option.get('message') : '';
console.log(` mosaicId : ${mosaicId}`);
console.log(` amount : ${amount}`);
console.log(`recipient : ${recipientAliasId} (Alias)`);
console.log(` message : ${msg}`);
const transferTx = nem.TransferTransaction.create(
nem.Deadline.create(),
recipient,
mosaics,
nem.PlainMessage.create(msg),
netType
);
TxUtil.sendSinglesigTx(sender, transferTx, option.get('url'));