-
Notifications
You must be signed in to change notification settings - Fork 3
Scope facilitator memo to V2 in 2.0.2 #12
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
Open
tony1908
wants to merge
7
commits into
main
Choose a base branch
from
feat/v2-facilitator-memo-2-0-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be5db6f
fix: restore legacy v1 memo behavior
tony1908 823f857
docs: clarify facilitator memo as v2-only
tony1908 74f9ced
release: scope facilitator memo to v2 in 2.0.2
tony1908 ad88e88
feat: add v2 facilitator memo helpers
tony1908 e2d8ae5
fix: narrow facilitator memo helper detection
tony1908 1549e5b
perf: reuse facilitator memo pattern
tony1908 afbfa63
release: publish 2.0.3
tony1908 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module.exports = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| roots: ['<rootDir>/test'], | ||
| moduleFileExtensions: ['ts', 'js', 'json'], | ||
| clearMocks: true, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import { | |
| TokenType, | ||
| } from './types'; | ||
|
|
||
|
|
||
| /** | ||
| * Payment client for making x402 payments on Stacks | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| jest.mock('@stacks/network', () => ({ | ||
| StacksMainnet: class { url = 'https://stacks-node-api.mainnet.stacks.co'; }, | ||
| StacksTestnet: class { url = 'https://stacks-node-api.testnet.stacks.co'; }, | ||
| })); | ||
|
|
||
| import type { AxiosInstance } from 'axios'; | ||
| import { wrapAxiosWithPayment, X402_HEADERS } from '../src'; | ||
|
|
||
| const mockMakeSTXTokenTransfer = jest.fn(); | ||
| const mockMakeContractCall = jest.fn(); | ||
| const mockBufferCVFromString = jest.fn((value: string) => value); | ||
| const mockSomeCV = jest.fn((value: unknown) => ({ type: 'some', value })); | ||
|
|
||
| jest.mock('@stacks/transactions', () => ({ | ||
| makeSTXTokenTransfer: (opts: any) => mockMakeSTXTokenTransfer(opts), | ||
| makeContractCall: (opts: any) => mockMakeContractCall(opts), | ||
| AnchorMode: { Any: 3 }, | ||
| PostConditionMode: { Allow: 1 }, | ||
| uintCV: (value: string) => value, | ||
| principalCV: (value: string) => value, | ||
| someCV: (value: any) => mockSomeCV(value), | ||
| noneCV: () => ({ type: 'none' }), | ||
| bufferCVFromString: (value: string) => mockBufferCVFromString(value), | ||
| getAddressFromPrivateKey: () => 'ST2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7', | ||
| TransactionVersion: { Mainnet: 0, Testnet: 1 }, | ||
| })); | ||
|
|
||
| function createAxiosHarness() { | ||
| let rejected!: (error: any) => Promise<any>; | ||
|
|
||
| const instance = { | ||
| interceptors: { | ||
| response: { | ||
| use: (_fulfilled: unknown, onRejected: typeof rejected) => { | ||
| rejected = onRejected; | ||
| return 0; | ||
| }, | ||
| }, | ||
| }, | ||
| request: jest.fn().mockResolvedValue({ status: 200, data: { ok: true } }), | ||
| } as unknown as AxiosInstance & { request: jest.Mock }; | ||
|
|
||
| wrapAxiosWithPayment(instance, { | ||
| address: 'ST2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7', | ||
| privateKey: '1'.repeat(64), | ||
| network: 'testnet', | ||
| }); | ||
|
|
||
| return { instance, rejected }; | ||
| } | ||
|
|
||
| describe('wrapAxiosWithPayment facilitator memo', () => { | ||
| beforeEach(() => { | ||
| mockMakeSTXTokenTransfer.mockResolvedValue({ serialize: () => Uint8Array.from([0xde, 0xad]) }); | ||
| mockMakeContractCall.mockResolvedValue({ serialize: () => Uint8Array.from([0xca, 0xfe]) }); | ||
| }); | ||
|
|
||
| it('signs STX retries with an x402-prefixed memo', async () => { | ||
| const { rejected } = createAxiosHarness(); | ||
|
|
||
| const paymentRequired = { | ||
| x402Version: 2, | ||
| resource: { url: 'https://api.example.com/premium' }, | ||
| accepts: [{ | ||
| scheme: 'exact', | ||
| network: 'stacks:2147483648', | ||
| amount: '1000', | ||
| asset: 'STX', | ||
| payTo: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM', | ||
| maxTimeoutSeconds: 300, | ||
| }], | ||
| }; | ||
|
|
||
| await rejected({ | ||
| config: { headers: {} }, | ||
| response: { | ||
| status: 402, | ||
| headers: { | ||
| [X402_HEADERS.PAYMENT_REQUIRED]: Buffer.from(JSON.stringify(paymentRequired)).toString('base64'), | ||
| }, | ||
| data: null, | ||
| }, | ||
| }); | ||
|
|
||
| expect(mockMakeSTXTokenTransfer).toHaveBeenCalledWith(expect.objectContaining({ | ||
| memo: expect.stringMatching(/^x402:[A-Za-z0-9_-]{24}$/), | ||
| })); | ||
| }); | ||
|
|
||
| it('passes the same memo pattern into SIP-010 contract calls', async () => { | ||
| const { rejected } = createAxiosHarness(); | ||
|
|
||
| const paymentRequired = { | ||
| x402Version: 2, | ||
| resource: { url: 'https://api.example.com/premium' }, | ||
| accepts: [{ | ||
| scheme: 'exact', | ||
| network: 'stacks:2147483648', | ||
| amount: '1000', | ||
| asset: 'SBTC', | ||
| payTo: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM', | ||
| maxTimeoutSeconds: 300, | ||
| }], | ||
| }; | ||
|
|
||
| await rejected({ | ||
| config: { headers: {} }, | ||
| response: { | ||
| status: 402, | ||
| headers: { | ||
| [X402_HEADERS.PAYMENT_REQUIRED]: Buffer.from(JSON.stringify(paymentRequired)).toString('base64'), | ||
| }, | ||
| data: null, | ||
| }, | ||
| }); | ||
|
|
||
| expect(mockBufferCVFromString).toHaveBeenCalledWith(expect.stringMatching(/^x402:[A-Za-z0-9_-]{24}$/)); | ||
| expect(mockSomeCV).toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
isFacilitatorMemofunction creates a newRegExpobject on every invocation. Since the prefix is a constant and the nonce pattern is already defined as a constant (FACILITATOR_NONCE_PATTERN), it is more efficient to reuse the existing pattern or perform a string prefix check followed by the nonce validation. This avoids unnecessary object allocation and compilation overhead, which is beneficial if this utility is used in performance-sensitive areas like transaction scanning.