-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtransaction.ts
More file actions
151 lines (124 loc) · 3.94 KB
/
transaction.ts
File metadata and controls
151 lines (124 loc) · 3.94 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @ts-ignore
import { withPrefix, sansPrefix } from '@onflow/util-address';
import { CadenceSourceCode, resolveCadence, Arguments, resolveArgs } from './cadence';
import { FCLTransaction } from './fcl';
const toHex = (buffer: Buffer) => buffer.toString('hex');
const fromHex = (hex: string) => Buffer.from(hex, 'hex');
export interface Signer {
sign(message: Buffer): Buffer;
}
export interface TransactionResult {
transactionId: string;
events: TransactionEvent[];
}
export type TransactionEvent = {
type: string;
data: { [key: string]: string };
};
export enum TransactionStatus {
UNKNOWN = 'UNKNOWN',
SUBMITTED = 'SUBMITTED',
PENDING = 'PENDING',
EXECUTED = 'EXECUTED',
SEALED = 'SEALED',
}
export class TransactionAuthorizer {
address: string;
keyIndex: number;
signer: Signer;
constructor({ address, keyIndex, signer }: { address: string; keyIndex: number; signer: Signer }) {
this.address = address;
this.keyIndex = keyIndex;
this.signer = signer;
}
toFCLAuthorizationFunction() {
return async (account = {}) => {
return {
...account,
tempId: `${withPrefix(this.address)}-${this.keyIndex}`,
addr: sansPrefix(this.address),
keyId: this.keyIndex,
signingFunction: (data: { message: string }) => ({
addr: withPrefix(this.address),
keyId: this.keyIndex,
signature: toHex(this.signer.sign(fromHex(data.message))),
}),
};
};
}
}
export type TransactionResultTransformer<T> = (result: TransactionResult) => T;
export type TransactionSigners = {
payer: TransactionAuthorizer;
proposer: TransactionAuthorizer;
authorizers: TransactionAuthorizer[];
};
export type TransactionParameters = {
cadence: CadenceSourceCode;
args?: Arguments;
computeLimit?: number;
signers?: TransactionSigners;
};
export function isTransactionParameters(transaction: any): transaction is TransactionParameters {
return (transaction as TransactionParameters).cadence !== undefined;
}
export class Transaction<T = TransactionResult> {
static defaultComputeLimit = 1000;
cadence: CadenceSourceCode;
args?: Arguments;
computeLimit?: number;
signers?: TransactionSigners;
private _onResult: TransactionResultTransformer<T>;
/* eslint-disable @typescript-eslint/no-empty-function */
static VoidResult = () => {};
constructor(
{ cadence, args, computeLimit, signers }: TransactionParameters,
onResult: TransactionResultTransformer<T> = (result: TransactionResult) => result as T,
) {
this.cadence = cadence;
this.args = args;
this.computeLimit = computeLimit;
this.signers = signers;
this._onResult = onResult;
}
onResult<T>(onResult: TransactionResultTransformer<T>): Transaction<T> {
return new Transaction<T>(
{
cadence: this.cadence,
args: this.args,
computeLimit: this.computeLimit,
signers: this.signers,
},
onResult,
);
}
async toFCLTransaction(network: string): Promise<FCLTransaction> {
const limit = this.computeLimit ?? Transaction.defaultComputeLimit;
const cadence = resolveCadence(this.cadence, network);
const args = await resolveArgs(cadence, this.args ?? []);
return {
cadence,
args,
limit,
...this.getAuthorizations(this.signers),
};
}
async transformResult(result: TransactionResult): Promise<T> {
return await this._onResult(result);
}
private getAuthorizations(signers: TransactionSigners | undefined) {
if (signers) {
return {
payer: signers.payer.toFCLAuthorizationFunction(),
proposer: signers.proposer.toFCLAuthorizationFunction(),
authorizations: signers.authorizers.map((auth) => auth.toFCLAuthorizationFunction()),
};
}
return {};
}
}
class TransactionError extends Error {}
export function convertToTransactionError(error: any): Error {
// TODO: parse error and convert to correct error class
return new TransactionError(error);
}