/
transfer.ts
43 lines (37 loc) · 1.58 KB
/
transfer.ts
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
/**
* @module index
*/
import { TRANSACTION_TYPE, ITransferTransaction, ITransferParams, WithId, WithSender } from '../transactions'
import { signBytes, blake2b, base58Encode } from '@acryl/ts-lib-crypto'
import { addProof, getSenderPublicKey, convertToPairs, fee, normalizeAssetId } from '../generic'
import { validate } from '../validators'
import { TSeedTypes } from '../types'
import { binary } from '@acryl/marshall'
/* @echo DOCS */
export function transfer(params: ITransferParams, seed: TSeedTypes): ITransferTransaction & WithId
export function transfer(paramsOrTx: ITransferParams & WithSender | ITransferTransaction, seed?: TSeedTypes): ITransferTransaction & WithId
export function transfer(paramsOrTx: any, seed?: TSeedTypes): ITransferTransaction {
const type = TRANSACTION_TYPE.TRANSFER
const version = paramsOrTx.version || 2
const seedsAndIndexes = convertToPairs(seed)
const senderPublicKey = getSenderPublicKey(seedsAndIndexes, paramsOrTx)
const tx: ITransferTransaction & WithId = {
type,
version,
senderPublicKey,
assetId: normalizeAssetId(paramsOrTx.assetId),
recipient: paramsOrTx.recipient,
amount: paramsOrTx.amount,
attachment: paramsOrTx.attachment || '',
fee: fee(paramsOrTx, 100000),
feeAssetId: normalizeAssetId(paramsOrTx.feeAssetId),
timestamp: paramsOrTx.timestamp || Date.now(),
proofs: paramsOrTx.proofs || [],
id: '',
}
validate.transfer(tx)
const bytes = binary.serializeTx(tx)
seedsAndIndexes.forEach(([s, i]) => addProof(tx, signBytes(s, bytes), i))
tx.id = base58Encode(blake2b(bytes))
return tx
}