Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: new sbtc network #5982

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@
"@hirosystems/token-metadata-api-client": "1.2.0",
"@hookform/resolvers": "3.9.1",
"@leather.io/analytics": "2.0.1",
"@leather.io/bitcoin": "0.16.4",
"@leather.io/constants": "0.13.2",
"@leather.io/crypto": "1.6.11",
"@leather.io/models": "0.19.1",
"@leather.io/query": "2.22.3",
"@leather.io/stacks": "1.3.4",
"@leather.io/bitcoin": "0.16.5",
"@leather.io/constants": "0.13.3",
"@leather.io/crypto": "1.6.12",
"@leather.io/models": "0.20.0",
"@leather.io/query": "2.22.4",
"@leather.io/stacks": "1.3.5",
"@leather.io/tokens": "0.12.1",
"@leather.io/ui": "1.36.0",
"@leather.io/utils": "0.19.0",
"@leather.io/ui": "1.36.1",
"@leather.io/utils": "0.19.1",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.5.0",
"@noble/secp256k1": "2.1.0",
Expand Down Expand Up @@ -270,7 +270,7 @@
"@leather.io/eslint-config": "0.7.0",
"@leather.io/panda-preset": "0.5.2",
"@leather.io/prettier-config": "0.6.0",
"@leather.io/rpc": "2.1.17",
"@leather.io/rpc": "2.1.18",
"@ls-lint/ls-lint": "2.2.3",
"@mdx-js/loader": "3.0.0",
"@pandacss/dev": "0.46.1",
Expand Down
2,032 changes: 1,010 additions & 1,022 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/shared/rpc/methods/send-transfer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,18 @@ describe('`sendTransfer` method', () => {
);
expect(result.success).toEqual(false);
});

test('that it supports regtest addresses', () => {
const result = rpcSendTransferParamsSchema.safeParse({
network: 'sbtcTestnet',
account: 0,
recipients: [
{
address: 'bcrt1pzxz00vdnc8j4rz6u9axp6jmh60f8v7t8zkmwzm4x3899fvvl78nseyz7r2',
amount: '100',
},
],
});
expect(result.success).toEqual(true);
});
});
32 changes: 27 additions & 5 deletions src/shared/rpc/methods/send-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SendTransferRequestParams } from '@btckit/types';
import { z } from 'zod';

import { type BitcoinNetworkModes } from '@leather.io/models';
import { type BitcoinNetworkModes, type DefaultNetworkConfigurations } from '@leather.io/models';
import { uniqueArray } from '@leather.io/utils';

import { FormErrorMessages } from '@shared/error-messages';
Expand All @@ -11,7 +11,7 @@ import {
} from '@shared/forms/address-validators';
import { checkIfDigitsOnly } from '@shared/forms/amount-validators';

import { defaultNetworksSchema } from '../rpc-schemas';
import { defaultNetworkIdSchema } from '../rpc-schemas';
import {
accountSchema,
formatValidationErrors,
Expand All @@ -21,17 +21,35 @@ import {

export const defaultRpcSendTransferNetwork = 'mainnet';

function defaultNetworkIdToBitcoinNetworkMode(
networkId: DefaultNetworkConfigurations
): BitcoinNetworkModes {
switch (networkId) {
case 'mainnet':
return 'mainnet';
case 'testnet':
case 'testnet4':
return 'testnet';
case 'sbtcTestnet':
case 'sbtcDevenv':
case 'devnet':
return 'regtest';
case 'signet':
return 'signet';
}
}

export const rpcSendTransferParamsSchemaLegacy = z.object({
account: accountSchema.optional(),
address: z.string(),
amount: z.string(),
network: defaultNetworksSchema.optional(),
network: defaultNetworkIdSchema.optional(),
});

export const rpcSendTransferParamsSchema = z
.object({
account: accountSchema.optional(),
network: defaultNetworksSchema.optional(),
network: defaultNetworkIdSchema.optional(),
recipients: z
.array(
z.object({
Expand All @@ -54,8 +72,12 @@ export const rpcSendTransferParamsSchema = z
})
.refine(
({ network, recipients }) => {
if (!network) return true;

const addressNetworks = recipients.map(recipient =>
btcAddressNetworkValidator(network as BitcoinNetworkModes).isValidSync(recipient.address)
btcAddressNetworkValidator(defaultNetworkIdToBitcoinNetworkMode(network)).isValidSync(
recipient.address
)
);

return !addressNetworks.some(val => val === false);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/rpc/methods/sign-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';

import type { PaymentTypes } from '@leather.io/rpc';

import { defaultNetworksSchema } from '../rpc-schemas';
import { defaultNetworkIdSchema } from '../rpc-schemas';
import {
accountSchema,
formatValidationErrors,
Expand All @@ -14,7 +14,7 @@ const rpcSignMessageParamsSchema = z.object({
type: z.enum(['bip322']).optional(),
account: accountSchema.optional(),
message: z.string(),
network: defaultNetworksSchema.optional(),
network: defaultNetworkIdSchema.optional(),
paymentType: z.enum(['p2tr', 'p2wpkh'] as [PaymentTypes, PaymentTypes]).optional(),
});

Expand Down
4 changes: 2 additions & 2 deletions src/shared/rpc/methods/sign-psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RpcResponse,
} from '@leather.io/rpc';

import { defaultNetworksSchema } from '../rpc-schemas';
import { defaultNetworkIdSchema } from '../rpc-schemas';
import {
accountSchema,
formatValidationErrors,
Expand Down Expand Up @@ -36,7 +36,7 @@ const rpcSignPsbtParamsSchema = z.object({
allowedSighash: z.array(z.any()).optional(),
broadcast: z.boolean().optional(),
hex: z.string(),
network: defaultNetworksSchema.optional(),
network: defaultNetworkIdSchema.optional(),
signAtIndex: z
.union([z.number(), z.array(z.number())])
.optional()
Expand Down
9 changes: 6 additions & 3 deletions src/shared/rpc/rpc-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import {
WalletDefaultNetworkConfigurationIds,
} from '@leather.io/models';

type NonEmptyNetworkList = [DefaultNetworkConfigurations, ...DefaultNetworkConfigurations[]];
type NonEmptyDefaultNetworkIdList = [
DefaultNetworkConfigurations,
...DefaultNetworkConfigurations[],
];

export const defaultNetworksSchema = z.enum(
Object.values(WalletDefaultNetworkConfigurationIds) as NonEmptyNetworkList
export const defaultNetworkIdSchema = z.enum(
Object.values(WalletDefaultNetworkConfigurationIds) as NonEmptyDefaultNetworkIdList
);
6 changes: 5 additions & 1 deletion tests/specs/settings/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { OnboardingSelectors } from '@tests/selectors/onboarding.selectors';
import { SettingsSelectors } from '@tests/selectors/settings.selectors';
import { SharedComponentsSelectors } from '@tests/selectors/shared-component.selectors';

import { WalletDefaultNetworkConfigurationIds } from '@leather.io/models';

import { test } from '../../fixtures/fixtures';

test.describe('Settings menu', () => {
Expand Down Expand Up @@ -67,7 +69,9 @@ test.describe('Settings menu', () => {
await page.getByTestId(SettingsSelectors.ChangeNetworkAction).click();
await page.waitForTimeout(1000);
const networkListItems = await page.getByTestId(SettingsSelectors.NetworkListItem).all();
test.expect(networkListItems).toHaveLength(6);
test
.expect(networkListItems)
.toHaveLength(Object.keys(WalletDefaultNetworkConfigurationIds).length);
});

test('that menu item can toggle privacy', async ({ page, homePage }) => {
Expand Down
Loading