Skip to content

Commit f9b1aea

Browse files
authored
BREAKING CHANGE: add MakeCallTx, SignTx and BroadcastTxCommit in GnoNativeApi (#180)
This PR adds MakeCallTx, SignTx and BroadcastTxCommit in GnoNativeApi. So the front can directly calls these methods now. This PR also cleans the GnoNativeApi files, mostly by removing unnecessary `await` keywords. BREAKING CHANGE: the gasWanted parameter (used in several gRPC message) is now a bigint instead of a number to keep the precision in the calling stack. --------- Signed-off-by: D4ryl00 <[email protected]>
1 parent ecdd187 commit f9b1aea

File tree

9 files changed

+412
-165
lines changed

9 files changed

+412
-165
lines changed

examples/js/expo/gnoboard/src/GoBridge/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type GnoConfig = {
66
KeyName: string;
77
Password: string;
88
GasFee: string;
9-
GasWanted: number;
9+
GasWanted: bigint;
1010
Mnemonic: string;
1111
};
1212

examples/js/expo/gnoboard/src/api/GnoNativeApi.ts

+121-53
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AddressFromBech32Request,
33
AddressToBech32Request,
44
AddressFromMnemonicRequest,
5+
BroadcastTxCommitResponse,
56
CallRequest,
67
CallResponse,
78
CreateAccountRequest,
@@ -18,11 +19,13 @@ import {
1819
GetKeyInfoByNameRequest,
1920
GetRemoteRequest,
2021
HasKeyByAddressRequest,
22+
HasKeyByNameOrAddressRequest,
2123
HasKeyByNameRequest,
2224
HelloRequest,
2325
HelloStreamResponse,
2426
KeyInfo,
2527
ListKeyInfoRequest,
28+
MakeTxResponse,
2629
MsgCall,
2730
MsgSend,
2831
QEvalRequest,
@@ -43,6 +46,7 @@ import {
4346
SetPasswordResponse,
4447
SetRemoteRequest,
4548
SetRemoteResponse,
49+
SignTxResponse,
4650
UpdatePasswordRequest,
4751
UpdatePasswordResponse,
4852
} from '@buf/gnolang_gnonative.bufbuild_es/gnonativetypes_pb';
@@ -81,7 +85,9 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
8185

8286
try {
8387
await this.clientInstance.setRemote(new SetRemoteRequest({ remote: this.config.remote }));
84-
await this.clientInstance.setChainID(new SetChainIDRequest({ chainId: this.config.chain_id }));
88+
await this.clientInstance.setChainID(
89+
new SetChainIDRequest({ chainId: this.config.chain_id }),
90+
);
8591
console.log('✅ GnoNative bridge initialized.');
8692
if (this.config.start_gnokey_mobile_service) {
8793
await this.startGnokeyMobileService();
@@ -113,31 +119,46 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
113119
}
114120

115121
async setRemote(remote: string): Promise<SetRemoteResponse> {
116-
const client = await this.#getClient();
117-
const response = await client.setRemote(new SetRemoteRequest({ remote }));
122+
const client = this.#getClient();
123+
const response = client.setRemote(new SetRemoteRequest({ remote }));
118124
return response;
119125
}
120126

121127
async getRemote(): Promise<string> {
122-
const client = await this.#getClient();
128+
const client = this.#getClient();
123129
const response = await client.getRemote(new GetRemoteRequest());
124130
return response.remote;
125131
}
126132

133+
async signTx(
134+
txJson: string,
135+
address: Uint8Array,
136+
accountNumber: bigint = BigInt(0),
137+
sequenceNumber: bigint = BigInt(0),
138+
): Promise<SignTxResponse> {
139+
const client = this.#getClient();
140+
const response = client.signTx({ txJson, address, accountNumber, sequenceNumber });
141+
return response;
142+
}
143+
127144
async setChainID(chainId: string): Promise<SetChainIDResponse> {
128-
const client = await this.#getClient();
129-
const response = await client.setChainID(new SetChainIDRequest({ chainId }));
145+
const client = this.#getClient();
146+
const response = client.setChainID(new SetChainIDRequest({ chainId }));
130147
return response;
131148
}
132149

133150
async getChainID() {
134-
const client = await this.#getClient();
151+
const client = this.#getClient();
135152
const response = await client.getChainID(new GetChainIDRequest());
136153
return response.chainId;
137154
}
138155

139-
async createAccount(nameOrBech32: string, mnemonic: string, password: string): Promise<KeyInfo | undefined> {
140-
const client = await this.#getClient();
156+
async createAccount(
157+
nameOrBech32: string,
158+
mnemonic: string,
159+
password: string,
160+
): Promise<KeyInfo | undefined> {
161+
const client = this.#getClient();
141162
const reponse = await client.createAccount(
142163
new CreateAccountRequest({
143164
nameOrBech32,
@@ -149,56 +170,88 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
149170
}
150171

151172
async generateRecoveryPhrase() {
152-
const client = await this.#getClient();
173+
const client = this.#getClient();
153174
const response = await client.generateRecoveryPhrase(new GenerateRecoveryPhraseRequest());
154175
return response.phrase;
155176
}
156177

157178
async hasKeyByName(name: string) {
158-
const client = await this.#getClient();
179+
const client = this.#getClient();
159180
const response = await client.hasKeyByName(new HasKeyByNameRequest({ name }));
160181
return response.has;
161182
}
162183

163184
async hasKeyByAddress(address: Uint8Array) {
164-
const client = await this.#getClient();
185+
const client = this.#getClient();
165186
const response = await client.hasKeyByAddress(new HasKeyByAddressRequest({ address }));
166187
return response.has;
167188
}
168189

169190
async hasKeyByNameOrAddress(nameOrBech32: string) {
170-
const client = await this.#getClient();
171-
const response = await client.hasKeyByNameOrAddress(new HasKeyByNameOrAddressRequest({ nameOrBech32 }));
191+
const client = this.#getClient();
192+
const response = await client.hasKeyByNameOrAddress(
193+
new HasKeyByNameOrAddressRequest({ nameOrBech32 }),
194+
);
172195
return response.has;
173196
}
174197

175198
async getKeyInfoByName(name: string): Promise<KeyInfo | undefined> {
176-
const client = await this.#getClient();
199+
const client = this.#getClient();
177200
const response = await client.getKeyInfoByName(new GetKeyInfoByNameRequest({ name }));
178201
return response.key;
179202
}
180203

181204
async getKeyInfoByAddress(address: Uint8Array): Promise<KeyInfo | undefined> {
182-
const client = await this.#getClient();
205+
const client = this.#getClient();
183206
const response = await client.getKeyInfoByAddress(new GetKeyInfoByAddressRequest({ address }));
184207
return response.key;
185208
}
186209

187210
async getKeyInfoByNameOrAddress(nameOrBech32: string): Promise<KeyInfo | undefined> {
188-
const client = await this.#getClient();
189-
const response = await client.getKeyInfoByNameOrAddress(new GetKeyInfoByNameOrAddressRequest({ nameOrBech32 }));
211+
const client = this.#getClient();
212+
const response = await client.getKeyInfoByNameOrAddress(
213+
new GetKeyInfoByNameOrAddressRequest({ nameOrBech32 }),
214+
);
190215
return response.key;
191216
}
192217

193218
async listKeyInfo(): Promise<KeyInfo[]> {
194-
const client = await this.#getClient();
219+
const client = this.#getClient();
195220
const response = await client.listKeyInfo(new ListKeyInfoRequest());
196221
return response.keys;
197222
}
198223

224+
async makeCallTx(
225+
packagePath: string,
226+
fnc: string,
227+
args: string[],
228+
gasFee: string,
229+
gasWanted: bigint,
230+
callerAddress?: Uint8Array,
231+
send?: string,
232+
memo?: string,
233+
): Promise<MakeTxResponse> {
234+
const client = this.#getClient();
235+
const reponse = client.makeCallTx({
236+
gasFee,
237+
gasWanted,
238+
memo,
239+
callerAddress,
240+
msgs: [
241+
{
242+
packagePath,
243+
fnc,
244+
args,
245+
send,
246+
},
247+
],
248+
});
249+
return reponse;
250+
}
251+
199252
async selectAccount(nameOrBech32: string): Promise<SelectAccountResponse> {
200-
const client = await this.#getClient();
201-
const response = await client.selectAccount(
253+
const client = this.#getClient();
254+
const response = client.selectAccount(
202255
new SelectAccountRequest({
203256
nameOrBech32,
204257
}),
@@ -207,8 +260,8 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
207260
}
208261

209262
async activateAccount(nameOrBech32: string): Promise<ActivateAccountResponse> {
210-
const client = await this.#getClient();
211-
const response = await client.activateAccount(
263+
const client = this.#getClient();
264+
const response = client.activateAccount(
212265
new ActivateAccountRequest({
213266
nameOrBech32,
214267
}),
@@ -228,38 +281,45 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
228281
}
229282

230283
async setPassword(password: string, address?: Uint8Array): Promise<SetPasswordResponse> {
231-
const client = await this.#getClient();
232-
const response = await client.setPassword(new SetPasswordRequest({ password, address }));
284+
const client = this.#getClient();
285+
const response = client.setPassword(new SetPasswordRequest({ password, address }));
233286
return response;
234287
}
235288

236-
async updatePassword(newPassword: string, address?: Uint8Array): Promise<UpdatePasswordResponse> {
237-
const client = await this.#getClient();
238-
const response = await client.updatePassword(new UpdatePasswordRequest({ newPassword, address }));
289+
async updatePassword(
290+
newPassword: string,
291+
addresses: Uint8Array[],
292+
): Promise<UpdatePasswordResponse> {
293+
const client = this.#getClient();
294+
const response = client.updatePassword(new UpdatePasswordRequest({ newPassword, addresses }));
239295
return response;
240296
}
241297

242298
async getActiveAccount(): Promise<GetActiveAccountResponse> {
243-
const client = await this.#getClient();
244-
const response = await client.getActiveAccount(new GetActiveAccountRequest());
299+
const client = this.#getClient();
300+
const response = client.getActiveAccount(new GetActiveAccountRequest());
245301
return response;
246302
}
247303

248304
async getActivatedAccount(): Promise<GetActivatedAccountResponse> {
249-
const client = await this.#getClient();
250-
const response = await client.getActivatedAccount(new GetActivatedAccountRequest());
305+
const client = this.#getClient();
306+
const response = client.getActivatedAccount(new GetActivatedAccountRequest());
251307
return response;
252308
}
253309

254310
async queryAccount(address: Uint8Array): Promise<QueryAccountResponse> {
255-
const client = await this.#getClient();
256-
const reponse = await client.queryAccount(new QueryAccountRequest({ address }));
311+
const client = this.#getClient();
312+
const reponse = client.queryAccount(new QueryAccountRequest({ address }));
257313
return reponse;
258314
}
259315

260-
async deleteAccount(nameOrBech32: string, password: string | undefined, skipPassword: boolean): Promise<DeleteAccountResponse> {
261-
const client = await this.#getClient();
262-
const response = await client.deleteAccount(
316+
async deleteAccount(
317+
nameOrBech32: string,
318+
password: string | undefined,
319+
skipPassword: boolean,
320+
): Promise<DeleteAccountResponse> {
321+
const client = this.#getClient();
322+
const response = client.deleteAccount(
263323
new DeleteAccountRequest({
264324
nameOrBech32,
265325
password,
@@ -270,8 +330,8 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
270330
}
271331

272332
async query(path: string, data: Uint8Array): Promise<QueryResponse> {
273-
const client = await this.#getClient();
274-
const reponse = await client.query(
333+
const client = this.#getClient();
334+
const reponse = client.query(
275335
new QueryRequest({
276336
path,
277337
data,
@@ -281,7 +341,7 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
281341
}
282342

283343
async render(packagePath: string, args: string) {
284-
const client = await this.#getClient();
344+
const client = this.#getClient();
285345
const reponse = await client.render(
286346
new RenderRequest({
287347
packagePath,
@@ -292,7 +352,7 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
292352
}
293353

294354
async qEval(packagePath: string, expression: string) {
295-
const client = await this.#getClient();
355+
const client = this.#getClient();
296356
const reponse = await client.qEval(
297357
new QEvalRequest({
298358
packagePath,
@@ -307,16 +367,16 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
307367
fnc: string,
308368
args: string[],
309369
gasFee: string,
310-
gasWanted: number,
370+
gasWanted: bigint,
311371
callerAddress?: Uint8Array,
312372
send?: string,
313373
memo?: string,
314374
): Promise<AsyncIterable<CallResponse>> {
315-
const client = await this.#getClient();
375+
const client = this.#getClient();
316376
const reponse = client.call(
317377
new CallRequest({
318378
gasFee,
319-
gasWanted: BigInt(gasWanted),
379+
gasWanted,
320380
memo,
321381
callerAddress,
322382
msgs: [
@@ -336,15 +396,15 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
336396
toAddress: Uint8Array,
337397
send: string,
338398
gasFee: string,
339-
gasWanted: number,
399+
gasWanted: bigint,
340400
callerAddress?: Uint8Array,
341401
memo?: string,
342402
): Promise<AsyncIterable<SendResponse>> {
343-
const client = await this.#getClient();
403+
const client = this.#getClient();
344404
const reponse = client.send(
345405
new SendRequest({
346406
gasFee,
347-
gasWanted: BigInt(gasWanted),
407+
gasWanted,
348408
memo,
349409
callerAddress,
350410
msgs: [
@@ -359,33 +419,41 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
359419
}
360420

361421
async addressToBech32(address: Uint8Array) {
362-
const client = await this.#getClient();
422+
const client = this.#getClient();
363423
const response = await client.addressToBech32(new AddressToBech32Request({ address }));
364424
return response.bech32Address;
365425
}
366426

367427
async addressFromMnemonic(mnemonic: string) {
368-
const client = await this.#getClient();
428+
const client = this.#getClient();
369429
const response = await client.addressFromMnemonic(new AddressFromMnemonicRequest({ mnemonic }));
370430
return response.address;
371431
}
372432

373433
async addressFromBech32(bech32Address: string) {
374-
const client = await this.#getClient();
375-
const response = await client.addressFromBech32(new AddressFromBech32Request({ bech32Address }));
434+
const client = this.#getClient();
435+
const response = await client.addressFromBech32(
436+
new AddressFromBech32Request({ bech32Address }),
437+
);
376438
return response.address;
377439
}
378440

441+
async broadcastTxCommit(signedTxJson: string): Promise<AsyncIterable<BroadcastTxCommitResponse>> {
442+
const client = this.#getClient();
443+
const response = client.broadcastTxCommit({ signedTxJson });
444+
return response;
445+
}
446+
379447
// // debug
380448
async hello(name: string) {
381-
const client = await this.#getClient();
449+
const client = this.#getClient();
382450
const response = await client.hello(new HelloRequest({ name }));
383451
return response.greeting;
384452
}
385453

386454
// // debug
387455
async helloStream(name: string): Promise<AsyncIterable<HelloStreamResponse>> {
388-
const client = await this.#getClient();
456+
const client = this.#getClient();
389457
return client.helloStream(new HelloRequest({ name }));
390458
}
391459

0 commit comments

Comments
 (0)