-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTransaction.ts
81 lines (73 loc) · 2.22 KB
/
Transaction.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
import {
schemaTransactionSign,
schemaTransactionSignAndBroadcast,
serializeTransaction,
Transaction,
TransactionSign,
TransactionSignAndBroadcast,
} from "@ledgerhq/wallet-api-core";
import type { WalletAPIClient } from "../WalletAPIClient";
export class TransactionModule {
private client: WalletAPIClient;
constructor(client: WalletAPIClient) {
this.client = client;
}
/**
* Let the user sign a transaction that won't be broadcasted by the connected wallet
* @param accountId - id of the account
* @param transaction - The transaction object in the currency family-specific format
* @param options - Extra parameters
*
* @returns The raw signed transaction
* @throws {@link RpcError} if an error occurred on server side
*/
async sign(
accountId: string,
transaction: Transaction,
options?: TransactionSign["params"]["options"],
meta?: Record<string, unknown>,
): Promise<Buffer> {
const transactionSignResult = await this.client.request(
"transaction.sign",
{
accountId,
rawTransaction: serializeTransaction(transaction),
options,
meta,
},
);
const safeResults = schemaTransactionSign.result.parse(
transactionSignResult,
);
return Buffer.from(safeResults.signedTransactionHex, "hex");
}
/**
* Let the user sign and broadcast a transaction
* @param accountId - id of the account
* @param transaction - The transaction object in the currency family-specific format
* @param options - Extra parameters
*
* @returns The transaction hash
* @throws {@link RpcError} if an error occurred on server side
*/
async signAndBroadcast(
accountId: string,
transaction: Transaction,
options?: TransactionSignAndBroadcast["params"]["options"],
meta?: Record<string, unknown>,
): Promise<string> {
const transactionSignResult = await this.client.request(
"transaction.signAndBroadcast",
{
accountId,
rawTransaction: serializeTransaction(transaction),
options,
meta,
},
);
const safeResults = schemaTransactionSignAndBroadcast.result.parse(
transactionSignResult,
);
return safeResults.transactionHash;
}
}