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

ENG-3296 sats-connect repeat-inscriptions #50

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
2 changes: 1 addition & 1 deletion .github/workflows/release-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ jobs:
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
$GITHUB_URL \
-d "{\"body\":\"> Test this PR with \`npm i sats-connect@$VERSION\`\"}"
-d "{\"body\":\"> Test this PR with \`npm i --save-exact sats-connect@$VERSION\`\"}"
1 change: 1 addition & 0 deletions src/capabilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const extractOrValidateCapabilities = (
signTransaction: validateCapability('signTransaction'),
sendBtcTransaction: validateCapability('sendBtcTransaction'),
createInscription: validateCapability('createInscription'),
createRepeatInscriptions: validateCapability('createRepeatInscriptions'),
};

return Object.entries(capabilityMap).reduce((acc, [capability, value]) => {
Expand Down
30 changes: 1 addition & 29 deletions src/inscriptions/createInscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,7 @@ import { createUnsecuredToken } from 'jsontokens';

import { getProviderOrThrow } from '../provider';
import { CreateInscriptionOptions, CreateInscriptionPayload } from './types';

const MAX_CONTENT_LENGTH_MAINNET = 400e3; // 400kb is the max miners will mine
const MAX_CONTENT_LENGTH_TESTNET = 60e3; // 60kb limit on Testnet to prevent spam

export const validateInscriptionPayload = (payload: CreateInscriptionPayload) => {
const { contentType, content, payloadType, network, appFeeAddress, appFee } = payload;
if (!/^[a-z]+\/[a-z0-9\-\.\+]+(?=;.*|$)/.test(contentType)) {
throw new Error('Invalid content type detected');
}

if (!content || content.length === 0) {
throw new Error('Empty content not allowed');
}

if (!payloadType || (payloadType !== 'BASE_64' && payloadType !== 'PLAIN_TEXT')) {
throw new Error('Empty invalid payloadType specified');
}

if (
content.length >
(network.type === 'Mainnet' ? MAX_CONTENT_LENGTH_MAINNET : MAX_CONTENT_LENGTH_TESTNET)
) {
throw new Error('Content too large');
}

if ((appFeeAddress?.length ?? 0) > 0 && (appFee ?? 0) <= 0) {
throw new Error('Invalid combination of app fee address and fee provided');
}
};
import { validateInscriptionPayload } from './utils';

export const createInscription = async (options: CreateInscriptionOptions) => {
const { getProvider } = options;
Expand Down
20 changes: 20 additions & 0 deletions src/inscriptions/createRepeatInscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getProviderOrThrow } from '../provider';
import { CreateRepeatInscriptionsOptions } from './types';
import { Json, createUnsecuredToken } from 'jsontokens';
import { validateInscriptionPayload } from './utils';

export const createRepeatInscriptions = async (options: CreateRepeatInscriptionsOptions) => {
const { getProvider } = options;
const provider = await getProviderOrThrow(getProvider);

validateInscriptionPayload(options.payload);

try {
const request = createUnsecuredToken(options.payload as unknown as Json);
const response = await provider.createRepeatInscriptions(request);
options.onFinish?.(response);
} catch (error) {
console.error('[Connect] Error during create repeat inscriptions', error);
options.onCancel?.();
}
};
1 change: 1 addition & 0 deletions src/inscriptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './createInscription';
export * from './createRepeatInscriptions';
export * from './types';
13 changes: 13 additions & 0 deletions src/inscriptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,24 @@ export interface CreateInscriptionPayload extends RequestPayload {
token?: string;
}

export interface CreateRepeatInscriptionsPayload extends CreateInscriptionPayload {
repeat: number;
}

export type CreateInscriptionResponse = {
txId: string;
};

export type CreateRepeatInscriptionsResponse = {
txId: string;
};

export type CreateInscriptionOptions = RequestOptions<
CreateInscriptionPayload,
CreateInscriptionResponse
>;

export type CreateRepeatInscriptionsOptions = RequestOptions<
CreateRepeatInscriptionsPayload,
CreateRepeatInscriptionsResponse
>;
30 changes: 30 additions & 0 deletions src/inscriptions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CreateInscriptionPayload } from './types';

const MAX_CONTENT_LENGTH_MAINNET = 400e3; // 400kb is the max miners will mine
const MAX_CONTENT_LENGTH_TESTNET = 60e3; // 60kb limit on Testnet to prevent spam

export const validateInscriptionPayload = (payload: CreateInscriptionPayload) => {
const { contentType, content, payloadType, network, appFeeAddress, appFee } = payload;
if (!/^[a-z]+\/[a-z0-9\-\.\+]+(?=;.*|$)/.test(contentType)) {
throw new Error('Invalid content type detected');
}

if (!content || content.length === 0) {
throw new Error('Empty content not allowed');
}

if (!payloadType || (payloadType !== 'BASE_64' && payloadType !== 'PLAIN_TEXT')) {
throw new Error('Empty invalid payloadType specified');
}

if (
content.length >
(network.type === 'Mainnet' ? MAX_CONTENT_LENGTH_MAINNET : MAX_CONTENT_LENGTH_TESTNET)
) {
throw new Error('Content too large');
}

if ((appFeeAddress?.length ?? 0) > 0 && (appFee ?? 0) <= 0) {
throw new Error('Invalid combination of app fee address and fee provided');
}
};
3 changes: 2 additions & 1 deletion src/provider/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GetAddressResponse } from '../addresses';
import type { CallWalletResponse } from '../call';
import type { GetCapabilitiesResponse } from '../capabilities';
import type { CreateInscriptionResponse } from '../inscriptions';
import type { CreateInscriptionResponse, CreateRepeatInscriptionsResponse } from '../inscriptions';
import type { SignMessageResponse } from '../messages';
import type { SendBtcTransactionResponse, SignTransactionResponse } from '../transactions';

Expand All @@ -12,6 +12,7 @@ interface BaseBitcoinProvider {
signTransaction: (request: string) => Promise<SignTransactionResponse>;
sendBtcTransaction: (request: string) => Promise<SendBtcTransactionResponse>;
createInscription: (request: string) => Promise<CreateInscriptionResponse>;
createRepeatInscriptions: (request: string) => Promise<CreateRepeatInscriptionsResponse>;
}

export type Capability = keyof BaseBitcoinProvider;
Expand Down
Loading