@n1xyz/wallet-widget 0.0.1
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 +29 -0
- package/dist/Common/NIcon.d.ts +1 -0
- package/dist/Common/NIcon.js +7 -0
- package/dist/Logic/getPseudoName.d.ts +1 -0
- package/dist/Logic/getPseudoName.js +5825 -0
- package/dist/Logic/utils.d.ts +20 -0
- package/dist/Logic/utils.js +199 -0
- package/dist/Logic/words.d.ts +9 -0
- package/dist/Logic/words.js +93571 -0
- package/dist/Modal/BackButton.d.ts +3 -0
- package/dist/Modal/BackButton.js +13 -0
- package/dist/Modal/ConnectView.d.ts +1 -0
- package/dist/Modal/ConnectView.js +71 -0
- package/dist/Modal/CreateSessionView.d.ts +1 -0
- package/dist/Modal/CreateSessionView.js +236 -0
- package/dist/Modal/CreateUserView.d.ts +1 -0
- package/dist/Modal/CreateUserView.js +160 -0
- package/dist/Modal/ModalHeader.d.ts +1 -0
- package/dist/Modal/ModalHeader.js +9 -0
- package/dist/Modal/N1WalletModal.d.ts +1 -0
- package/dist/Modal/N1WalletModal.js +232 -0
- package/dist/Modal/NoWhitelistView.d.ts +1 -0
- package/dist/Modal/NoWhitelistView.js +29 -0
- package/dist/Modal/Sidebar/N1Sidebar.d.ts +1 -0
- package/dist/Modal/Sidebar/N1Sidebar.js +38 -0
- package/dist/Modal/SigningView.d.ts +1 -0
- package/dist/Modal/SigningView.js +5 -0
- package/dist/Modal/SuccessView.d.ts +1 -0
- package/dist/Modal/SuccessView.js +10 -0
- package/dist/Provider/LazyWalletProvider.d.ts +1 -0
- package/dist/Provider/LazyWalletProvider.js +160 -0
- package/dist/Provider/N1WalletProvider.d.ts +2 -0
- package/dist/Provider/N1WalletProvider.js +219 -0
- package/dist/Provider/context.d.ts +5 -0
- package/dist/Provider/context.js +4 -0
- package/dist/Provider/hooks.d.ts +2 -0
- package/dist/Provider/hooks.js +16 -0
- package/dist/Provider/icons/CoinbaseWalletIcon.d.ts +1 -0
- package/dist/Provider/icons/CoinbaseWalletIcon.js +4 -0
- package/dist/Provider/icons/ConnectionDotsSVG.d.ts +1 -0
- package/dist/Provider/icons/ConnectionDotsSVG.js +4 -0
- package/dist/Provider/icons/MetamaskIcon.d.ts +1 -0
- package/dist/Provider/icons/MetamaskIcon.js +4 -0
- package/dist/Provider/icons/PhantomIcon.d.ts +1 -0
- package/dist/Provider/icons/PhantomIcon.js +4 -0
- package/dist/Provider/icons/UserIcon.d.ts +1 -0
- package/dist/Provider/icons/UserIcon.js +4 -0
- package/dist/Provider/icons/WalletConnectIcon.d.ts +1 -0
- package/dist/Provider/icons/WalletConnectIcon.js +4 -0
- package/dist/Provider/index.d.ts +3 -0
- package/dist/Provider/index.js +3 -0
- package/dist/Provider/types.d.ts +68 -0
- package/dist/Provider/types.js +14 -0
- package/dist/WidgetButton/N1ConnectButton.d.ts +5 -0
- package/dist/WidgetButton/N1ConnectButton.js +24 -0
- package/dist/components/AnimatedButton.d.ts +9 -0
- package/dist/components/AnimatedButton.js +5 -0
- package/dist/components/LazyLoadWrapper.d.ts +8 -0
- package/dist/components/LazyLoadWrapper.js +17 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +2 -0
- package/dist/errors/types.d.ts +21 -0
- package/dist/errors/types.js +64 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/main.css +1 -0
- package/dist/utils/lazyLoad.d.ts +3 -0
- package/dist/utils/lazyLoad.js +9 -0
- package/dist/utils/logger.d.ts +20 -0
- package/dist/utils/logger.js +106 -0
- package/dist/utils/react-shim.d.ts +2 -0
- package/dist/utils/react-shim.js +6 -0
- package/package.json +46 -0
- package/postcss.config.js +6 -0
- package/src/Logic/getPseudoName.ts +5832 -0
- package/src/Logic/utils.ts +162 -0
- package/src/Logic/words.ts +93571 -0
- package/src/Modal/BackButton.tsx +23 -0
- package/src/Modal/ConnectView.tsx +88 -0
- package/src/Modal/CreateSessionView.tsx +242 -0
- package/src/Modal/CreateUserView.tsx +195 -0
- package/src/Modal/ModalHeader.tsx +64 -0
- package/src/Modal/N1WalletModal.tsx +275 -0
- package/src/Modal/NoWhitelistView.tsx +61 -0
- package/src/Modal/Sidebar/N1Sidebar.tsx +172 -0
- package/src/Modal/SigningView.tsx +18 -0
- package/src/Modal/SuccessView.tsx +36 -0
- package/src/Provider/LazyWalletProvider.tsx +136 -0
- package/src/Provider/N1WalletProvider.tsx +208 -0
- package/src/Provider/context.ts +11 -0
- package/src/Provider/hooks.ts +22 -0
- package/src/Provider/icons/CoinbaseWalletIcon.tsx +27 -0
- package/src/Provider/icons/ConnectionDotsSVG.tsx +19 -0
- package/src/Provider/icons/MetamaskIcon.tsx +58 -0
- package/src/Provider/icons/PhantomIcon.tsx +26 -0
- package/src/Provider/icons/UserIcon.tsx +15 -0
- package/src/Provider/icons/WalletConnectIcon.tsx +15 -0
- package/src/Provider/index.ts +3 -0
- package/src/Provider/types.ts +77 -0
- package/src/WidgetButton/N1ConnectButton.tsx +72 -0
- package/src/components/LazyLoadWrapper.tsx +19 -0
- package/src/config.ts +2 -0
- package/src/errors/types.ts +49 -0
- package/src/index.ts +5 -0
- package/src/main.css +3 -0
- package/src/styles/main.css +3 -0
- package/src/utils/lazyLoad.ts +12 -0
- package/src/utils/logger.ts +98 -0
- package/src/utils/react-shim.js +8 -0
- package/tailwind.config.js +44 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateSessionKeyPair,
|
|
3
|
+
generateRandomUint8Array,
|
|
4
|
+
} from '@n1xyz/nts-sdk';
|
|
5
|
+
import { logger } from '../utils/logger';
|
|
6
|
+
|
|
7
|
+
export { generateSessionKeyPair, generateRandomUint8Array };
|
|
8
|
+
|
|
9
|
+
export function arrayBufferToBase64(buffer: Uint8Array) {
|
|
10
|
+
let binary = '';
|
|
11
|
+
const bytes = new Uint8Array(buffer);
|
|
12
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
13
|
+
binary += String.fromCharCode(bytes[i]);
|
|
14
|
+
}
|
|
15
|
+
return btoa(binary);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Convert Base64 string back to Uint8Array
|
|
19
|
+
export function base64ToUint8Array(base64String: string) {
|
|
20
|
+
const binaryString = atob(base64String);
|
|
21
|
+
const length = binaryString.length;
|
|
22
|
+
const bytes = new Uint8Array(length);
|
|
23
|
+
for (let i = 0; i < length; i++) {
|
|
24
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
25
|
+
}
|
|
26
|
+
return bytes;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Function to store keys with a custom ID format in local storage, including private key
|
|
30
|
+
export function storeSessionKeysInLocalStorage({
|
|
31
|
+
privKey,
|
|
32
|
+
walletPublicKey,
|
|
33
|
+
chainName,
|
|
34
|
+
}: {
|
|
35
|
+
privKey: Uint8Array;
|
|
36
|
+
walletPublicKey: string;
|
|
37
|
+
chainName: string;
|
|
38
|
+
}) {
|
|
39
|
+
const storageKey = `n1_sessionKey_${walletPublicKey}`;
|
|
40
|
+
|
|
41
|
+
const data = {
|
|
42
|
+
ed25519PrivateKey: arrayBufferToBase64(privKey),
|
|
43
|
+
walletPublicKey,
|
|
44
|
+
chainName,
|
|
45
|
+
createdAt: new Date().toISOString(),
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
localStorage.setItem(storageKey, JSON.stringify(data));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Retrieve data from local storage by wallet key and convert the private key back to Uint8Array
|
|
52
|
+
export function getSessionKeysFromLocalStorage(walletPublicKey: string): {
|
|
53
|
+
ed25519PrivateKey: Uint8Array;
|
|
54
|
+
walletPublicKey: string;
|
|
55
|
+
chainName: string;
|
|
56
|
+
createdAt: string;
|
|
57
|
+
} | null {
|
|
58
|
+
const storageKey = `n1_sessionKey_${walletPublicKey}`;
|
|
59
|
+
|
|
60
|
+
const data = localStorage.getItem(storageKey);
|
|
61
|
+
if (data) {
|
|
62
|
+
const parsedData = JSON.parse(data);
|
|
63
|
+
if (parsedData.ed25519PrivateKey) {
|
|
64
|
+
parsedData.ed25519PrivateKey = base64ToUint8Array(
|
|
65
|
+
parsedData.ed25519PrivateKey
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return parsedData;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Function to remove data from local storage by wallet key
|
|
74
|
+
export function removeKeysFromLocalStorage(walletPublicKey: string) {
|
|
75
|
+
const storageKey = `n1_sessionKey_${walletPublicKey}`;
|
|
76
|
+
|
|
77
|
+
localStorage.removeItem(storageKey);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function hexToUint8Array(hex: string) {
|
|
81
|
+
if (hex.length % 2 !== 0) {
|
|
82
|
+
throw new Error('Invalid hex string');
|
|
83
|
+
}
|
|
84
|
+
const array = new Uint8Array(hex.length / 2);
|
|
85
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
86
|
+
array[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
87
|
+
}
|
|
88
|
+
return array;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getMsgBytes(timestamp: number) {
|
|
92
|
+
const hex = 'LoginSigning : ' + timestamp;
|
|
93
|
+
const array = new Uint8Array(hex.length / 2);
|
|
94
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
95
|
+
array[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
96
|
+
}
|
|
97
|
+
return array;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function createWebSession(
|
|
101
|
+
signMessageWithSessionKey: any,
|
|
102
|
+
sessionPubKey: any,
|
|
103
|
+
walletPubKey: any
|
|
104
|
+
) {
|
|
105
|
+
logger.debug('Creating web session...');
|
|
106
|
+
const timestamp = new Date().getTime();
|
|
107
|
+
const msg = getMsgBytes(timestamp);
|
|
108
|
+
const signature = await signMessageWithSessionKey(msg);
|
|
109
|
+
const response = await fetch(`${process.env.ARGUS_URL}/api/auth/login`, {
|
|
110
|
+
method: 'POST',
|
|
111
|
+
headers: {
|
|
112
|
+
'Content-Type': 'application/json',
|
|
113
|
+
},
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
sessionKey: Array.from(sessionPubKey),
|
|
116
|
+
timestamp: timestamp,
|
|
117
|
+
msg: Array.from(msg),
|
|
118
|
+
signature: Array.from(signature),
|
|
119
|
+
publicKey: Array.from(walletPubKey),
|
|
120
|
+
}),
|
|
121
|
+
credentials: 'include', // Include cookies in requests
|
|
122
|
+
});
|
|
123
|
+
if (response.status != 200) {
|
|
124
|
+
console.log(await response.json());
|
|
125
|
+
throw new Error('failed to create a session!');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function createUser(username: any) {
|
|
130
|
+
const response = await fetch(
|
|
131
|
+
`${process.env.ARGUS_URL}/api/db/createUser?name=${username}`,
|
|
132
|
+
{
|
|
133
|
+
method: 'GET',
|
|
134
|
+
headers: {
|
|
135
|
+
'Content-Type': 'application/json',
|
|
136
|
+
},
|
|
137
|
+
credentials: 'include',
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
if (response.status != 200) {
|
|
141
|
+
throw new Error('failed to create a user!');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function getUser() {
|
|
146
|
+
try {
|
|
147
|
+
const response = await fetch(`${process.env.ARGUS_URL}/api/db/getUser`, {
|
|
148
|
+
method: 'GET',
|
|
149
|
+
headers: {
|
|
150
|
+
'Content-Type': 'application/json',
|
|
151
|
+
},
|
|
152
|
+
credentials: 'include', // Include cookies in requests
|
|
153
|
+
});
|
|
154
|
+
if (response.status === 200) {
|
|
155
|
+
return await response.json();
|
|
156
|
+
} else if (response.status === 404) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
} catch (error) {
|
|
160
|
+
throw new Error('failed to get user!');
|
|
161
|
+
}
|
|
162
|
+
}
|