@yeying-community/web3-bs 1.0.12 → 1.0.13

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.
@@ -0,0 +1,54 @@
1
+ /**
2
+ * 加密/解密 SDK API
3
+ *
4
+ * 调用钱包插件的 `yeying_encrypt` / `yeying_decrypt` / `yeying_getCipherSuites`
5
+ * EIP-1193 自定义方法。命名安全套件 + 静默执行 + 站点授权模型同 UCAN。
6
+ *
7
+ * 数据密码(password 参数)由 DApp 自行管理与钱包密码独立;
8
+ * plaintext 走 base64 字符串跨 message 边界传输。
9
+ */
10
+ import { isUserRejectedWalletAction } from './provider';
11
+ import type { Eip1193Provider } from './types';
12
+ export interface CipherSuiteInfo {
13
+ name: string;
14
+ description: string;
15
+ mode: 'hash' | 'symmetric';
16
+ }
17
+ export interface EncryptOptions {
18
+ data: string | Uint8Array;
19
+ password: string;
20
+ suite?: string;
21
+ provider?: Eip1193Provider;
22
+ }
23
+ export interface DecryptOptions {
24
+ ciphertext: string;
25
+ password: string;
26
+ provider?: Eip1193Provider;
27
+ }
28
+ export interface GetCipherSuitesOptions {
29
+ provider?: Eip1193Provider;
30
+ }
31
+ export declare class CipherError extends Error {
32
+ readonly type: 'userRejected' | 'disconnected' | 'timeout' | 'notFound' | 'unknown';
33
+ readonly code: number | null;
34
+ readonly originalError: unknown;
35
+ constructor(message: string, type: CipherError['type'], code: number | null, originalError: unknown);
36
+ }
37
+ declare function bytesToBase64(bytes: Uint8Array): string;
38
+ declare function base64ToBytes(b64: string): Uint8Array;
39
+ /**
40
+ * 用指定/默认套件加密数据
41
+ * @returns v1 格式密文 base64 字符串
42
+ */
43
+ export declare function encrypt(options: EncryptOptions): Promise<string>;
44
+ /**
45
+ * 解密 v1 格式密文
46
+ * @returns 明文 Uint8Array
47
+ */
48
+ export declare function decrypt(options: DecryptOptions): Promise<Uint8Array>;
49
+ /**
50
+ * 列出可用的命名安全套件
51
+ */
52
+ export declare function getSupportedCipherSuites(options?: GetCipherSuitesOptions): Promise<CipherSuiteInfo[]>;
53
+ export { bytesToBase64, base64ToBytes };
54
+ export { isUserRejectedWalletAction };
@@ -3,3 +3,4 @@ export * from './provider';
3
3
  export * from './siwe';
4
4
  export * from './ucan';
5
5
  export * from './central';
6
+ export * from './encrypt';
@@ -1810,6 +1810,122 @@ async function authCentralUcanFetch(input, init = {}, options = {}) {
1810
1810
  });
1811
1811
  }
1812
1812
 
1813
+ /**
1814
+ * 加密/解密 SDK API
1815
+ *
1816
+ * 调用钱包插件的 `yeying_encrypt` / `yeying_decrypt` / `yeying_getCipherSuites`
1817
+ * EIP-1193 自定义方法。命名安全套件 + 静默执行 + 站点授权模型同 UCAN。
1818
+ *
1819
+ * 数据密码(password 参数)由 DApp 自行管理与钱包密码独立;
1820
+ * plaintext 走 base64 字符串跨 message 边界传输。
1821
+ */
1822
+ class CipherError extends Error {
1823
+ type;
1824
+ code;
1825
+ originalError;
1826
+ constructor(message, type, code, originalError) {
1827
+ super(message);
1828
+ this.name = 'CipherError';
1829
+ this.type = type;
1830
+ this.code = code;
1831
+ this.originalError = originalError;
1832
+ }
1833
+ }
1834
+ function wrapCipherError(err) {
1835
+ const info = classifyWalletError(err);
1836
+ throw new CipherError(info.message, info.type, info.code, err);
1837
+ }
1838
+ function bytesToBase64(bytes) {
1839
+ if (typeof Buffer !== 'undefined') {
1840
+ return Buffer.from(bytes).toString('base64');
1841
+ }
1842
+ let binary = '';
1843
+ for (let i = 0; i < bytes.length; i += 1) {
1844
+ binary += String.fromCharCode(bytes[i]);
1845
+ }
1846
+ return btoa(binary);
1847
+ }
1848
+ function base64ToBytes(b64) {
1849
+ if (typeof Buffer !== 'undefined') {
1850
+ return new Uint8Array(Buffer.from(b64, 'base64'));
1851
+ }
1852
+ const binary = atob(b64);
1853
+ const out = new Uint8Array(binary.length);
1854
+ for (let i = 0; i < binary.length; i += 1)
1855
+ out[i] = binary.charCodeAt(i);
1856
+ return out;
1857
+ }
1858
+ /**
1859
+ * 用指定/默认套件加密数据
1860
+ * @returns v1 格式密文 base64 字符串
1861
+ */
1862
+ async function encrypt(options) {
1863
+ const provider = options.provider || (await requireProvider());
1864
+ try {
1865
+ const result = await provider.request({
1866
+ method: 'yeying_encrypt',
1867
+ params: [{
1868
+ data: options.data,
1869
+ password: options.password,
1870
+ suite: options.suite
1871
+ }]
1872
+ });
1873
+ if (!result || typeof result !== 'object' || typeof result.ciphertext !== 'string') {
1874
+ throw new Error('Invalid encrypt response: missing ciphertext');
1875
+ }
1876
+ return result.ciphertext;
1877
+ }
1878
+ catch (err) {
1879
+ wrapCipherError(err);
1880
+ }
1881
+ }
1882
+ /**
1883
+ * 解密 v1 格式密文
1884
+ * @returns 明文 Uint8Array
1885
+ */
1886
+ async function decrypt(options) {
1887
+ const provider = options.provider || (await requireProvider());
1888
+ try {
1889
+ const result = await provider.request({
1890
+ method: 'yeying_decrypt',
1891
+ params: [{
1892
+ ciphertext: options.ciphertext,
1893
+ password: options.password
1894
+ }]
1895
+ });
1896
+ if (!result || typeof result !== 'object' || typeof result.plaintext !== 'string') {
1897
+ throw new Error('Invalid decrypt response: missing plaintext');
1898
+ }
1899
+ const { plaintext, encoding } = result;
1900
+ if (encoding && encoding !== 'base64') {
1901
+ throw new Error(`Unsupported plaintext encoding: ${encoding}`);
1902
+ }
1903
+ return base64ToBytes(plaintext);
1904
+ }
1905
+ catch (err) {
1906
+ wrapCipherError(err);
1907
+ }
1908
+ }
1909
+ /**
1910
+ * 列出可用的命名安全套件
1911
+ */
1912
+ async function getSupportedCipherSuites(options = {}) {
1913
+ const provider = options.provider || (await requireProvider());
1914
+ try {
1915
+ const result = await provider.request({
1916
+ method: 'yeying_getCipherSuites',
1917
+ params: []
1918
+ });
1919
+ if (!result || typeof result !== 'object' || !Array.isArray(result.suites)) {
1920
+ throw new Error('Invalid getCipherSuites response: missing suites array');
1921
+ }
1922
+ return result.suites;
1923
+ }
1924
+ catch (err) {
1925
+ wrapCipherError(err);
1926
+ }
1927
+ }
1928
+
1813
1929
  function normalizeBaseUrl(baseUrl) {
1814
1930
  return baseUrl.replace(/\/+$/, '');
1815
1931
  }
@@ -2355,5 +2471,5 @@ async function initDappSession(options) {
2355
2471
  return result;
2356
2472
  }
2357
2473
 
2358
- export { DEFAULT_UCAN_SESSION_TTL_MS, DEFAULT_UCAN_TOKEN_SKEW_MS, DEFAULT_UCAN_TOKEN_TTL_MS, WebDavClient, authCentralUcanFetch, authFetch, authUcanFetch, classifyUcanAuthError, classifyWalletError, clearAccessToken, clearCentralSessionToken, clearUcanSession, createAndIssueCentralUcan, createCentralSession, createDelegationUcan, createInvocationUcan, createRootUcan, createUcanSession, createWebDavClient, decodeUcanPayload, deriveAppIdFromHost, deriveAppIdFromLocation, focusPendingApproval, getAccessToken, getAccounts, getBalance, getCapabilityAction, getCapabilityResource, getCentralIssuerInfo, getCentralSessionToken, getChainId, getOrCreateInvocationUcan, getOrCreateUcanRoot, getPreferredAccount, getProvider, getStoredUcanRoot, getUcanSession, getUcanTokenTiming, getWalletErrorCode, getWalletErrorMessage, initDappSession, initWebDavStorage, isUcanTokenFresh, isUserRejectedWalletAction, isWalletReconnectError, isYeYingProvider, issueCentralUcan, loginWithChallenge, logout, normalizeAppHostnameForAppId, normalizeUcanCapabilities, normalizeUcanCapability, normalizeUcanExpiry, onAccountsChanged, onChainChanged, refreshAccessToken, requestAccounts, requireProvider, setAccessToken, setCentralSessionToken, signMessage, storeUcanRoot, watchAccounts, watchProvider };
2474
+ export { CipherError, DEFAULT_UCAN_SESSION_TTL_MS, DEFAULT_UCAN_TOKEN_SKEW_MS, DEFAULT_UCAN_TOKEN_TTL_MS, WebDavClient, authCentralUcanFetch, authFetch, authUcanFetch, base64ToBytes, bytesToBase64, classifyUcanAuthError, classifyWalletError, clearAccessToken, clearCentralSessionToken, clearUcanSession, createAndIssueCentralUcan, createCentralSession, createDelegationUcan, createInvocationUcan, createRootUcan, createUcanSession, createWebDavClient, decodeUcanPayload, decrypt, deriveAppIdFromHost, deriveAppIdFromLocation, encrypt, focusPendingApproval, getAccessToken, getAccounts, getBalance, getCapabilityAction, getCapabilityResource, getCentralIssuerInfo, getCentralSessionToken, getChainId, getOrCreateInvocationUcan, getOrCreateUcanRoot, getPreferredAccount, getProvider, getStoredUcanRoot, getSupportedCipherSuites, getUcanSession, getUcanTokenTiming, getWalletErrorCode, getWalletErrorMessage, initDappSession, initWebDavStorage, isUcanTokenFresh, isUserRejectedWalletAction, isWalletReconnectError, isYeYingProvider, issueCentralUcan, loginWithChallenge, logout, normalizeAppHostnameForAppId, normalizeUcanCapabilities, normalizeUcanCapability, normalizeUcanExpiry, onAccountsChanged, onChainChanged, refreshAccessToken, requestAccounts, requireProvider, setAccessToken, setCentralSessionToken, signMessage, storeUcanRoot, watchAccounts, watchProvider };
2359
2475
  //# sourceMappingURL=web3-bs.esm.js.map