@gnolang/gnonative 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +5 -0
- package/.prettierrc.js +12 -0
- package/Makefile +62 -0
- package/README.md +118 -0
- package/android/build.gradle +93 -0
- package/android/libs/gnocore-sources.jar +0 -0
- package/android/libs/gnocore.aar +0 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/land/gno/gnonative/GnonativeModule.kt +150 -0
- package/android/src/main/java/land/gno/gnonative/GnonativeView.kt +7 -0
- package/android/src/main/java/land/gno/gnonative/GoBridgeError.kt +6 -0
- package/android/src/main/java/land/gno/gnonative/JavaPromiseBlock.kt +42 -0
- package/build/Gnonative.types.d.ts +7 -0
- package/build/Gnonative.types.d.ts.map +1 -0
- package/build/Gnonative.types.js +2 -0
- package/build/Gnonative.types.js.map +1 -0
- package/build/GnonativeModule.d.ts +3 -0
- package/build/GnonativeModule.d.ts.map +1 -0
- package/build/GnonativeModule.js +5 -0
- package/build/GnonativeModule.js.map +1 -0
- package/build/GnonativeModule.web.d.ts +7 -0
- package/build/GnonativeModule.web.d.ts.map +1 -0
- package/build/GnonativeModule.web.js +12 -0
- package/build/GnonativeModule.web.js.map +1 -0
- package/build/GnonativeView.d.ts +4 -0
- package/build/GnonativeView.d.ts.map +1 -0
- package/build/GnonativeView.js +7 -0
- package/build/GnonativeView.js.map +1 -0
- package/build/GnonativeView.web.d.ts +4 -0
- package/build/GnonativeView.web.d.ts.map +1 -0
- package/build/GnonativeView.web.js +7 -0
- package/build/GnonativeView.web.js.map +1 -0
- package/build/GoBridge.d.ts +12 -0
- package/build/GoBridge.d.ts.map +1 -0
- package/build/GoBridge.js +27 -0
- package/build/GoBridge.js.map +1 -0
- package/build/grpc/client.d.ts +4 -0
- package/build/grpc/client.d.ts.map +1 -0
- package/build/grpc/client.js +12 -0
- package/build/grpc/client.js.map +1 -0
- package/build/grpc/error.d.ts +15 -0
- package/build/grpc/error.d.ts.map +1 -0
- package/build/grpc/error.js +70 -0
- package/build/grpc/error.js.map +1 -0
- package/build/grpc/transport_native.d.ts +4 -0
- package/build/grpc/transport_native.d.ts.map +1 -0
- package/build/grpc/transport_native.js +140 -0
- package/build/grpc/transport_native.js.map +1 -0
- package/build/hooks/types.d.ts +17 -0
- package/build/hooks/types.d.ts.map +1 -0
- package/build/hooks/types.js +2 -0
- package/build/hooks/types.js.map +1 -0
- package/build/hooks/use-gno.d.ts +35 -0
- package/build/hooks/use-gno.d.ts.map +1 -0
- package/build/hooks/use-gno.js +248 -0
- package/build/hooks/use-gno.js.map +1 -0
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +13 -0
- package/build/index.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/Frameworks/GnoCore.xcframework/Info.plist +44 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Resources/Info.plist +6 -0
- package/ios/GnoError.swift +31 -0
- package/ios/Gnonative.podspec +28 -0
- package/ios/GnonativeModule.swift +186 -0
- package/ios/GnonativeView.swift +7 -0
- package/ios/PromiseBlock.swift +45 -0
- package/package.json +50 -0
- package/src/Gnonative.types.ts +7 -0
- package/src/GnonativeModule.ts +5 -0
- package/src/GnonativeModule.web.ts +13 -0
- package/src/GnonativeView.tsx +11 -0
- package/src/GnonativeView.web.tsx +11 -0
- package/src/GoBridge.ts +44 -0
- package/src/grpc/client.ts +14 -0
- package/src/grpc/error.ts +89 -0
- package/src/grpc/transport_native.ts +197 -0
- package/src/hooks/types.ts +19 -0
- package/src/hooks/use-gno.ts +423 -0
- package/src/index.ts +20 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { AnyMessage, MethodInfo, PartialMessage, ServiceType } from '@bufbuild/protobuf';
|
|
2
|
+
import { Message, MethodKind } from '@bufbuild/protobuf';
|
|
3
|
+
import type {
|
|
4
|
+
ContextValues,
|
|
5
|
+
StreamResponse,
|
|
6
|
+
Transport,
|
|
7
|
+
UnaryRequest,
|
|
8
|
+
UnaryResponse,
|
|
9
|
+
} from '@connectrpc/connect';
|
|
10
|
+
import { createContextValues } from '@connectrpc/connect';
|
|
11
|
+
import {
|
|
12
|
+
createClientMethodSerializers,
|
|
13
|
+
createMethodUrl,
|
|
14
|
+
runStreamingCall,
|
|
15
|
+
runUnaryCall,
|
|
16
|
+
} from '@connectrpc/connect/protocol';
|
|
17
|
+
import { requestHeader } from '@connectrpc/connect/protocol-grpc-web';
|
|
18
|
+
import { GrpcWebTransportOptions } from '@connectrpc/connect-web';
|
|
19
|
+
import { CodedError } from 'expo-modules-core';
|
|
20
|
+
import { decode } from 'js-base64';
|
|
21
|
+
|
|
22
|
+
import { GoBridge } from '../GoBridge';
|
|
23
|
+
|
|
24
|
+
function base64ToBytes(base64: string): Uint8Array {
|
|
25
|
+
const binString = decode(base64);
|
|
26
|
+
const bytes = new Uint8Array(binString.length);
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < binString.length; i++) {
|
|
29
|
+
bytes[i] = binString.charCodeAt(i);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return bytes;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createNativeGrpcTransport(options: GrpcWebTransportOptions): Transport {
|
|
36
|
+
const useBinaryFormat = options.useBinaryFormat ?? true;
|
|
37
|
+
return {
|
|
38
|
+
async unary<I extends Message<I> = AnyMessage, O extends Message<O> = AnyMessage>(
|
|
39
|
+
service: ServiceType,
|
|
40
|
+
method: MethodInfo<I, O>,
|
|
41
|
+
signal: AbortSignal | undefined,
|
|
42
|
+
timeoutMs: number | undefined,
|
|
43
|
+
header: HeadersInit | undefined,
|
|
44
|
+
message: PartialMessage<I>,
|
|
45
|
+
contextValues?: ContextValues,
|
|
46
|
+
): Promise<UnaryResponse<I, O>> {
|
|
47
|
+
const { parse } = createClientMethodSerializers(
|
|
48
|
+
method,
|
|
49
|
+
useBinaryFormat,
|
|
50
|
+
options.jsonOptions,
|
|
51
|
+
options.binaryOptions,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return await runUnaryCall<I, O>({
|
|
55
|
+
signal,
|
|
56
|
+
interceptors: options.interceptors,
|
|
57
|
+
req: {
|
|
58
|
+
stream: false,
|
|
59
|
+
service,
|
|
60
|
+
method,
|
|
61
|
+
url: createMethodUrl(options.baseUrl, service, method),
|
|
62
|
+
init: {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
mode: 'cors',
|
|
65
|
+
},
|
|
66
|
+
header: new Headers(),
|
|
67
|
+
contextValues: contextValues ?? createContextValues(),
|
|
68
|
+
message,
|
|
69
|
+
},
|
|
70
|
+
next: async (req: UnaryRequest<I, O>): Promise<UnaryResponse<I, O>> => {
|
|
71
|
+
try {
|
|
72
|
+
const res = await GoBridge.invokeGrpcMethod(
|
|
73
|
+
req.method.name,
|
|
74
|
+
req.message.toJsonString(),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const header: Headers | undefined = new Headers();
|
|
78
|
+
const trailer: Headers | undefined = new Headers();
|
|
79
|
+
|
|
80
|
+
const data = base64ToBytes(res);
|
|
81
|
+
const message = parse(data);
|
|
82
|
+
|
|
83
|
+
return <UnaryResponse<I, O>>{
|
|
84
|
+
stream: false,
|
|
85
|
+
service,
|
|
86
|
+
method,
|
|
87
|
+
header,
|
|
88
|
+
message,
|
|
89
|
+
trailer,
|
|
90
|
+
};
|
|
91
|
+
} catch (e) {
|
|
92
|
+
console.log('next: unary call error:', e);
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
async stream<I extends Message<I> = AnyMessage, O extends Message<O> = AnyMessage>(
|
|
100
|
+
service: ServiceType,
|
|
101
|
+
method: MethodInfo<I, O>,
|
|
102
|
+
signal: AbortSignal | undefined,
|
|
103
|
+
timeoutMs: number | undefined,
|
|
104
|
+
header: HeadersInit | undefined,
|
|
105
|
+
input: AsyncIterable<PartialMessage<I>>,
|
|
106
|
+
contextValues?: ContextValues,
|
|
107
|
+
): Promise<StreamResponse<I, O>> {
|
|
108
|
+
const { parse } = createClientMethodSerializers(
|
|
109
|
+
method,
|
|
110
|
+
useBinaryFormat,
|
|
111
|
+
options.jsonOptions,
|
|
112
|
+
options.binaryOptions,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
async function createRequestBody(input: AsyncIterable<I>): Promise<string> {
|
|
116
|
+
if (method.kind !== MethodKind.ServerStreaming) {
|
|
117
|
+
throw 'The fetch API does not support streaming request bodies';
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const r = await input[Symbol.asyncIterator]().next();
|
|
121
|
+
if (r.done === true) {
|
|
122
|
+
throw 'missing request message';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return r.value.toJsonString();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return await runStreamingCall<I, O>({
|
|
129
|
+
interceptors: options.interceptors,
|
|
130
|
+
timeoutMs,
|
|
131
|
+
signal,
|
|
132
|
+
req: {
|
|
133
|
+
stream: true,
|
|
134
|
+
service,
|
|
135
|
+
method,
|
|
136
|
+
url: createMethodUrl(options.baseUrl, service, method),
|
|
137
|
+
init: {
|
|
138
|
+
method: 'POST',
|
|
139
|
+
credentials: options.credentials ?? 'same-origin',
|
|
140
|
+
mode: 'cors',
|
|
141
|
+
},
|
|
142
|
+
header: requestHeader(useBinaryFormat, timeoutMs, header, false),
|
|
143
|
+
message: input,
|
|
144
|
+
contextValues: contextValues ?? createContextValues(),
|
|
145
|
+
},
|
|
146
|
+
next: async req => {
|
|
147
|
+
const header: Headers | undefined = new Headers();
|
|
148
|
+
const trailer: Headers | undefined = new Headers();
|
|
149
|
+
|
|
150
|
+
const body = await createRequestBody(req.message);
|
|
151
|
+
|
|
152
|
+
let streamId: string;
|
|
153
|
+
try {
|
|
154
|
+
streamId = await GoBridge.createStreamClient(req.method.name, body);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
console.log('createStreamClient error:', e);
|
|
157
|
+
throw e;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const generator = {
|
|
161
|
+
async *[Symbol.asyncIterator]() {
|
|
162
|
+
for (; ;) {
|
|
163
|
+
try {
|
|
164
|
+
const res = await GoBridge.streamClientReceive(streamId);
|
|
165
|
+
const data = base64ToBytes(res);
|
|
166
|
+
const message = parse(data);
|
|
167
|
+
yield message;
|
|
168
|
+
} catch (e) {
|
|
169
|
+
// close the stream
|
|
170
|
+
try {
|
|
171
|
+
await GoBridge.closeStreamClient(streamId);
|
|
172
|
+
} catch (e) {
|
|
173
|
+
console.log('closeStreamClient error:', e);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!(e instanceof CodedError && e.message === 'EOF')) {
|
|
177
|
+
console.log('streamClientReceive error:', e);
|
|
178
|
+
throw e;
|
|
179
|
+
}
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const res: StreamResponse<I, O> = {
|
|
187
|
+
...req,
|
|
188
|
+
header,
|
|
189
|
+
trailer,
|
|
190
|
+
message: generator,
|
|
191
|
+
};
|
|
192
|
+
return res;
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { KeyInfo } from '@buf/gnolang_gnonative.bufbuild_es/gnonativetypes_pb';
|
|
2
|
+
|
|
3
|
+
export type GnoConfig = {
|
|
4
|
+
Remote: string;
|
|
5
|
+
ChainID: string;
|
|
6
|
+
KeyName: string;
|
|
7
|
+
Password: string;
|
|
8
|
+
GasFee: string;
|
|
9
|
+
GasWanted: number;
|
|
10
|
+
Mnemonic: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type NetworkMetainfo = {
|
|
14
|
+
chainId: string;
|
|
15
|
+
chainName: string;
|
|
16
|
+
gnoAddress: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type GnoAccount = KeyInfo;
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddressFromBech32Request,
|
|
3
|
+
AddressToBech32Request,
|
|
4
|
+
CallRequest,
|
|
5
|
+
CallResponse,
|
|
6
|
+
CreateAccountRequest,
|
|
7
|
+
DeleteAccountRequest,
|
|
8
|
+
DeleteAccountResponse,
|
|
9
|
+
GenerateRecoveryPhraseRequest,
|
|
10
|
+
GetActiveAccountRequest,
|
|
11
|
+
GetActiveAccountResponse,
|
|
12
|
+
GetChainIDRequest,
|
|
13
|
+
GetKeyInfoByAddressRequest,
|
|
14
|
+
GetKeyInfoByNameOrAddressRequest,
|
|
15
|
+
GetKeyInfoByNameRequest,
|
|
16
|
+
GetRemoteRequest,
|
|
17
|
+
HasKeyByAddressRequest,
|
|
18
|
+
HasKeyByNameOrAddressRequest,
|
|
19
|
+
HasKeyByNameRequest,
|
|
20
|
+
HelloRequest,
|
|
21
|
+
HelloStreamResponse,
|
|
22
|
+
ListKeyInfoRequest,
|
|
23
|
+
MsgCall,
|
|
24
|
+
MsgSend,
|
|
25
|
+
QEvalRequest,
|
|
26
|
+
QueryAccountRequest,
|
|
27
|
+
QueryAccountResponse,
|
|
28
|
+
QueryRequest,
|
|
29
|
+
QueryResponse,
|
|
30
|
+
RenderRequest,
|
|
31
|
+
SelectAccountRequest,
|
|
32
|
+
SelectAccountResponse,
|
|
33
|
+
SendRequest,
|
|
34
|
+
SendResponse,
|
|
35
|
+
SetChainIDRequest,
|
|
36
|
+
SetChainIDResponse,
|
|
37
|
+
SetPasswordRequest,
|
|
38
|
+
SetPasswordResponse,
|
|
39
|
+
SetRemoteRequest,
|
|
40
|
+
SetRemoteResponse,
|
|
41
|
+
} from '@buf/gnolang_gnonative.bufbuild_es/gnonativetypes_pb';
|
|
42
|
+
import { GnoNativeService } from '@buf/gnolang_gnonative.connectrpc_es/rpc_connect';
|
|
43
|
+
import { PromiseClient } from '@connectrpc/connect';
|
|
44
|
+
|
|
45
|
+
import { GnoAccount } from './types';
|
|
46
|
+
import { GoBridge } from '../GoBridge';
|
|
47
|
+
import * as Grpc from '../grpc/client';
|
|
48
|
+
|
|
49
|
+
export interface GnoResponse {
|
|
50
|
+
setRemote: (remote: string) => Promise<SetRemoteResponse>;
|
|
51
|
+
getRemote: () => Promise<string>;
|
|
52
|
+
setChainID: (chainId: string) => Promise<SetChainIDResponse>;
|
|
53
|
+
getChainID: () => Promise<string>;
|
|
54
|
+
createAccount: (
|
|
55
|
+
nameOrBech32: string,
|
|
56
|
+
mnemonic: string,
|
|
57
|
+
password: string,
|
|
58
|
+
) => Promise<GnoAccount | undefined>;
|
|
59
|
+
generateRecoveryPhrase: () => Promise<string>;
|
|
60
|
+
listKeyInfo: () => Promise<GnoAccount[]>;
|
|
61
|
+
hasKeyByName: (name: string) => Promise<boolean>;
|
|
62
|
+
hasKeyByAddress: (address: Uint8Array) => Promise<boolean>;
|
|
63
|
+
hasKeyByNameOrAddress: (nameOrBech32: string) => Promise<boolean>;
|
|
64
|
+
getKeyInfoByName: (name: string) => Promise<GnoAccount | undefined>;
|
|
65
|
+
getKeyInfoByAddress: (address: Uint8Array) => Promise<GnoAccount | undefined>;
|
|
66
|
+
getKeyInfoByNameOrAddress: (nameOrBech32: string) => Promise<GnoAccount | undefined>;
|
|
67
|
+
selectAccount: (nameOrBech32: string) => Promise<SelectAccountResponse>;
|
|
68
|
+
setPassword: (password: string) => Promise<SetPasswordResponse>;
|
|
69
|
+
getActiveAccount: () => Promise<GetActiveAccountResponse>;
|
|
70
|
+
queryAccount: (address: Uint8Array) => Promise<QueryAccountResponse>;
|
|
71
|
+
deleteAccount: (
|
|
72
|
+
nameOrBech32: string,
|
|
73
|
+
password: string | undefined,
|
|
74
|
+
skipPassword: boolean,
|
|
75
|
+
) => Promise<DeleteAccountResponse>;
|
|
76
|
+
query: (path: string, data: Uint8Array) => Promise<QueryResponse>;
|
|
77
|
+
render: (packagePath: string, args: string) => Promise<string>;
|
|
78
|
+
qEval: (packagePath: string, expression: string) => Promise<string>;
|
|
79
|
+
call: (
|
|
80
|
+
packagePath: string,
|
|
81
|
+
fnc: string,
|
|
82
|
+
args: Array<string>,
|
|
83
|
+
gasFee: string,
|
|
84
|
+
gasWanted: number,
|
|
85
|
+
send?: string,
|
|
86
|
+
memo?: string,
|
|
87
|
+
) => Promise<AsyncIterable<CallResponse>>;
|
|
88
|
+
send: (
|
|
89
|
+
toAddress: Uint8Array,
|
|
90
|
+
send: string,
|
|
91
|
+
gasFee: string,
|
|
92
|
+
gasWanted: number,
|
|
93
|
+
memo?: string,
|
|
94
|
+
) => Promise<AsyncIterable<SendResponse>>;
|
|
95
|
+
addressToBech32: (address: Uint8Array) => Promise<string>;
|
|
96
|
+
addressFromBech32: (bech32Address: string) => Promise<Uint8Array>;
|
|
97
|
+
closeBridge: () => Promise<void>;
|
|
98
|
+
initBridge: () => Promise<void>;
|
|
99
|
+
// debug
|
|
100
|
+
hello: (name: string) => Promise<string>;
|
|
101
|
+
helloStream: (name: string) => Promise<AsyncIterable<HelloStreamResponse>>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let clientInstance: PromiseClient<typeof GnoNativeService> | undefined = undefined;
|
|
105
|
+
let bridgeInstance: boolean = false;
|
|
106
|
+
|
|
107
|
+
export const useGno = (): GnoResponse => {
|
|
108
|
+
const getClient = async () => {
|
|
109
|
+
if (!bridgeInstance) {
|
|
110
|
+
await initBridge();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (clientInstance) return clientInstance;
|
|
114
|
+
|
|
115
|
+
console.log('Creating GRPC client instance...');
|
|
116
|
+
|
|
117
|
+
const port = await GoBridge.getTcpPort();
|
|
118
|
+
clientInstance = Grpc.createClient(port);
|
|
119
|
+
|
|
120
|
+
console.log('Creating GRPC client instance... done.');
|
|
121
|
+
|
|
122
|
+
// Set the initial configuration where it's different from the default.
|
|
123
|
+
await clientInstance.setRemote(new SetRemoteRequest({ remote: 'gno.land:26657' }));
|
|
124
|
+
await clientInstance.setChainID(new SetChainIDRequest({ chainId: 'portal-loop' }));
|
|
125
|
+
|
|
126
|
+
return clientInstance;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const closeBridge = async () => {
|
|
130
|
+
if (bridgeInstance) {
|
|
131
|
+
console.log('Closing bridge...');
|
|
132
|
+
await GoBridge.closeBridge();
|
|
133
|
+
console.log('Bridge closed.');
|
|
134
|
+
bridgeInstance = false;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const initBridge = async () => {
|
|
139
|
+
if (!bridgeInstance) {
|
|
140
|
+
console.log('Initializing bridge...');
|
|
141
|
+
await GoBridge.initBridge();
|
|
142
|
+
console.log('Bridge initialized.');
|
|
143
|
+
bridgeInstance = true;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const setRemote = async (remote: string) => {
|
|
148
|
+
const client = await getClient();
|
|
149
|
+
const response = await client.setRemote(new SetRemoteRequest({ remote }));
|
|
150
|
+
return response;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const getRemote = async () => {
|
|
154
|
+
const client = await getClient();
|
|
155
|
+
const response = await client.getRemote(new GetRemoteRequest());
|
|
156
|
+
return response.remote;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const setChainID = async (chainId: string) => {
|
|
160
|
+
const client = await getClient();
|
|
161
|
+
const response = await client.setChainID(new SetChainIDRequest({ chainId }));
|
|
162
|
+
return response;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const getChainID = async () => {
|
|
166
|
+
const client = await getClient();
|
|
167
|
+
const response = await client.getChainID(new GetChainIDRequest());
|
|
168
|
+
return response.chainId;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const createAccount = async (nameOrBech32: string, mnemonic: string, password: string) => {
|
|
172
|
+
const client = await getClient();
|
|
173
|
+
const reponse = await client.createAccount(
|
|
174
|
+
new CreateAccountRequest({
|
|
175
|
+
nameOrBech32,
|
|
176
|
+
mnemonic,
|
|
177
|
+
password,
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
return reponse.key;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const generateRecoveryPhrase = async () => {
|
|
184
|
+
const client = await getClient();
|
|
185
|
+
const response = await client.generateRecoveryPhrase(new GenerateRecoveryPhraseRequest());
|
|
186
|
+
return response.phrase;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const hasKeyByName = async (name: string) => {
|
|
190
|
+
const client = await getClient();
|
|
191
|
+
const response = await client.hasKeyByName(new HasKeyByNameRequest({ name }));
|
|
192
|
+
return response.has;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const hasKeyByAddress = async (address: Uint8Array) => {
|
|
196
|
+
const client = await getClient();
|
|
197
|
+
const response = await client.hasKeyByAddress(new HasKeyByAddressRequest({ address }));
|
|
198
|
+
return response.has;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const hasKeyByNameOrAddress = async (nameOrBech32: string) => {
|
|
202
|
+
const client = await getClient();
|
|
203
|
+
const response = await client.hasKeyByNameOrAddress(
|
|
204
|
+
new HasKeyByNameOrAddressRequest({ nameOrBech32 }),
|
|
205
|
+
);
|
|
206
|
+
return response.has;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const getKeyInfoByName = async (name: string) => {
|
|
210
|
+
const client = await getClient();
|
|
211
|
+
const response = await client.getKeyInfoByName(new GetKeyInfoByNameRequest({ name }));
|
|
212
|
+
return response.key;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const getKeyInfoByAddress = async (address: Uint8Array) => {
|
|
216
|
+
const client = await getClient();
|
|
217
|
+
const response = await client.getKeyInfoByAddress(new GetKeyInfoByAddressRequest({ address }));
|
|
218
|
+
return response.key;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const getKeyInfoByNameOrAddress = async (nameOrBech32: string) => {
|
|
222
|
+
const client = await getClient();
|
|
223
|
+
const response = await client.getKeyInfoByNameOrAddress(
|
|
224
|
+
new GetKeyInfoByNameOrAddressRequest({ nameOrBech32 }),
|
|
225
|
+
);
|
|
226
|
+
return response.key;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const listKeyInfo = async () => {
|
|
230
|
+
const client = await getClient();
|
|
231
|
+
const response = await client.listKeyInfo(new ListKeyInfoRequest());
|
|
232
|
+
return response.keys;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const selectAccount = async (nameOrBech32: string) => {
|
|
236
|
+
const client = await getClient();
|
|
237
|
+
const response = await client.selectAccount(
|
|
238
|
+
new SelectAccountRequest({
|
|
239
|
+
nameOrBech32,
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
return response;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const setPassword = async (password: string) => {
|
|
246
|
+
const client = await getClient();
|
|
247
|
+
const response = await client.setPassword(new SetPasswordRequest({ password }));
|
|
248
|
+
return response;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const getActiveAccount = async () => {
|
|
252
|
+
const client = await getClient();
|
|
253
|
+
const response = await client.getActiveAccount(new GetActiveAccountRequest());
|
|
254
|
+
return response;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const queryAccount = async (address: Uint8Array) => {
|
|
258
|
+
const client = await getClient();
|
|
259
|
+
const reponse = await client.queryAccount(new QueryAccountRequest({ address }));
|
|
260
|
+
return reponse;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const deleteAccount = async (
|
|
264
|
+
nameOrBech32: string,
|
|
265
|
+
password: string | undefined,
|
|
266
|
+
skipPassword: boolean,
|
|
267
|
+
) => {
|
|
268
|
+
const client = await getClient();
|
|
269
|
+
const response = await client.deleteAccount(
|
|
270
|
+
new DeleteAccountRequest({
|
|
271
|
+
nameOrBech32,
|
|
272
|
+
password,
|
|
273
|
+
skipPassword,
|
|
274
|
+
}),
|
|
275
|
+
);
|
|
276
|
+
return response;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const query = async (path: string, data: Uint8Array) => {
|
|
280
|
+
const client = await getClient();
|
|
281
|
+
const reponse = await client.query(
|
|
282
|
+
new QueryRequest({
|
|
283
|
+
path,
|
|
284
|
+
data,
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
return reponse;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const render = async (packagePath: string, args: string) => {
|
|
291
|
+
const client = await getClient();
|
|
292
|
+
const reponse = await client.render(
|
|
293
|
+
new RenderRequest({
|
|
294
|
+
packagePath,
|
|
295
|
+
args,
|
|
296
|
+
}),
|
|
297
|
+
);
|
|
298
|
+
return reponse.result;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const qEval = async (packagePath: string, expression: string) => {
|
|
302
|
+
const client = await getClient();
|
|
303
|
+
const reponse = await client.qEval(
|
|
304
|
+
new QEvalRequest({
|
|
305
|
+
packagePath,
|
|
306
|
+
expression,
|
|
307
|
+
}),
|
|
308
|
+
);
|
|
309
|
+
return reponse.result;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const call = async (
|
|
313
|
+
packagePath: string,
|
|
314
|
+
fnc: string,
|
|
315
|
+
args: Array<string>,
|
|
316
|
+
gasFee: string,
|
|
317
|
+
gasWanted: number,
|
|
318
|
+
send?: string,
|
|
319
|
+
memo?: string,
|
|
320
|
+
) => {
|
|
321
|
+
const client = await getClient();
|
|
322
|
+
const reponse = client.call(
|
|
323
|
+
new CallRequest({
|
|
324
|
+
gasFee,
|
|
325
|
+
gasWanted: BigInt(gasWanted),
|
|
326
|
+
memo,
|
|
327
|
+
msgs: [
|
|
328
|
+
new MsgCall({
|
|
329
|
+
packagePath,
|
|
330
|
+
fnc,
|
|
331
|
+
args,
|
|
332
|
+
send,
|
|
333
|
+
}),
|
|
334
|
+
],
|
|
335
|
+
}),
|
|
336
|
+
);
|
|
337
|
+
return reponse;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const send = async (
|
|
341
|
+
toAddress: Uint8Array,
|
|
342
|
+
send: string,
|
|
343
|
+
gasFee: string,
|
|
344
|
+
gasWanted: number,
|
|
345
|
+
memo?: string,
|
|
346
|
+
) => {
|
|
347
|
+
const client = await getClient();
|
|
348
|
+
const reponse = client.send(
|
|
349
|
+
new SendRequest({
|
|
350
|
+
gasFee,
|
|
351
|
+
gasWanted: BigInt(gasWanted),
|
|
352
|
+
memo,
|
|
353
|
+
msgs: [
|
|
354
|
+
new MsgSend({
|
|
355
|
+
toAddress,
|
|
356
|
+
send,
|
|
357
|
+
}),
|
|
358
|
+
],
|
|
359
|
+
}),
|
|
360
|
+
);
|
|
361
|
+
return reponse;
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const addressToBech32 = async (address: Uint8Array) => {
|
|
365
|
+
const client = await getClient();
|
|
366
|
+
const response = await client.addressToBech32(new AddressToBech32Request({ address }));
|
|
367
|
+
return response.bech32Address;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const addressFromBech32 = async (bech32Address: string) => {
|
|
371
|
+
const client = await getClient();
|
|
372
|
+
const response = await client.addressFromBech32(
|
|
373
|
+
new AddressFromBech32Request({ bech32Address }),
|
|
374
|
+
);
|
|
375
|
+
return response.address;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// debug
|
|
379
|
+
const hello = async (name: string) => {
|
|
380
|
+
const client = await getClient();
|
|
381
|
+
const response = await client.hello(new HelloRequest({ name }));
|
|
382
|
+
return response.greeting;
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// debug
|
|
386
|
+
const helloStream = async (name: string) => {
|
|
387
|
+
const client = await getClient();
|
|
388
|
+
return client.helloStream(new HelloRequest({ name }));
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
return {
|
|
392
|
+
initBridge,
|
|
393
|
+
closeBridge,
|
|
394
|
+
setRemote,
|
|
395
|
+
getRemote,
|
|
396
|
+
setChainID,
|
|
397
|
+
getChainID,
|
|
398
|
+
createAccount,
|
|
399
|
+
generateRecoveryPhrase,
|
|
400
|
+
listKeyInfo,
|
|
401
|
+
hasKeyByName,
|
|
402
|
+
hasKeyByAddress,
|
|
403
|
+
hasKeyByNameOrAddress,
|
|
404
|
+
getKeyInfoByName,
|
|
405
|
+
getKeyInfoByAddress,
|
|
406
|
+
getKeyInfoByNameOrAddress,
|
|
407
|
+
selectAccount,
|
|
408
|
+
setPassword,
|
|
409
|
+
getActiveAccount,
|
|
410
|
+
queryAccount,
|
|
411
|
+
deleteAccount,
|
|
412
|
+
query,
|
|
413
|
+
render,
|
|
414
|
+
qEval,
|
|
415
|
+
call,
|
|
416
|
+
send,
|
|
417
|
+
addressToBech32,
|
|
418
|
+
addressFromBech32,
|
|
419
|
+
// debug
|
|
420
|
+
hello,
|
|
421
|
+
helloStream,
|
|
422
|
+
};
|
|
423
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EventEmitter, NativeModulesProxy, Subscription } from 'expo-modules-core';
|
|
2
|
+
|
|
3
|
+
// Import the native module. On web, it will be resolved to Gnonative.web.ts
|
|
4
|
+
// and on native platforms to Gnonative.ts
|
|
5
|
+
import { ChangeEventPayload, GnonativeViewProps } from './Gnonative.types';
|
|
6
|
+
import GnonativeModule from './GnonativeModule';
|
|
7
|
+
import GnonativeView from './GnonativeView';
|
|
8
|
+
import 'fast-text-encoding';
|
|
9
|
+
|
|
10
|
+
(Symbol as any).asyncIterator = Symbol.asyncIterator || Symbol.for('Symbol.asyncIterator');
|
|
11
|
+
|
|
12
|
+
const emitter = new EventEmitter(GnonativeModule ?? NativeModulesProxy.Gnonative);
|
|
13
|
+
|
|
14
|
+
export function addChangeListener(listener: (event: ChangeEventPayload) => void): Subscription {
|
|
15
|
+
return emitter.addListener<ChangeEventPayload>('onChange', listener);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { ChangeEventPayload, GnonativeView, GnonativeViewProps };
|
|
19
|
+
export { useGno } from './hooks/use-gno';
|
|
20
|
+
export * from '@buf/gnolang_gnonative.bufbuild_es/gnonativetypes_pb';
|