/
helpers.ts
130 lines (117 loc) · 3.99 KB
/
helpers.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import {
AuthMessage,
InitiateParameters,
PartialTransactionBody,
RedeemFees,
SwapTransactionStatus,
} from "./types";
export abstract class Helpers {
/**
* Get the details needed for `getAuthToken` request
*
* @remarks the `msgToSign` value needs to be signed before being used for Auth
* @param message message to include for the Atomex Authentication message
* @param address required for Tezos blockchain, in order to determine the EC algorithm
* @returns details required for Atomex Auth
*/
public abstract getAuthMessage(
message: string,
address?: string,
): AuthMessage;
/**
* Get the tx data for Atomex Contract Initiate Swap call
*
* @param swapDetails details of the swap being initiated
* @returns contract address and tx data that can be used to make a contract call
*/
public abstract buildInitiateTransaction(
initiateParameters: InitiateParameters,
): PartialTransactionBody;
/**
* Get the tx data for Atomex Contract Redeem Swap call
*
* @param secret secret that can used to verify and redeem the funds
* @returns contract address and tx data that can be used to make a contract call
*/
public abstract buildRedeemTransaction(
secret: string,
hashedSecret: string,
): PartialTransactionBody;
/**
* Get the tx data for Atomex Contract Refund Swap call
*
* @param secretHash secretHash to identify swap
* @returns contract address and tx data that can be used to make a contract call
*/
public abstract buildRefundTransaction(
secretHash: string,
): PartialTransactionBody;
/**
* Get the tx data for Atomex Contract AdditionalFunds call
*
* @param secretHash secretHash to identify swap
* @returns contract address and tx data that can be used to make a contract call
*/
public abstract buildAddTransaction(
secretHash: string,
amount: number,
): PartialTransactionBody;
/**
* Validate the Swap Details on chain using the tx detail from Atomex
* [does not check tx status, use status provided by atomex]
*
* @param blockHeight block height of the block where the tx is present
* @param txID operation/tx hash to identify blockchain transaction
* @param expectedData expected swap details that will be used for validation
* @param confirmations no. of tx confirmations required
* @returns status of tx, current no. of confirms and est. next block generation timestamp.
* No. of confirmations and block timestamp is only returned when `status:Included`
*/
public abstract validateInitiateTransaction(
blockHeight: number,
txID: string,
secretHash: string,
receivingAddress: string,
netAmount: number,
minRefundTimestamp: number,
minConfirmations: number,
): Promise<SwapTransactionStatus>;
/**
* Encodes Signature in a form compliant with Atomex
*
* @param signature signature to encode
* @returns encoded signature
*/
public abstract encodeSignature(signature: string): string;
/**
* Encodes Public Key in a form compliant with Atomex
*
* @param publicKey public key
* @returns encoded public key
*/
public abstract encodePublicKey(publicKey: string): string;
/**
* Estimates Initiate fees for a Swap
*
* @param source the initiator address
* @returns the initiate fees for Swap
*/
public abstract estimateInitiateFees(source: string): Promise<number>;
/**
* Estimates Miner Fee and Reward for Redeem to be used in a Swap
*
* @param recipient the counter-party address
* @returns the minerFee and the rewardForRedeem
*/
public abstract estimateRedeemFees(recipient: string): Promise<RedeemFees>;
/**
* Check validity of an account address
*
* @param address account address
* @returns true if valid, else false
*/
public abstract isValidAddress(address: string): boolean;
}
export const dt2ts = (isoTime: Date | string): number =>
Math.round(new Date(isoTime).getTime() / 1000);
export const now = (): number => Math.round(Date.now() / 1000);