-
Notifications
You must be signed in to change notification settings - Fork 21
/
signWithEckoWallet.ts
62 lines (53 loc) · 1.86 KB
/
signWithEckoWallet.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
import { pactCommandToSigningRequest } from '../utils/pactCommandToSigningRequest';
import { parseTransactionCommand } from '../utils/parseTransactionCommand';
import { connect, isConnected, isInstalled } from './eckoCommon';
import type { IEckoSignResponse, IEckoSignSingleFunction } from './eckoTypes';
declare global {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Window {
kadena?: {
isKadena: boolean;
request<T>(args: unknown): Promise<T>;
};
}
}
/**
* Creates the signWithEckoWallet function with interface {@link ISingleSignFunction}
*
* @remarks
* It is preferred to use the {@link createEckoWalletQuicksign} function
*
* @public
*/
export function createSignWithEckoWallet(): IEckoSignSingleFunction {
const signWithEckoWallet: IEckoSignSingleFunction = async (transaction) => {
const parsedTransaction = parseTransactionCommand(transaction);
const signingRequest = pactCommandToSigningRequest(parsedTransaction);
await connect(parsedTransaction.networkId);
const response = await window.kadena?.request<IEckoSignResponse>({
method: 'kda_requestSign',
data: {
networkId: parsedTransaction.networkId,
signingCmd: signingRequest,
},
});
if (response?.signedCmd === undefined) {
throw new Error('Error signing transaction');
}
return response.signedCmd;
};
signWithEckoWallet.isInstalled = isInstalled;
signWithEckoWallet.isConnected = isConnected;
signWithEckoWallet.connect = connect;
return signWithEckoWallet;
}
/**
* Creates the signWithEckoWallet function with interface {@link ISingleSignFunction}
*
* @remarks
* It is preferred to use the {@link createQuicksignWithEckoWallet} function
*
* @deprecated Use {@link createSignWithEckoWallet} instead
* @public
*/
export const createEckoWalletSign = createSignWithEckoWallet;