@ultraos/wallet-sdk 0.0.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/LICENSE +165 -0
- package/README.md +74 -0
- package/eslint.config.js +5 -0
- package/jest.config.ts +11 -0
- package/karma.conf.js +16 -0
- package/package.json +14 -0
- package/project.json +33 -0
- package/src/index.ts +2 -0
- package/src/lib/common/client-error.ts +14 -0
- package/src/lib/common/client-messenger.spec.ts +118 -0
- package/src/lib/common/client-messenger.ts +109 -0
- package/src/lib/common/window-manager.spec.ts +65 -0
- package/src/lib/common/window-manager.ts +34 -0
- package/src/lib/config/ultra-wallet-sdk.config.ts +37 -0
- package/src/lib/interfaces/blockchain-transaction.interface.ts +21 -0
- package/src/lib/interfaces/connect-params.interface.ts +13 -0
- package/src/lib/interfaces/error-enum.ts +15 -0
- package/src/lib/interfaces/index.ts +8 -0
- package/src/lib/interfaces/json-rpc-message.interface.ts +7 -0
- package/src/lib/interfaces/purchase-item.interface.ts +7 -0
- package/src/lib/interfaces/wallet-provider-response.interface.ts +157 -0
- package/src/lib/interfaces/wallet-provider.interface.ts +55 -0
- package/src/lib/interfaces/wallet-sdk-options.interface.ts +9 -0
- package/src/lib/providers/extension-provider/extension.provider.spec.ts +121 -0
- package/src/lib/providers/extension-provider/extension.provider.ts +55 -0
- package/src/lib/providers/index.ts +2 -0
- package/src/lib/providers/web-provider/web.provider.spec.ts +76 -0
- package/src/lib/providers/web-provider/web.provider.ts +59 -0
- package/src/lib/ultra-wallet-sdk.spec.ts +90 -0
- package/src/lib/ultra-wallet-sdk.ts +67 -0
- package/src/lib/utils/chain-validator.ts +6 -0
- package/src/lib/utils/detection.ts +3 -0
- package/tsconfig.json +28 -0
- package/tsconfig.lib.json +12 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +8 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { UltraWalletSDK } from './ultra-wallet-sdk';
|
|
2
|
+
import { ExtensionProvider, WebProvider } from './providers';
|
|
3
|
+
import * as detectionUtils from './utils/detection';
|
|
4
|
+
import { ConnectParams, PurchaseItemType } from './interfaces';
|
|
5
|
+
|
|
6
|
+
jest.mock('./providers/extension-provider/extension.provider');
|
|
7
|
+
jest.mock('./providers/web-provider/web.provider');
|
|
8
|
+
jest.mock('./utils/detection');
|
|
9
|
+
|
|
10
|
+
describe('UltraWalletSDK', () => {
|
|
11
|
+
let sdk: UltraWalletSDK;
|
|
12
|
+
let connectSpy: jest.Mock;
|
|
13
|
+
let disconnectSpy: jest.Mock;
|
|
14
|
+
let signMessageSpy: jest.Mock;
|
|
15
|
+
let signTransactionSpy: jest.Mock;
|
|
16
|
+
let getChainIdSpy: jest.Mock;
|
|
17
|
+
let purchaseItemSpy: jest.Mock;
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
connectSpy = jest.fn();
|
|
21
|
+
disconnectSpy = jest.fn();
|
|
22
|
+
signMessageSpy = jest.fn();
|
|
23
|
+
signTransactionSpy = jest.fn();
|
|
24
|
+
getChainIdSpy = jest.fn();
|
|
25
|
+
purchaseItemSpy = jest.fn();
|
|
26
|
+
|
|
27
|
+
(detectionUtils.isExtensionAvailable as jest.Mock).mockReturnValue(true);
|
|
28
|
+
(ExtensionProvider as jest.Mock).mockImplementation(() => ({
|
|
29
|
+
connect: connectSpy,
|
|
30
|
+
disconnect: disconnectSpy,
|
|
31
|
+
signMessage: signMessageSpy,
|
|
32
|
+
signTransaction: signTransactionSpy,
|
|
33
|
+
getChainId: getChainIdSpy,
|
|
34
|
+
purchaseItem: purchaseItemSpy,
|
|
35
|
+
}));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should use ExtensionProvider when extension is available', () => {
|
|
39
|
+
sdk = new UltraWalletSDK();
|
|
40
|
+
expect(sdk).toBeTruthy();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should use WebProvider when extension is not available', () => {
|
|
44
|
+
(detectionUtils.isExtensionAvailable as jest.Mock).mockReturnValue(false);
|
|
45
|
+
(WebProvider as unknown as jest.Mock).mockImplementation(() => ({
|
|
46
|
+
connect: connectSpy,
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
sdk = new UltraWalletSDK({ environment: 'testnet' });
|
|
50
|
+
expect(sdk).toBeTruthy();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should delegate connect to the provider', async () => {
|
|
54
|
+
sdk = new UltraWalletSDK();
|
|
55
|
+
const params: ConnectParams = { onlyIfTrusted: true };
|
|
56
|
+
await sdk.connect(params);
|
|
57
|
+
expect(connectSpy).toHaveBeenCalledWith(params);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should delegate disconnect to the provider', async () => {
|
|
61
|
+
sdk = new UltraWalletSDK();
|
|
62
|
+
await sdk.disconnect();
|
|
63
|
+
expect(disconnectSpy).toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should delegate signMessage to the provider', async () => {
|
|
67
|
+
sdk = new UltraWalletSDK();
|
|
68
|
+
await sdk.signMessage('hello');
|
|
69
|
+
expect(signMessageSpy).toHaveBeenCalledWith('hello');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should delegate signTransaction to the provider', async () => {
|
|
73
|
+
sdk = new UltraWalletSDK();
|
|
74
|
+
const transaction = { contract: 'test', action: 'test', data: {}, authorizations: [] };
|
|
75
|
+
await sdk.signTransaction(transaction);
|
|
76
|
+
expect(signTransactionSpy).toHaveBeenCalledWith(transaction);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should delegate getChainId to the provider', async () => {
|
|
80
|
+
sdk = new UltraWalletSDK();
|
|
81
|
+
await sdk.getChainId();
|
|
82
|
+
expect(getChainIdSpy).toHaveBeenCalled();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should delegate purchaseItem to the provider', async () => {
|
|
86
|
+
sdk = new UltraWalletSDK();
|
|
87
|
+
await sdk.purchaseItem(PurchaseItemType.UNIQ_FACTORY, 'item123');
|
|
88
|
+
expect(purchaseItemSpy).toHaveBeenCalledWith('UniqFactory', 'item123');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BlockchainTransaction,
|
|
3
|
+
ConnectParams,
|
|
4
|
+
ConnectResult,
|
|
5
|
+
PurchaseItemResult,
|
|
6
|
+
PurchaseItemType,
|
|
7
|
+
SignMessageResult,
|
|
8
|
+
SignTransactionResult,
|
|
9
|
+
UltraResponse,
|
|
10
|
+
UltraWalletProvider,
|
|
11
|
+
UltraWalletSdkOptions,
|
|
12
|
+
} from './interfaces';
|
|
13
|
+
import { ExtensionProvider, WebProvider } from './providers';
|
|
14
|
+
import { isExtensionAvailable } from './utils/detection';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* UltraWalletClient acts as a Facade for interacting with the Ultra Wallet.
|
|
18
|
+
* It provides a unified API regardless of the underlying provider implementation (Extension or Web).
|
|
19
|
+
*/
|
|
20
|
+
export class UltraWalletSDK {
|
|
21
|
+
private provider: UltraWalletProvider;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Constructs an UltraWalletClient instance.
|
|
25
|
+
* If the Ultra Wallet Extension is available (window.ultra), it will be used automatically.
|
|
26
|
+
* If the Extension is not available, the Web Wallet provider will be initialized using the given network options.
|
|
27
|
+
*
|
|
28
|
+
* @param {UltraWalletSdkOptions} [options] - Configuration options for the Web Wallet fallback (environment or custom URL).
|
|
29
|
+
*/
|
|
30
|
+
constructor(private readonly options?: UltraWalletSdkOptions) {
|
|
31
|
+
this.provider = this.resolveWalletProvider();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
connect(params?: ConnectParams): Promise<UltraResponse<ConnectResult>> {
|
|
35
|
+
return this.provider.connect(params);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
disconnect(): Promise<UltraResponse<boolean>> {
|
|
39
|
+
return this.provider.disconnect();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
signMessage(message: string): Promise<UltraResponse<SignMessageResult>> {
|
|
43
|
+
return this.provider.signMessage(message);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
signTransaction(
|
|
47
|
+
transaction: BlockchainTransaction | BlockchainTransaction[],
|
|
48
|
+
): Promise<UltraResponse<SignTransactionResult>> {
|
|
49
|
+
return this.provider.signTransaction(transaction);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getChainId(): Promise<UltraResponse<string>> {
|
|
53
|
+
return this.provider.getChainId();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
purchaseItem(itemType: PurchaseItemType, itemId: string): Promise<UltraResponse<PurchaseItemResult>> {
|
|
57
|
+
return this.provider.purchaseItem(itemType, itemId);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private resolveWalletProvider(): UltraWalletProvider {
|
|
61
|
+
if (isExtensionAvailable()) {
|
|
62
|
+
return new ExtensionProvider();
|
|
63
|
+
} else {
|
|
64
|
+
return new WebProvider(this.options);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import UltraWalletSdkConfig from '../config/ultra-wallet-sdk.config';
|
|
2
|
+
|
|
3
|
+
export function isValidChain(targetNetwork: string, walletChainId: string): boolean {
|
|
4
|
+
const { chainId } = UltraWalletSdkConfig.networkInfo[targetNetwork] || {};
|
|
5
|
+
return !chainId || chainId === walletChainId;
|
|
6
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"forceConsistentCasingInFileNames": true,
|
|
5
|
+
"strict": true,
|
|
6
|
+
"noImplicitOverride": true,
|
|
7
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
8
|
+
"noImplicitReturns": true,
|
|
9
|
+
"noFallthroughCasesInSwitch": true,
|
|
10
|
+
"target": "es2020"
|
|
11
|
+
},
|
|
12
|
+
"files": [],
|
|
13
|
+
"include": [],
|
|
14
|
+
"references": [
|
|
15
|
+
{
|
|
16
|
+
"path": "./tsconfig.lib.json"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"path": "./tsconfig.spec.json"
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"angularCompilerOptions": {
|
|
23
|
+
"enableI18nLegacyMessageIdFormat": false,
|
|
24
|
+
"strictInjectionParameters": true,
|
|
25
|
+
"strictInputAccessModifiers": true,
|
|
26
|
+
"strictTemplates": true
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"declarationMap": true,
|
|
7
|
+
"inlineSources": true,
|
|
8
|
+
"types": ["chrome"]
|
|
9
|
+
},
|
|
10
|
+
"files": ["src/index.ts"],
|
|
11
|
+
"exclude": ["src/**/*.spec.ts", "jest.config.ts", "src/**/*.test.ts"]
|
|
12
|
+
}
|