@yeying-community/web3-bs 1.0.12 → 1.0.14
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/README.md +9 -1
- package/dist/auth/encrypt.d.ts +54 -0
- package/dist/auth/index.d.ts +1 -0
- package/dist/auth/provider.d.ts +2 -1
- package/dist/auth/types.d.ts +29 -0
- package/dist/web3-bs.esm.js +171 -1
- package/dist/web3-bs.esm.js.map +1 -1
- package/dist/web3-bs.umd.js +177 -0
- package/dist/web3-bs.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/web3-bs.umd.js
CHANGED
|
@@ -79,6 +79,17 @@
|
|
|
79
79
|
}
|
|
80
80
|
return accounts[0] || null;
|
|
81
81
|
}
|
|
82
|
+
function normalizeAccount(account) {
|
|
83
|
+
const normalized = (account || '').trim();
|
|
84
|
+
return normalized || null;
|
|
85
|
+
}
|
|
86
|
+
function isSameAccount(left, right) {
|
|
87
|
+
const normalizedLeft = normalizeAccount(left);
|
|
88
|
+
const normalizedRight = normalizeAccount(right);
|
|
89
|
+
return Boolean(normalizedLeft &&
|
|
90
|
+
normalizedRight &&
|
|
91
|
+
normalizedLeft.toLowerCase() === normalizedRight.toLowerCase());
|
|
92
|
+
}
|
|
82
93
|
function isYeYingProvider(provider, info) {
|
|
83
94
|
if (!provider)
|
|
84
95
|
return false;
|
|
@@ -342,6 +353,49 @@
|
|
|
342
353
|
writeStoredAccount(storageKey, account);
|
|
343
354
|
return { account, accounts };
|
|
344
355
|
}
|
|
356
|
+
async function resolveWalletAccount(options = {}) {
|
|
357
|
+
const provider = options.provider || (await requireProvider());
|
|
358
|
+
let accounts = await getAccounts(provider);
|
|
359
|
+
if (accounts.length === 0 && options.autoConnect) {
|
|
360
|
+
accounts = await requestAccounts({ provider });
|
|
361
|
+
}
|
|
362
|
+
const expectedAccount = normalizeAccount(options.expectedAccount);
|
|
363
|
+
const walletAccount = normalizeAccount(accounts[0]);
|
|
364
|
+
if (!walletAccount) {
|
|
365
|
+
return {
|
|
366
|
+
status: 'unavailable',
|
|
367
|
+
account: null,
|
|
368
|
+
walletAccount: null,
|
|
369
|
+
expectedAccount,
|
|
370
|
+
accounts,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
if (!expectedAccount) {
|
|
374
|
+
return {
|
|
375
|
+
status: 'wallet',
|
|
376
|
+
account: walletAccount,
|
|
377
|
+
walletAccount,
|
|
378
|
+
expectedAccount: null,
|
|
379
|
+
accounts,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
if (isSameAccount(expectedAccount, walletAccount)) {
|
|
383
|
+
return {
|
|
384
|
+
status: 'matched',
|
|
385
|
+
account: walletAccount,
|
|
386
|
+
walletAccount,
|
|
387
|
+
expectedAccount,
|
|
388
|
+
accounts,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
status: 'mismatch',
|
|
393
|
+
account: null,
|
|
394
|
+
walletAccount,
|
|
395
|
+
expectedAccount,
|
|
396
|
+
accounts,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
345
399
|
function watchAccounts(provider, handler, options = {}) {
|
|
346
400
|
const storageKey = options.storageKey || DEFAULT_ACCOUNT_STORAGE_KEY;
|
|
347
401
|
const preferStored = options.preferStored !== false;
|
|
@@ -1816,6 +1870,122 @@
|
|
|
1816
1870
|
});
|
|
1817
1871
|
}
|
|
1818
1872
|
|
|
1873
|
+
/**
|
|
1874
|
+
* 加密/解密 SDK API
|
|
1875
|
+
*
|
|
1876
|
+
* 调用钱包插件的 `yeying_encrypt` / `yeying_decrypt` / `yeying_getCipherSuites`
|
|
1877
|
+
* EIP-1193 自定义方法。命名安全套件 + 静默执行 + 站点授权模型同 UCAN。
|
|
1878
|
+
*
|
|
1879
|
+
* 数据密码(password 参数)由 DApp 自行管理与钱包密码独立;
|
|
1880
|
+
* plaintext 走 base64 字符串跨 message 边界传输。
|
|
1881
|
+
*/
|
|
1882
|
+
class CipherError extends Error {
|
|
1883
|
+
type;
|
|
1884
|
+
code;
|
|
1885
|
+
originalError;
|
|
1886
|
+
constructor(message, type, code, originalError) {
|
|
1887
|
+
super(message);
|
|
1888
|
+
this.name = 'CipherError';
|
|
1889
|
+
this.type = type;
|
|
1890
|
+
this.code = code;
|
|
1891
|
+
this.originalError = originalError;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
function wrapCipherError(err) {
|
|
1895
|
+
const info = classifyWalletError(err);
|
|
1896
|
+
throw new CipherError(info.message, info.type, info.code, err);
|
|
1897
|
+
}
|
|
1898
|
+
function bytesToBase64(bytes) {
|
|
1899
|
+
if (typeof Buffer !== 'undefined') {
|
|
1900
|
+
return Buffer.from(bytes).toString('base64');
|
|
1901
|
+
}
|
|
1902
|
+
let binary = '';
|
|
1903
|
+
for (let i = 0; i < bytes.length; i += 1) {
|
|
1904
|
+
binary += String.fromCharCode(bytes[i]);
|
|
1905
|
+
}
|
|
1906
|
+
return btoa(binary);
|
|
1907
|
+
}
|
|
1908
|
+
function base64ToBytes(b64) {
|
|
1909
|
+
if (typeof Buffer !== 'undefined') {
|
|
1910
|
+
return new Uint8Array(Buffer.from(b64, 'base64'));
|
|
1911
|
+
}
|
|
1912
|
+
const binary = atob(b64);
|
|
1913
|
+
const out = new Uint8Array(binary.length);
|
|
1914
|
+
for (let i = 0; i < binary.length; i += 1)
|
|
1915
|
+
out[i] = binary.charCodeAt(i);
|
|
1916
|
+
return out;
|
|
1917
|
+
}
|
|
1918
|
+
/**
|
|
1919
|
+
* 用指定/默认套件加密数据
|
|
1920
|
+
* @returns v1 格式密文 base64 字符串
|
|
1921
|
+
*/
|
|
1922
|
+
async function encrypt(options) {
|
|
1923
|
+
const provider = options.provider || (await requireProvider());
|
|
1924
|
+
try {
|
|
1925
|
+
const result = await provider.request({
|
|
1926
|
+
method: 'yeying_encrypt',
|
|
1927
|
+
params: [{
|
|
1928
|
+
data: options.data,
|
|
1929
|
+
password: options.password,
|
|
1930
|
+
suite: options.suite
|
|
1931
|
+
}]
|
|
1932
|
+
});
|
|
1933
|
+
if (!result || typeof result !== 'object' || typeof result.ciphertext !== 'string') {
|
|
1934
|
+
throw new Error('Invalid encrypt response: missing ciphertext');
|
|
1935
|
+
}
|
|
1936
|
+
return result.ciphertext;
|
|
1937
|
+
}
|
|
1938
|
+
catch (err) {
|
|
1939
|
+
wrapCipherError(err);
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* 解密 v1 格式密文
|
|
1944
|
+
* @returns 明文 Uint8Array
|
|
1945
|
+
*/
|
|
1946
|
+
async function decrypt(options) {
|
|
1947
|
+
const provider = options.provider || (await requireProvider());
|
|
1948
|
+
try {
|
|
1949
|
+
const result = await provider.request({
|
|
1950
|
+
method: 'yeying_decrypt',
|
|
1951
|
+
params: [{
|
|
1952
|
+
ciphertext: options.ciphertext,
|
|
1953
|
+
password: options.password
|
|
1954
|
+
}]
|
|
1955
|
+
});
|
|
1956
|
+
if (!result || typeof result !== 'object' || typeof result.plaintext !== 'string') {
|
|
1957
|
+
throw new Error('Invalid decrypt response: missing plaintext');
|
|
1958
|
+
}
|
|
1959
|
+
const { plaintext, encoding } = result;
|
|
1960
|
+
if (encoding && encoding !== 'base64') {
|
|
1961
|
+
throw new Error(`Unsupported plaintext encoding: ${encoding}`);
|
|
1962
|
+
}
|
|
1963
|
+
return base64ToBytes(plaintext);
|
|
1964
|
+
}
|
|
1965
|
+
catch (err) {
|
|
1966
|
+
wrapCipherError(err);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* 列出可用的命名安全套件
|
|
1971
|
+
*/
|
|
1972
|
+
async function getSupportedCipherSuites(options = {}) {
|
|
1973
|
+
const provider = options.provider || (await requireProvider());
|
|
1974
|
+
try {
|
|
1975
|
+
const result = await provider.request({
|
|
1976
|
+
method: 'yeying_getCipherSuites',
|
|
1977
|
+
params: []
|
|
1978
|
+
});
|
|
1979
|
+
if (!result || typeof result !== 'object' || !Array.isArray(result.suites)) {
|
|
1980
|
+
throw new Error('Invalid getCipherSuites response: missing suites array');
|
|
1981
|
+
}
|
|
1982
|
+
return result.suites;
|
|
1983
|
+
}
|
|
1984
|
+
catch (err) {
|
|
1985
|
+
wrapCipherError(err);
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1819
1989
|
function normalizeBaseUrl(baseUrl) {
|
|
1820
1990
|
return baseUrl.replace(/\/+$/, '');
|
|
1821
1991
|
}
|
|
@@ -2361,6 +2531,7 @@
|
|
|
2361
2531
|
return result;
|
|
2362
2532
|
}
|
|
2363
2533
|
|
|
2534
|
+
exports.CipherError = CipherError;
|
|
2364
2535
|
exports.DEFAULT_UCAN_SESSION_TTL_MS = DEFAULT_UCAN_SESSION_TTL_MS;
|
|
2365
2536
|
exports.DEFAULT_UCAN_TOKEN_SKEW_MS = DEFAULT_UCAN_TOKEN_SKEW_MS;
|
|
2366
2537
|
exports.DEFAULT_UCAN_TOKEN_TTL_MS = DEFAULT_UCAN_TOKEN_TTL_MS;
|
|
@@ -2368,6 +2539,8 @@
|
|
|
2368
2539
|
exports.authCentralUcanFetch = authCentralUcanFetch;
|
|
2369
2540
|
exports.authFetch = authFetch;
|
|
2370
2541
|
exports.authUcanFetch = authUcanFetch;
|
|
2542
|
+
exports.base64ToBytes = base64ToBytes;
|
|
2543
|
+
exports.bytesToBase64 = bytesToBase64;
|
|
2371
2544
|
exports.classifyUcanAuthError = classifyUcanAuthError;
|
|
2372
2545
|
exports.classifyWalletError = classifyWalletError;
|
|
2373
2546
|
exports.clearAccessToken = clearAccessToken;
|
|
@@ -2381,8 +2554,10 @@
|
|
|
2381
2554
|
exports.createUcanSession = createUcanSession;
|
|
2382
2555
|
exports.createWebDavClient = createWebDavClient;
|
|
2383
2556
|
exports.decodeUcanPayload = decodeUcanPayload;
|
|
2557
|
+
exports.decrypt = decrypt;
|
|
2384
2558
|
exports.deriveAppIdFromHost = deriveAppIdFromHost;
|
|
2385
2559
|
exports.deriveAppIdFromLocation = deriveAppIdFromLocation;
|
|
2560
|
+
exports.encrypt = encrypt;
|
|
2386
2561
|
exports.focusPendingApproval = focusPendingApproval;
|
|
2387
2562
|
exports.getAccessToken = getAccessToken;
|
|
2388
2563
|
exports.getAccounts = getAccounts;
|
|
@@ -2397,6 +2572,7 @@
|
|
|
2397
2572
|
exports.getPreferredAccount = getPreferredAccount;
|
|
2398
2573
|
exports.getProvider = getProvider;
|
|
2399
2574
|
exports.getStoredUcanRoot = getStoredUcanRoot;
|
|
2575
|
+
exports.getSupportedCipherSuites = getSupportedCipherSuites;
|
|
2400
2576
|
exports.getUcanSession = getUcanSession;
|
|
2401
2577
|
exports.getUcanTokenTiming = getUcanTokenTiming;
|
|
2402
2578
|
exports.getWalletErrorCode = getWalletErrorCode;
|
|
@@ -2419,6 +2595,7 @@
|
|
|
2419
2595
|
exports.refreshAccessToken = refreshAccessToken;
|
|
2420
2596
|
exports.requestAccounts = requestAccounts;
|
|
2421
2597
|
exports.requireProvider = requireProvider;
|
|
2598
|
+
exports.resolveWalletAccount = resolveWalletAccount;
|
|
2422
2599
|
exports.setAccessToken = setAccessToken;
|
|
2423
2600
|
exports.setCentralSessionToken = setCentralSessionToken;
|
|
2424
2601
|
exports.signMessage = signMessage;
|