@dynamic-labs/midnight 4.82.0 → 4.83.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/CHANGELOG.md +21 -853
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +6 -6
- package/src/injected/fetchInjectedWalletConnectors/fetchInjectedWalletConnectors.cjs +34 -1
- package/src/injected/fetchInjectedWalletConnectors/fetchInjectedWalletConnectors.js +35 -2
- package/src/utils/MidnightUiTransaction/MidnightUiTransaction.d.ts +38 -0
- package/src/utils/MidnightUiTransaction/index.d.ts +1 -0
- package/src/utils/parseMidnightAtomicAmount/index.d.ts +1 -0
- package/src/utils/parseMidnightAtomicAmount/parseMidnightAtomicAmount.cjs +33 -0
- package/src/utils/parseMidnightAtomicAmount/parseMidnightAtomicAmount.d.ts +1 -0
- package/src/utils/parseMidnightAtomicAmount/parseMidnightAtomicAmount.js +29 -0
- package/src/wallet/MidnightWallet.cjs +5 -1
- package/src/wallet/MidnightWallet.js +6 -2
package/package.cjs
CHANGED
package/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/midnight",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.83.1",
|
|
4
4
|
"description": "A React SDK for implementing wallet web3 authentication and authorization to your website.",
|
|
5
5
|
"author": "Dynamic Labs, Inc.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
},
|
|
19
19
|
"homepage": "https://www.dynamic.xyz/",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dynamic-labs/assert-package-version": "4.
|
|
21
|
+
"@dynamic-labs/assert-package-version": "4.83.1",
|
|
22
22
|
"@dynamic-labs/sdk-api-core": "0.0.964",
|
|
23
23
|
"@midnight-ntwrk/dapp-connector-api": "4.0.1",
|
|
24
|
-
"@dynamic-labs/types": "4.
|
|
25
|
-
"@dynamic-labs/utils": "4.
|
|
26
|
-
"@dynamic-labs/wallet-book": "4.
|
|
27
|
-
"@dynamic-labs/wallet-connector-core": "4.
|
|
24
|
+
"@dynamic-labs/types": "4.83.1",
|
|
25
|
+
"@dynamic-labs/utils": "4.83.1",
|
|
26
|
+
"@dynamic-labs/wallet-book": "4.83.1",
|
|
27
|
+
"@dynamic-labs/wallet-connector-core": "4.83.1"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {}
|
|
30
30
|
}
|
|
@@ -3,11 +3,33 @@
|
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
|
+
var utils = require('@dynamic-labs/utils');
|
|
6
7
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
7
8
|
var getConnectorConstructorInjectedWallet = require('../../utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.cjs');
|
|
8
9
|
var constants = require('../../constants.cjs');
|
|
9
10
|
|
|
10
11
|
const injectedWalletOverrides = [];
|
|
12
|
+
const isProviderInWindow = (windowLocations) => windowLocations.some((loc) => { var _a; return (_a = utils.getProvidersFromWindow(loc)[0]) === null || _a === void 0 ? void 0 : _a.rdns; });
|
|
13
|
+
// Polls until the Midnight provider appears in window, then emits providerInjected
|
|
14
|
+
// so WalletConnectorEvents can instantiate the connector and mark it as installed.
|
|
15
|
+
// Called once per connector at SDK init — duplicate emissions are harmless because
|
|
16
|
+
// WalletConnectorEvents deduplicates incoming connectors by key.
|
|
17
|
+
const watchForInjectedProvider = (key, windowLocations, Constructor, timeoutMs = 3000) => {
|
|
18
|
+
const deadline = Date.now() + timeoutMs;
|
|
19
|
+
const poll = () => {
|
|
20
|
+
if (isProviderInWindow(windowLocations)) {
|
|
21
|
+
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[MIDNIGHT fetchInjectedWalletConnectors] provider ready, emitting providerInjected', { key });
|
|
22
|
+
walletConnectorCore.walletConnectorEvents.emit('providerInjected', {
|
|
23
|
+
injectedConnectorConstructor: Constructor,
|
|
24
|
+
});
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (Date.now() >= deadline)
|
|
28
|
+
return;
|
|
29
|
+
setTimeout(poll, 100);
|
|
30
|
+
};
|
|
31
|
+
setTimeout(poll, 100);
|
|
32
|
+
};
|
|
11
33
|
const fetchInjectedWalletConnectors = ({ walletBook, walletsWithCustomConnectors, }) => {
|
|
12
34
|
var _a;
|
|
13
35
|
const walletBookConnectors = Object.entries((_a = walletBook === null || walletBook === void 0 ? void 0 : walletBook.wallets) !== null && _a !== void 0 ? _a : {})
|
|
@@ -16,8 +38,19 @@ const fetchInjectedWalletConnectors = ({ walletBook, walletsWithCustomConnectors
|
|
|
16
38
|
return ((_a = wallet.injectedConfig) === null || _a === void 0 ? void 0 : _a.find((config) => config.chain === constants.MIDNIGHT_CHAIN)) && !walletsWithCustomConnectors.includes(key);
|
|
17
39
|
})
|
|
18
40
|
.map(([key, wallet]) => {
|
|
41
|
+
var _a, _b, _c;
|
|
19
42
|
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[MIDNIGHT fetchInjectedWalletConnectors] creating connector from wallet-book', { key, name: wallet.name });
|
|
20
|
-
|
|
43
|
+
const Constructor = getConnectorConstructorInjectedWallet.getConnectorConstructorInjectedWallet(key, wallet);
|
|
44
|
+
const windowLocations = (_c = (_b = (_a = wallet.injectedConfig) === null || _a === void 0 ? void 0 : _a.find((c) => c.chain === constants.MIDNIGHT_CHAIN)) === null || _b === void 0 ? void 0 : _b.windowLocations) !== null && _c !== void 0 ? _c : [];
|
|
45
|
+
if (!isProviderInWindow(windowLocations)) {
|
|
46
|
+
watchForInjectedProvider(key, windowLocations, Constructor);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
walletConnectorCore.walletConnectorEvents.emit('providerInjected', {
|
|
50
|
+
injectedConnectorConstructor: Constructor,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return Constructor;
|
|
21
54
|
});
|
|
22
55
|
return walletBookConnectors;
|
|
23
56
|
};
|
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
'use client'
|
|
2
|
-
import {
|
|
2
|
+
import { getProvidersFromWindow } from '@dynamic-labs/utils';
|
|
3
|
+
import { logger, walletConnectorEvents } from '@dynamic-labs/wallet-connector-core';
|
|
3
4
|
import { getConnectorConstructorInjectedWallet } from '../../utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.js';
|
|
4
5
|
import { MIDNIGHT_CHAIN } from '../../constants.js';
|
|
5
6
|
|
|
6
7
|
const injectedWalletOverrides = [];
|
|
8
|
+
const isProviderInWindow = (windowLocations) => windowLocations.some((loc) => { var _a; return (_a = getProvidersFromWindow(loc)[0]) === null || _a === void 0 ? void 0 : _a.rdns; });
|
|
9
|
+
// Polls until the Midnight provider appears in window, then emits providerInjected
|
|
10
|
+
// so WalletConnectorEvents can instantiate the connector and mark it as installed.
|
|
11
|
+
// Called once per connector at SDK init — duplicate emissions are harmless because
|
|
12
|
+
// WalletConnectorEvents deduplicates incoming connectors by key.
|
|
13
|
+
const watchForInjectedProvider = (key, windowLocations, Constructor, timeoutMs = 3000) => {
|
|
14
|
+
const deadline = Date.now() + timeoutMs;
|
|
15
|
+
const poll = () => {
|
|
16
|
+
if (isProviderInWindow(windowLocations)) {
|
|
17
|
+
logger.logVerboseTroubleshootingMessage('[MIDNIGHT fetchInjectedWalletConnectors] provider ready, emitting providerInjected', { key });
|
|
18
|
+
walletConnectorEvents.emit('providerInjected', {
|
|
19
|
+
injectedConnectorConstructor: Constructor,
|
|
20
|
+
});
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (Date.now() >= deadline)
|
|
24
|
+
return;
|
|
25
|
+
setTimeout(poll, 100);
|
|
26
|
+
};
|
|
27
|
+
setTimeout(poll, 100);
|
|
28
|
+
};
|
|
7
29
|
const fetchInjectedWalletConnectors = ({ walletBook, walletsWithCustomConnectors, }) => {
|
|
8
30
|
var _a;
|
|
9
31
|
const walletBookConnectors = Object.entries((_a = walletBook === null || walletBook === void 0 ? void 0 : walletBook.wallets) !== null && _a !== void 0 ? _a : {})
|
|
@@ -12,8 +34,19 @@ const fetchInjectedWalletConnectors = ({ walletBook, walletsWithCustomConnectors
|
|
|
12
34
|
return ((_a = wallet.injectedConfig) === null || _a === void 0 ? void 0 : _a.find((config) => config.chain === MIDNIGHT_CHAIN)) && !walletsWithCustomConnectors.includes(key);
|
|
13
35
|
})
|
|
14
36
|
.map(([key, wallet]) => {
|
|
37
|
+
var _a, _b, _c;
|
|
15
38
|
logger.logVerboseTroubleshootingMessage('[MIDNIGHT fetchInjectedWalletConnectors] creating connector from wallet-book', { key, name: wallet.name });
|
|
16
|
-
|
|
39
|
+
const Constructor = getConnectorConstructorInjectedWallet(key, wallet);
|
|
40
|
+
const windowLocations = (_c = (_b = (_a = wallet.injectedConfig) === null || _a === void 0 ? void 0 : _a.find((c) => c.chain === MIDNIGHT_CHAIN)) === null || _b === void 0 ? void 0 : _b.windowLocations) !== null && _c !== void 0 ? _c : [];
|
|
41
|
+
if (!isProviderInWindow(windowLocations)) {
|
|
42
|
+
watchForInjectedProvider(key, windowLocations, Constructor);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
walletConnectorEvents.emit('providerInjected', {
|
|
46
|
+
injectedConnectorConstructor: Constructor,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return Constructor;
|
|
17
50
|
});
|
|
18
51
|
return walletBookConnectors;
|
|
19
52
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { TokenBalance } from '@dynamic-labs/sdk-api-core';
|
|
2
|
+
import type { IUITransaction } from '@dynamic-labs/types';
|
|
3
|
+
type MidnightPool = 'unshielded' | 'shielded';
|
|
4
|
+
type MidnightUiTransactionOpts = {
|
|
5
|
+
from: string;
|
|
6
|
+
onSubmit: (tx: MidnightUiTransaction) => Promise<void>;
|
|
7
|
+
getUnshieldedBalance: () => Promise<bigint>;
|
|
8
|
+
getShieldedBalance: () => Promise<bigint>;
|
|
9
|
+
fetchDustFee: () => Promise<bigint>;
|
|
10
|
+
};
|
|
11
|
+
export declare class MidnightUiTransaction implements IUITransaction {
|
|
12
|
+
from: string;
|
|
13
|
+
to: string | undefined;
|
|
14
|
+
value: bigint | undefined;
|
|
15
|
+
data: string | undefined;
|
|
16
|
+
receipt: string | undefined;
|
|
17
|
+
chain: string;
|
|
18
|
+
fee: {
|
|
19
|
+
gas: bigint | undefined;
|
|
20
|
+
};
|
|
21
|
+
feeTokenSymbol: string;
|
|
22
|
+
private pool;
|
|
23
|
+
private readonly onSubmitFn;
|
|
24
|
+
private readonly getUnshieldedBalanceFn;
|
|
25
|
+
private readonly getShieldedBalanceFn;
|
|
26
|
+
private readonly fetchDustFeeFn;
|
|
27
|
+
constructor(opts: MidnightUiTransactionOpts);
|
|
28
|
+
get activePool(): MidnightPool;
|
|
29
|
+
setSelectedToken(contractAddress: string): void;
|
|
30
|
+
getSendableTokenBalances(): Promise<TokenBalance[]>;
|
|
31
|
+
parse(input: string): bigint;
|
|
32
|
+
format(value: bigint): string;
|
|
33
|
+
getBalance(): Promise<bigint>;
|
|
34
|
+
fetchFee(): Promise<void>;
|
|
35
|
+
validateAddressFormat(address: string): boolean;
|
|
36
|
+
submit(): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MidnightUiTransaction } from './MidnightUiTransaction';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { parseMidnightAtomicAmount } from './parseMidnightAtomicAmount';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
// parseFloat("0.1") * 1e6 yields 100000.00000000001 due to IEEE 754. We use
|
|
7
|
+
// BigInt arithmetic throughout to avoid any floating-point rounding error.
|
|
8
|
+
const parseMidnightAtomicAmount = (amount, decimals) => {
|
|
9
|
+
const trimmed = amount.trim();
|
|
10
|
+
if (!/^\d+(\.\d*)?$/.test(trimmed)) {
|
|
11
|
+
throw new Error(`parseMidnightAtomicAmount: invalid amount "${amount}"`);
|
|
12
|
+
}
|
|
13
|
+
const dotIndex = trimmed.indexOf('.');
|
|
14
|
+
const wholeStr = dotIndex !== -1 ? trimmed.slice(0, dotIndex) : trimmed;
|
|
15
|
+
const rawFracStr = dotIndex !== -1 ? trimmed.slice(dotIndex + 1) : '';
|
|
16
|
+
// Truncate excess digits — ledger behaviour is truncation, not rounding.
|
|
17
|
+
const fracStr = rawFracStr.slice(0, decimals);
|
|
18
|
+
const scale = bigIntPow10(decimals);
|
|
19
|
+
// Arithmetic equivalent of right-padding fracStr with zeros to `decimals` length.
|
|
20
|
+
const fracScale = bigIntPow10(decimals - fracStr.length);
|
|
21
|
+
return BigInt(wholeStr) * scale + BigInt(fracStr || '0') * fracScale;
|
|
22
|
+
};
|
|
23
|
+
// BigInt literal syntax (10n) and BigInt ** require ES2020/ES2016 targets.
|
|
24
|
+
// This loop stays correct for any precision and compiles at ES6.
|
|
25
|
+
const bigIntPow10 = (exp) => {
|
|
26
|
+
let result = BigInt(1);
|
|
27
|
+
const ten = BigInt(10);
|
|
28
|
+
for (let i = 0; i < exp; i++)
|
|
29
|
+
result = result * ten;
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.parseMidnightAtomicAmount = parseMidnightAtomicAmount;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const parseMidnightAtomicAmount: (amount: string, decimals: number) => bigint;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
// parseFloat("0.1") * 1e6 yields 100000.00000000001 due to IEEE 754. We use
|
|
3
|
+
// BigInt arithmetic throughout to avoid any floating-point rounding error.
|
|
4
|
+
const parseMidnightAtomicAmount = (amount, decimals) => {
|
|
5
|
+
const trimmed = amount.trim();
|
|
6
|
+
if (!/^\d+(\.\d*)?$/.test(trimmed)) {
|
|
7
|
+
throw new Error(`parseMidnightAtomicAmount: invalid amount "${amount}"`);
|
|
8
|
+
}
|
|
9
|
+
const dotIndex = trimmed.indexOf('.');
|
|
10
|
+
const wholeStr = dotIndex !== -1 ? trimmed.slice(0, dotIndex) : trimmed;
|
|
11
|
+
const rawFracStr = dotIndex !== -1 ? trimmed.slice(dotIndex + 1) : '';
|
|
12
|
+
// Truncate excess digits — ledger behaviour is truncation, not rounding.
|
|
13
|
+
const fracStr = rawFracStr.slice(0, decimals);
|
|
14
|
+
const scale = bigIntPow10(decimals);
|
|
15
|
+
// Arithmetic equivalent of right-padding fracStr with zeros to `decimals` length.
|
|
16
|
+
const fracScale = bigIntPow10(decimals - fracStr.length);
|
|
17
|
+
return BigInt(wholeStr) * scale + BigInt(fracStr || '0') * fracScale;
|
|
18
|
+
};
|
|
19
|
+
// BigInt literal syntax (10n) and BigInt ** require ES2020/ES2016 targets.
|
|
20
|
+
// This loop stays correct for any precision and compiles at ES6.
|
|
21
|
+
const bigIntPow10 = (exp) => {
|
|
22
|
+
let result = BigInt(1);
|
|
23
|
+
const ten = BigInt(10);
|
|
24
|
+
for (let i = 0; i < exp; i++)
|
|
25
|
+
result = result * ten;
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { parseMidnightAtomicAmount };
|
|
@@ -8,17 +8,21 @@ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
|
8
8
|
var constants = require('../constants.cjs');
|
|
9
9
|
var formatMidnightConnectorDustBalance = require('../utils/formatMidnightConnectorDustBalance/formatMidnightConnectorDustBalance.cjs');
|
|
10
10
|
var formatMidnightNativeUnshieldedBalance = require('../utils/formatMidnightNativeUnshieldedBalance/formatMidnightNativeUnshieldedBalance.cjs');
|
|
11
|
+
var parseMidnightAtomicAmount = require('../utils/parseMidnightAtomicAmount/parseMidnightAtomicAmount.cjs');
|
|
11
12
|
|
|
12
13
|
class MidnightWallet extends walletConnectorCore.Wallet {
|
|
13
14
|
sendBalance(_a) {
|
|
14
15
|
return _tslib.__awaiter(this, arguments, void 0, function* ({ amount, toAddress, token, }) {
|
|
16
|
+
var _b;
|
|
15
17
|
// Midnight address format: shielded addresses begin with "mn_shield",
|
|
16
18
|
// all others are unshielded. This prefix is defined by the Midnight protocol.
|
|
17
19
|
const type = toAddress.startsWith('mn_shield') ? 'shielded' : 'unshielded';
|
|
20
|
+
const decimals = (_b = token === null || token === void 0 ? void 0 : token.decimals) !== null && _b !== void 0 ? _b : constants.MIDNIGHT_NATIVE_UNSHIELDED_DECIMALS;
|
|
21
|
+
const atomicAmount = parseMidnightAtomicAmount.parseMidnightAtomicAmount(amount, decimals).toString();
|
|
18
22
|
const { serializedTransaction } = yield this._connector.createTransferTransaction({
|
|
19
23
|
transfers: [
|
|
20
24
|
{
|
|
21
|
-
amount,
|
|
25
|
+
amount: atomicAmount,
|
|
22
26
|
recipientAddress: toAddress,
|
|
23
27
|
tokenType: token === null || token === void 0 ? void 0 : token.address,
|
|
24
28
|
type,
|
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
3
|
import { Wallet } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
-
import { MIDNIGHT_NATIVE_SHIELDED_TOKEN_TYPE, MIDNIGHT_NATIVE_TOKEN_TYPE } from '../constants.js';
|
|
4
|
+
import { MIDNIGHT_NATIVE_UNSHIELDED_DECIMALS, MIDNIGHT_NATIVE_SHIELDED_TOKEN_TYPE, MIDNIGHT_NATIVE_TOKEN_TYPE } from '../constants.js';
|
|
5
5
|
import { formatMidnightConnectorDustBalance } from '../utils/formatMidnightConnectorDustBalance/formatMidnightConnectorDustBalance.js';
|
|
6
6
|
import { formatMidnightNativeUnshieldedBalance } from '../utils/formatMidnightNativeUnshieldedBalance/formatMidnightNativeUnshieldedBalance.js';
|
|
7
|
+
import { parseMidnightAtomicAmount } from '../utils/parseMidnightAtomicAmount/parseMidnightAtomicAmount.js';
|
|
7
8
|
|
|
8
9
|
class MidnightWallet extends Wallet {
|
|
9
10
|
sendBalance(_a) {
|
|
10
11
|
return __awaiter(this, arguments, void 0, function* ({ amount, toAddress, token, }) {
|
|
12
|
+
var _b;
|
|
11
13
|
// Midnight address format: shielded addresses begin with "mn_shield",
|
|
12
14
|
// all others are unshielded. This prefix is defined by the Midnight protocol.
|
|
13
15
|
const type = toAddress.startsWith('mn_shield') ? 'shielded' : 'unshielded';
|
|
16
|
+
const decimals = (_b = token === null || token === void 0 ? void 0 : token.decimals) !== null && _b !== void 0 ? _b : MIDNIGHT_NATIVE_UNSHIELDED_DECIMALS;
|
|
17
|
+
const atomicAmount = parseMidnightAtomicAmount(amount, decimals).toString();
|
|
14
18
|
const { serializedTransaction } = yield this._connector.createTransferTransaction({
|
|
15
19
|
transfers: [
|
|
16
20
|
{
|
|
17
|
-
amount,
|
|
21
|
+
amount: atomicAmount,
|
|
18
22
|
recipientAddress: toAddress,
|
|
19
23
|
tokenType: token === null || token === void 0 ? void 0 : token.address,
|
|
20
24
|
type,
|