@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.
@@ -1816,6 +1816,122 @@
1816
1816
  });
1817
1817
  }
1818
1818
 
1819
+ /**
1820
+ * 加密/解密 SDK API
1821
+ *
1822
+ * 调用钱包插件的 `yeying_encrypt` / `yeying_decrypt` / `yeying_getCipherSuites`
1823
+ * EIP-1193 自定义方法。命名安全套件 + 静默执行 + 站点授权模型同 UCAN。
1824
+ *
1825
+ * 数据密码(password 参数)由 DApp 自行管理与钱包密码独立;
1826
+ * plaintext 走 base64 字符串跨 message 边界传输。
1827
+ */
1828
+ class CipherError extends Error {
1829
+ type;
1830
+ code;
1831
+ originalError;
1832
+ constructor(message, type, code, originalError) {
1833
+ super(message);
1834
+ this.name = 'CipherError';
1835
+ this.type = type;
1836
+ this.code = code;
1837
+ this.originalError = originalError;
1838
+ }
1839
+ }
1840
+ function wrapCipherError(err) {
1841
+ const info = classifyWalletError(err);
1842
+ throw new CipherError(info.message, info.type, info.code, err);
1843
+ }
1844
+ function bytesToBase64(bytes) {
1845
+ if (typeof Buffer !== 'undefined') {
1846
+ return Buffer.from(bytes).toString('base64');
1847
+ }
1848
+ let binary = '';
1849
+ for (let i = 0; i < bytes.length; i += 1) {
1850
+ binary += String.fromCharCode(bytes[i]);
1851
+ }
1852
+ return btoa(binary);
1853
+ }
1854
+ function base64ToBytes(b64) {
1855
+ if (typeof Buffer !== 'undefined') {
1856
+ return new Uint8Array(Buffer.from(b64, 'base64'));
1857
+ }
1858
+ const binary = atob(b64);
1859
+ const out = new Uint8Array(binary.length);
1860
+ for (let i = 0; i < binary.length; i += 1)
1861
+ out[i] = binary.charCodeAt(i);
1862
+ return out;
1863
+ }
1864
+ /**
1865
+ * 用指定/默认套件加密数据
1866
+ * @returns v1 格式密文 base64 字符串
1867
+ */
1868
+ async function encrypt(options) {
1869
+ const provider = options.provider || (await requireProvider());
1870
+ try {
1871
+ const result = await provider.request({
1872
+ method: 'yeying_encrypt',
1873
+ params: [{
1874
+ data: options.data,
1875
+ password: options.password,
1876
+ suite: options.suite
1877
+ }]
1878
+ });
1879
+ if (!result || typeof result !== 'object' || typeof result.ciphertext !== 'string') {
1880
+ throw new Error('Invalid encrypt response: missing ciphertext');
1881
+ }
1882
+ return result.ciphertext;
1883
+ }
1884
+ catch (err) {
1885
+ wrapCipherError(err);
1886
+ }
1887
+ }
1888
+ /**
1889
+ * 解密 v1 格式密文
1890
+ * @returns 明文 Uint8Array
1891
+ */
1892
+ async function decrypt(options) {
1893
+ const provider = options.provider || (await requireProvider());
1894
+ try {
1895
+ const result = await provider.request({
1896
+ method: 'yeying_decrypt',
1897
+ params: [{
1898
+ ciphertext: options.ciphertext,
1899
+ password: options.password
1900
+ }]
1901
+ });
1902
+ if (!result || typeof result !== 'object' || typeof result.plaintext !== 'string') {
1903
+ throw new Error('Invalid decrypt response: missing plaintext');
1904
+ }
1905
+ const { plaintext, encoding } = result;
1906
+ if (encoding && encoding !== 'base64') {
1907
+ throw new Error(`Unsupported plaintext encoding: ${encoding}`);
1908
+ }
1909
+ return base64ToBytes(plaintext);
1910
+ }
1911
+ catch (err) {
1912
+ wrapCipherError(err);
1913
+ }
1914
+ }
1915
+ /**
1916
+ * 列出可用的命名安全套件
1917
+ */
1918
+ async function getSupportedCipherSuites(options = {}) {
1919
+ const provider = options.provider || (await requireProvider());
1920
+ try {
1921
+ const result = await provider.request({
1922
+ method: 'yeying_getCipherSuites',
1923
+ params: []
1924
+ });
1925
+ if (!result || typeof result !== 'object' || !Array.isArray(result.suites)) {
1926
+ throw new Error('Invalid getCipherSuites response: missing suites array');
1927
+ }
1928
+ return result.suites;
1929
+ }
1930
+ catch (err) {
1931
+ wrapCipherError(err);
1932
+ }
1933
+ }
1934
+
1819
1935
  function normalizeBaseUrl(baseUrl) {
1820
1936
  return baseUrl.replace(/\/+$/, '');
1821
1937
  }
@@ -2361,6 +2477,7 @@
2361
2477
  return result;
2362
2478
  }
2363
2479
 
2480
+ exports.CipherError = CipherError;
2364
2481
  exports.DEFAULT_UCAN_SESSION_TTL_MS = DEFAULT_UCAN_SESSION_TTL_MS;
2365
2482
  exports.DEFAULT_UCAN_TOKEN_SKEW_MS = DEFAULT_UCAN_TOKEN_SKEW_MS;
2366
2483
  exports.DEFAULT_UCAN_TOKEN_TTL_MS = DEFAULT_UCAN_TOKEN_TTL_MS;
@@ -2368,6 +2485,8 @@
2368
2485
  exports.authCentralUcanFetch = authCentralUcanFetch;
2369
2486
  exports.authFetch = authFetch;
2370
2487
  exports.authUcanFetch = authUcanFetch;
2488
+ exports.base64ToBytes = base64ToBytes;
2489
+ exports.bytesToBase64 = bytesToBase64;
2371
2490
  exports.classifyUcanAuthError = classifyUcanAuthError;
2372
2491
  exports.classifyWalletError = classifyWalletError;
2373
2492
  exports.clearAccessToken = clearAccessToken;
@@ -2381,8 +2500,10 @@
2381
2500
  exports.createUcanSession = createUcanSession;
2382
2501
  exports.createWebDavClient = createWebDavClient;
2383
2502
  exports.decodeUcanPayload = decodeUcanPayload;
2503
+ exports.decrypt = decrypt;
2384
2504
  exports.deriveAppIdFromHost = deriveAppIdFromHost;
2385
2505
  exports.deriveAppIdFromLocation = deriveAppIdFromLocation;
2506
+ exports.encrypt = encrypt;
2386
2507
  exports.focusPendingApproval = focusPendingApproval;
2387
2508
  exports.getAccessToken = getAccessToken;
2388
2509
  exports.getAccounts = getAccounts;
@@ -2397,6 +2518,7 @@
2397
2518
  exports.getPreferredAccount = getPreferredAccount;
2398
2519
  exports.getProvider = getProvider;
2399
2520
  exports.getStoredUcanRoot = getStoredUcanRoot;
2521
+ exports.getSupportedCipherSuites = getSupportedCipherSuites;
2400
2522
  exports.getUcanSession = getUcanSession;
2401
2523
  exports.getUcanTokenTiming = getUcanTokenTiming;
2402
2524
  exports.getWalletErrorCode = getWalletErrorCode;