zano_web3 9.6.0 → 10.1.0
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/package.json +17 -9
- package/server/dist/server.d.ts +13 -2
- package/server/dist/server.js +68 -6
- package/server/dist/server.js.map +1 -1
- package/server/dist/types.d.ts +24 -24
- package/server/src/server.ts +101 -8
- package/server/src/types.ts +111 -111
- package/shared/dist/utils.d.ts +61 -0
- package/shared/dist/utils.js +188 -0
- package/shared/dist/utils.js.map +1 -1
- package/shared/src/utils.ts +276 -1
- package/web/dist/index.d.ts +2 -4
- package/web/dist/index.js +2 -4
- package/web/dist/index.js.map +1 -1
- package/web/dist/types/errors/index.d.ts +7 -0
- package/web/dist/types/errors/index.js +9 -0
- package/web/dist/types/errors/index.js.map +1 -0
- package/web/dist/types/index.d.ts +4 -0
- package/web/dist/types/index.js +5 -0
- package/web/dist/types/index.js.map +1 -0
- package/web/dist/types/params/companion-requests/index.d.ts +1 -0
- package/web/dist/types/params/companion-requests/index.js +2 -0
- package/web/dist/types/params/companion-requests/index.js.map +1 -0
- package/web/dist/types/params/companion-requests/request-permissions/index.d.ts +5 -0
- package/web/dist/types/params/companion-requests/request-permissions/index.js +2 -0
- package/web/dist/types/params/companion-requests/request-permissions/index.js.map +1 -0
- package/web/dist/types/params/index.d.ts +1 -0
- package/web/dist/types/params/index.js +2 -0
- package/web/dist/types/params/index.js.map +1 -0
- package/web/dist/types/responses/companion-methods/create-alias/index.d.ts +13 -0
- package/web/dist/types/responses/companion-methods/create-alias/index.js +10 -0
- package/web/dist/types/responses/companion-methods/create-alias/index.js.map +1 -0
- package/web/dist/types/responses/companion-methods/get-address-by-alias/index.d.ts +12 -0
- package/web/dist/types/responses/companion-methods/get-address-by-alias/index.js +8 -0
- package/web/dist/types/responses/companion-methods/get-address-by-alias/index.js.map +1 -0
- package/web/dist/types/responses/companion-methods/get-wallet-data/index.d.ts +73 -0
- package/web/dist/types/responses/companion-methods/get-wallet-data/index.js +39 -0
- package/web/dist/types/responses/companion-methods/get-wallet-data/index.js.map +1 -0
- package/web/dist/types/responses/companion-methods/index.d.ts +5 -0
- package/web/dist/types/responses/companion-methods/index.js +2 -0
- package/web/dist/types/responses/companion-methods/index.js.map +1 -0
- package/web/dist/types/responses/companion-methods/request-message-sign/index.d.ts +23 -0
- package/web/dist/types/responses/companion-methods/request-message-sign/index.js +16 -0
- package/web/dist/types/responses/companion-methods/request-message-sign/index.js.map +1 -0
- package/web/dist/types/responses/companion-methods/request-permissions/index.d.ts +13 -0
- package/web/dist/types/responses/companion-methods/request-permissions/index.js +10 -0
- package/web/dist/types/responses/companion-methods/request-permissions/index.js.map +1 -0
- package/web/dist/types/responses/index.d.ts +1 -0
- package/web/dist/types/responses/index.js +2 -0
- package/web/dist/types/responses/index.js.map +1 -0
- package/web/dist/types/special/index.d.ts +1 -0
- package/web/dist/types/special/index.js +2 -0
- package/web/dist/types/special/index.js.map +1 -0
- package/web/dist/types/special/zano-window/index.d.ts +7 -0
- package/web/dist/types/special/zano-window/index.js +2 -0
- package/web/dist/types/special/zano-window/index.js.map +1 -0
- package/web/dist/zano-wallet.d.ts +11 -0
- package/web/dist/zano-wallet.js +125 -0
- package/web/dist/zano-wallet.js.map +1 -0
- package/web/src/index.ts +4 -7
- package/web/src/types/errors/index.ts +10 -0
- package/web/src/types/index.ts +4 -0
- package/web/src/types/params/companion-requests/index.ts +1 -0
- package/web/src/types/params/companion-requests/request-permissions/index.ts +10 -0
- package/web/src/types/params/index.ts +1 -0
- package/web/src/types/responses/companion-methods/create-alias/index.ts +19 -0
- package/web/src/types/responses/companion-methods/get-address-by-alias/index.ts +18 -0
- package/web/src/types/responses/companion-methods/get-wallet-data/index.ts +52 -0
- package/web/src/types/responses/companion-methods/index.ts +5 -0
- package/web/src/types/responses/companion-methods/request-message-sign/index.ts +33 -0
- package/web/src/types/responses/companion-methods/request-permissions/index.ts +19 -0
- package/web/src/types/responses/index.ts +2 -0
- package/web/src/types/special/index.ts +1 -0
- package/web/src/types/special/zano-window/index.ts +9 -0
- package/web/src/zano-wallet.ts +161 -0
- package/web/dist/hooks.d.ts +0 -3
- package/web/dist/hooks.js +0 -14
- package/web/dist/hooks.js.map +0 -1
- package/web/dist/types.d.ts +0 -32
- package/web/dist/types.js +0 -2
- package/web/dist/types.js.map +0 -1
- package/web/dist/zanoWallet.d.ts +0 -42
- package/web/dist/zanoWallet.js +0 -156
- package/web/dist/zanoWallet.js.map +0 -1
- package/web/src/hooks.ts +0 -18
- package/web/src/types.ts +0 -35
- package/web/src/zanoWallet.ts +0 -240
package/web/dist/zanoWallet.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
class ZanoWallet {
|
|
3
|
-
DEFAULT_LOCAL_STORAGE_KEY = "wallet";
|
|
4
|
-
localStorageKey;
|
|
5
|
-
params;
|
|
6
|
-
zanoWallet;
|
|
7
|
-
constructor(params) {
|
|
8
|
-
if (typeof window === 'undefined') {
|
|
9
|
-
throw new Error('ZanoWallet can only be used in the browser');
|
|
10
|
-
}
|
|
11
|
-
if (!window.zano) {
|
|
12
|
-
console.error('ZanoWallet requires the ZanoWallet extension to be installed');
|
|
13
|
-
}
|
|
14
|
-
this.params = params;
|
|
15
|
-
this.zanoWallet = window.zano;
|
|
16
|
-
this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
|
|
17
|
-
}
|
|
18
|
-
handleError({ message }) {
|
|
19
|
-
if (this.params.onConnectError) {
|
|
20
|
-
this.params.onConnectError(message);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
console.error(message);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
getSavedWalletCredentials() {
|
|
27
|
-
const savedWallet = localStorage.getItem(this.localStorageKey);
|
|
28
|
-
if (!savedWallet)
|
|
29
|
-
return undefined;
|
|
30
|
-
try {
|
|
31
|
-
return JSON.parse(savedWallet);
|
|
32
|
-
}
|
|
33
|
-
catch {
|
|
34
|
-
return undefined;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
setWalletCredentials(credentials) {
|
|
38
|
-
if (credentials) {
|
|
39
|
-
localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
localStorage.removeItem(this.localStorageKey);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
cleanWalletCredentials() {
|
|
46
|
-
this.setWalletCredentials(undefined);
|
|
47
|
-
}
|
|
48
|
-
async requestPermissions(permissions) {
|
|
49
|
-
try {
|
|
50
|
-
return await this.zanoWallet.request('REQUEST_ACCESS', {
|
|
51
|
-
permissions,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
return {
|
|
56
|
-
error: error instanceof Error ? error.message : String(error),
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async connect() {
|
|
61
|
-
if (this.params.beforeConnect) {
|
|
62
|
-
await this.params.beforeConnect();
|
|
63
|
-
}
|
|
64
|
-
if (this.params.onConnectStart) {
|
|
65
|
-
this.params.onConnectStart();
|
|
66
|
-
}
|
|
67
|
-
const walletData = (await window.zano.request('GET_WALLET_DATA')).data;
|
|
68
|
-
if (!walletData?.address) {
|
|
69
|
-
return this.handleError({ message: 'Companion is offline' });
|
|
70
|
-
}
|
|
71
|
-
if (!walletData?.alias && this.params.aliasRequired) {
|
|
72
|
-
return this.handleError({ message: 'Alias not found' });
|
|
73
|
-
}
|
|
74
|
-
let nonce = "";
|
|
75
|
-
let signature = "";
|
|
76
|
-
let publicKey = "";
|
|
77
|
-
const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
|
|
78
|
-
const existingWalletValid = existingWallet && existingWallet.address === walletData.address;
|
|
79
|
-
console.log('existingWalletValid', existingWalletValid);
|
|
80
|
-
console.log('existingWallet', existingWallet);
|
|
81
|
-
console.log('walletData', walletData);
|
|
82
|
-
if (existingWalletValid) {
|
|
83
|
-
nonce = existingWallet.nonce;
|
|
84
|
-
signature = existingWallet.signature;
|
|
85
|
-
publicKey = existingWallet.publicKey;
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
const generatedNonce = this.params.customNonce || uuidv4();
|
|
89
|
-
const signResult = await this.zanoWallet.request('REQUEST_MESSAGE_SIGN', {
|
|
90
|
-
message: generatedNonce
|
|
91
|
-
}, null);
|
|
92
|
-
if (!signResult?.data?.result) {
|
|
93
|
-
return this.handleError({ message: 'Failed to sign message' });
|
|
94
|
-
}
|
|
95
|
-
nonce = generatedNonce;
|
|
96
|
-
signature = signResult.data.result.sig;
|
|
97
|
-
publicKey = signResult.data.result.pkey;
|
|
98
|
-
}
|
|
99
|
-
const serverData = {
|
|
100
|
-
alias: walletData.alias,
|
|
101
|
-
address: walletData.address,
|
|
102
|
-
signature,
|
|
103
|
-
pkey: publicKey,
|
|
104
|
-
message: nonce,
|
|
105
|
-
isSavedData: existingWalletValid
|
|
106
|
-
};
|
|
107
|
-
if (this.params.onLocalConnectEnd) {
|
|
108
|
-
this.params.onLocalConnectEnd(serverData);
|
|
109
|
-
}
|
|
110
|
-
if (!this.params.disableServerRequest) {
|
|
111
|
-
const result = await fetch(this.params.customServerPath || "/api/auth", {
|
|
112
|
-
method: "POST",
|
|
113
|
-
headers: {
|
|
114
|
-
"Content-Type": "application/json",
|
|
115
|
-
},
|
|
116
|
-
body: JSON.stringify({
|
|
117
|
-
data: serverData
|
|
118
|
-
})
|
|
119
|
-
})
|
|
120
|
-
.then(res => res.json())
|
|
121
|
-
.catch((e) => ({
|
|
122
|
-
success: false,
|
|
123
|
-
error: e.message
|
|
124
|
-
}));
|
|
125
|
-
if (!result?.success || !result?.data) {
|
|
126
|
-
return this.handleError({ message: result.error });
|
|
127
|
-
}
|
|
128
|
-
if (!existingWalletValid && this.params.useLocalStorage) {
|
|
129
|
-
this.setWalletCredentials({
|
|
130
|
-
publicKey,
|
|
131
|
-
signature,
|
|
132
|
-
nonce,
|
|
133
|
-
address: walletData.address
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
if (this.params.onConnectEnd) {
|
|
137
|
-
this.params.onConnectEnd({
|
|
138
|
-
...serverData,
|
|
139
|
-
token: result.data.token
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
async getWallet() {
|
|
146
|
-
return (await this.zanoWallet.request('GET_WALLET_DATA'))?.data;
|
|
147
|
-
}
|
|
148
|
-
async getAddressByAlias(alias) {
|
|
149
|
-
return ((await this.zanoWallet.request('GET_ALIAS_DETAILS', { alias })) || undefined);
|
|
150
|
-
}
|
|
151
|
-
async createAlias(alias) {
|
|
152
|
-
return ((await this.zanoWallet.request('CREATE_ALIAS', { alias })) || undefined).data;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
export default ZanoWallet;
|
|
156
|
-
//# sourceMappingURL=zanoWallet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"zanoWallet.js","sourceRoot":"","sources":["../src/zanoWallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AA8CpC,MAAM,UAAU;IAEJ,yBAAyB,GAAG,QAAQ,CAAC;IACrC,eAAe,CAAS;IAExB,MAAM,CAAmB;IACzB,UAAU,CAAmB;IAErC,YAAY,MAAwB;QAEhC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAClE,CAAC;QAED,IAAI,CAAG,MAAiC,CAAC,IAAI,EAAE,CAAC;YAC5C,OAAO,CAAC,KAAK,CAAC,8DAA8D,CAAC,CAAC;QAClF,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,UAAU,GAAK,MA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BAAiB,CAAC,KAAa;QACjC,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,mBAAmB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,SAAS,CAAuB,CAAC;IAChH,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,KAAa;QAC3B,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,IAAI,CAAC;IAC1F,CAAC;CACJ;AAED,eAAe,UAAU,CAAC"}
|
package/web/src/hooks.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import ZanoWallet, { ZanoWalletParams } from './zanoWallet';
|
|
2
|
-
import { useEffect, useState } from 'react';
|
|
3
|
-
|
|
4
|
-
function useZanoWallet(params: ZanoWalletParams) {
|
|
5
|
-
const [zanoWallet, setZanoWallet] = useState<ZanoWallet | null>(null);
|
|
6
|
-
|
|
7
|
-
useEffect(() => {
|
|
8
|
-
if (typeof window === 'undefined') {
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
setZanoWallet(new ZanoWallet(params));
|
|
13
|
-
}, []);
|
|
14
|
-
|
|
15
|
-
return zanoWallet;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { useZanoWallet };
|
package/web/src/types.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export interface Asset {
|
|
2
|
-
name: string;
|
|
3
|
-
ticker: string;
|
|
4
|
-
assetId: string;
|
|
5
|
-
decimalPoint: number;
|
|
6
|
-
balance: string;
|
|
7
|
-
unlockedBalance: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface Transfer {
|
|
11
|
-
amount: string;
|
|
12
|
-
assetId: string;
|
|
13
|
-
incoming: boolean;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface Transaction {
|
|
17
|
-
isConfirmed: boolean;
|
|
18
|
-
txHash: string;
|
|
19
|
-
blobSize: number;
|
|
20
|
-
timestamp: number;
|
|
21
|
-
height: number;
|
|
22
|
-
paymentId: string;
|
|
23
|
-
comment: string;
|
|
24
|
-
fee: string;
|
|
25
|
-
isInitiator: boolean;
|
|
26
|
-
transfers: Transfer[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface Wallet {
|
|
30
|
-
address: string;
|
|
31
|
-
alias: string;
|
|
32
|
-
balance: string;
|
|
33
|
-
assets: Asset[];
|
|
34
|
-
transactions: Transaction[];
|
|
35
|
-
}
|
package/web/src/zanoWallet.ts
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import { Wallet } from './types';
|
|
3
|
-
|
|
4
|
-
export interface ZanoWalletParams {
|
|
5
|
-
authPath: string;
|
|
6
|
-
useLocalStorage?: boolean; // default: true
|
|
7
|
-
aliasRequired?: boolean;
|
|
8
|
-
customLocalStorageKey?: string;
|
|
9
|
-
customNonce?: string;
|
|
10
|
-
customServerPath?: string;
|
|
11
|
-
disableServerRequest?: boolean;
|
|
12
|
-
|
|
13
|
-
onConnectStart?: (...params: any) => any;
|
|
14
|
-
onConnectEnd?: (...params: any) => any;
|
|
15
|
-
onConnectError?: (...params: any) => any;
|
|
16
|
-
|
|
17
|
-
beforeConnect?: (...params: any) => any;
|
|
18
|
-
onLocalConnectEnd?: (...params: any) => any;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type GlobalWindow = Window & typeof globalThis;
|
|
22
|
-
|
|
23
|
-
interface ZanoWindowParams {
|
|
24
|
-
request: (str: string, params?: any, timeoutMs?: number | null) => Promise<any>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
type ZanoWindow = Omit<GlobalWindow, 'Infinity'> & {
|
|
28
|
-
zano: ZanoWindowParams
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface WalletCredentials {
|
|
32
|
-
nonce: string;
|
|
33
|
-
signature: string;
|
|
34
|
-
publicKey: string;
|
|
35
|
-
address: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type PermissionType =
|
|
39
|
-
| 'general'
|
|
40
|
-
| 'balance'
|
|
41
|
-
| 'history'
|
|
42
|
-
|
|
43
|
-
interface CompanionPermission {
|
|
44
|
-
type: PermissionType;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
class ZanoWallet {
|
|
48
|
-
|
|
49
|
-
private DEFAULT_LOCAL_STORAGE_KEY = "wallet";
|
|
50
|
-
private localStorageKey: string;
|
|
51
|
-
|
|
52
|
-
private params: ZanoWalletParams;
|
|
53
|
-
private zanoWallet: ZanoWindowParams;
|
|
54
|
-
|
|
55
|
-
constructor(params: ZanoWalletParams) {
|
|
56
|
-
|
|
57
|
-
if (typeof window === 'undefined') {
|
|
58
|
-
throw new Error('ZanoWallet can only be used in the browser');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (!((window as unknown) as ZanoWindow).zano) {
|
|
62
|
-
console.error('ZanoWallet requires the ZanoWallet extension to be installed');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
this.params = params;
|
|
66
|
-
this.zanoWallet = ((window as unknown) as ZanoWindow).zano;
|
|
67
|
-
this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
private handleError({ message }: { message: string }) {
|
|
72
|
-
if (this.params.onConnectError) {
|
|
73
|
-
this.params.onConnectError(message);
|
|
74
|
-
} else {
|
|
75
|
-
console.error(message);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
getSavedWalletCredentials() {
|
|
80
|
-
const savedWallet = localStorage.getItem(this.localStorageKey);
|
|
81
|
-
if (!savedWallet) return undefined;
|
|
82
|
-
try {
|
|
83
|
-
return JSON.parse(savedWallet) as WalletCredentials;
|
|
84
|
-
} catch {
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
setWalletCredentials(credentials: WalletCredentials | undefined) {
|
|
90
|
-
if (credentials) {
|
|
91
|
-
localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
|
|
92
|
-
} else {
|
|
93
|
-
localStorage.removeItem(this.localStorageKey);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
cleanWalletCredentials() {
|
|
98
|
-
this.setWalletCredentials(undefined);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async requestPermissions(permissions: CompanionPermission[]) {
|
|
102
|
-
try {
|
|
103
|
-
return await this.zanoWallet.request('REQUEST_ACCESS', {
|
|
104
|
-
permissions,
|
|
105
|
-
});
|
|
106
|
-
} catch (error) {
|
|
107
|
-
return {
|
|
108
|
-
error: error instanceof Error ? error.message : String(error),
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async connect() {
|
|
114
|
-
if (this.params.beforeConnect) {
|
|
115
|
-
await this.params.beforeConnect();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (this.params.onConnectStart) {
|
|
119
|
-
this.params.onConnectStart();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const walletData = (await ((window as unknown) as ZanoWindow).zano.request('GET_WALLET_DATA')).data;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (!walletData?.address) {
|
|
126
|
-
return this.handleError({ message: 'Companion is offline' });
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (!walletData?.alias && this.params.aliasRequired) {
|
|
130
|
-
return this.handleError({ message: 'Alias not found' });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
let nonce = "";
|
|
134
|
-
let signature = "";
|
|
135
|
-
let publicKey = "";
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
|
|
139
|
-
|
|
140
|
-
const existingWalletValid = existingWallet && existingWallet.address === walletData.address;
|
|
141
|
-
|
|
142
|
-
console.log('existingWalletValid', existingWalletValid);
|
|
143
|
-
console.log('existingWallet', existingWallet);
|
|
144
|
-
console.log('walletData', walletData);
|
|
145
|
-
|
|
146
|
-
if (existingWalletValid) {
|
|
147
|
-
nonce = existingWallet.nonce;
|
|
148
|
-
signature = existingWallet.signature;
|
|
149
|
-
publicKey = existingWallet.publicKey;
|
|
150
|
-
} else {
|
|
151
|
-
const generatedNonce = this.params.customNonce || uuidv4();
|
|
152
|
-
|
|
153
|
-
const signResult = await this.zanoWallet.request(
|
|
154
|
-
'REQUEST_MESSAGE_SIGN',
|
|
155
|
-
{
|
|
156
|
-
message: generatedNonce
|
|
157
|
-
},
|
|
158
|
-
null
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
if (!signResult?.data?.result) {
|
|
162
|
-
return this.handleError({ message: 'Failed to sign message' });
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
nonce = generatedNonce;
|
|
166
|
-
signature = signResult.data.result.sig;
|
|
167
|
-
publicKey = signResult.data.result.pkey;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const serverData = {
|
|
172
|
-
alias: walletData.alias,
|
|
173
|
-
address: walletData.address,
|
|
174
|
-
signature,
|
|
175
|
-
pkey: publicKey,
|
|
176
|
-
message: nonce,
|
|
177
|
-
isSavedData: existingWalletValid
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (this.params.onLocalConnectEnd) {
|
|
181
|
-
this.params.onLocalConnectEnd(serverData);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (!this.params.disableServerRequest) {
|
|
186
|
-
const result = await fetch(this.params.customServerPath || "/api/auth", {
|
|
187
|
-
method: "POST",
|
|
188
|
-
headers: {
|
|
189
|
-
"Content-Type": "application/json",
|
|
190
|
-
},
|
|
191
|
-
body: JSON.stringify(
|
|
192
|
-
{
|
|
193
|
-
data: serverData
|
|
194
|
-
}
|
|
195
|
-
)
|
|
196
|
-
})
|
|
197
|
-
.then(res => res.json())
|
|
198
|
-
.catch((e) => ({
|
|
199
|
-
success: false,
|
|
200
|
-
error: e.message
|
|
201
|
-
}));
|
|
202
|
-
|
|
203
|
-
if (!result?.success || !result?.data) {
|
|
204
|
-
return this.handleError({ message: result.error });
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (!existingWalletValid && this.params.useLocalStorage) {
|
|
208
|
-
this.setWalletCredentials({
|
|
209
|
-
publicKey,
|
|
210
|
-
signature,
|
|
211
|
-
nonce,
|
|
212
|
-
address: walletData.address
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (this.params.onConnectEnd) {
|
|
217
|
-
this.params.onConnectEnd({
|
|
218
|
-
...serverData,
|
|
219
|
-
token: result.data.token
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return true;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
async getWallet() {
|
|
228
|
-
return (await this.zanoWallet.request('GET_WALLET_DATA'))?.data as Wallet;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
async getAddressByAlias(alias: string) {
|
|
232
|
-
return ((await this.zanoWallet.request('GET_ALIAS_DETAILS', { alias })) || undefined) as string | undefined;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
async createAlias(alias: string) {
|
|
236
|
-
return ((await this.zanoWallet.request('CREATE_ALIAS', { alias })) || undefined).data;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export default ZanoWallet;
|