-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rdt): add subintent request builder
- Loading branch information
Showing
8 changed files
with
175 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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
1 change: 1 addition & 0 deletions
1
packages/dapp-toolkit/src/modules/wallet-request/pre-authorization-request/index.ts
This file contains 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 @@ | ||
export * from './subintent-builder' |
55 changes: 55 additions & 0 deletions
55
...pp-toolkit/src/modules/wallet-request/pre-authorization-request/subintent-builder.spec.ts
This file contains 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,55 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { SubintentRequestBuilder } from './subintent-builder' | ||
|
||
describe('SubintentRequestBuilder', () => { | ||
it('should build a subintent request', () => { | ||
const tx = SubintentRequestBuilder() | ||
.manifest('...') | ||
.setExpiration('secondsAfterSignature', 60) | ||
.addBlobs('deadbeef', 'beefdead') | ||
.message('hello') | ||
.toRequestItem() | ||
|
||
expect(tx).toEqual({ | ||
discriminator: 'subintent', | ||
version: 1, | ||
transactionManifestVersion: 1, | ||
transactionManifest: '...', | ||
expiration: { | ||
discriminator: 'expireAfterSignature', | ||
value: 60, | ||
}, | ||
blobs: ['deadbeef', 'beefdead'], | ||
message: 'hello', | ||
}) | ||
}) | ||
|
||
it('should build a subintent request using raw object', () => { | ||
const tx = SubintentRequestBuilder() | ||
.rawConfig({ | ||
version: 1, | ||
transactionManifestVersion: 1, | ||
transactionManifest: '...', | ||
expiration: { | ||
discriminator: 'expireAfterSignature', | ||
value: 60, | ||
}, | ||
blobs: ['deadbeef', 'beefdead'], | ||
message: 'hello', | ||
}) | ||
.toRequestItem() | ||
|
||
expect(tx).toEqual({ | ||
discriminator: 'subintent', | ||
version: 1, | ||
transactionManifestVersion: 1, | ||
transactionManifest: '...', | ||
expiration: { | ||
discriminator: 'expireAfterSignature', | ||
value: 60, | ||
}, | ||
blobs: ['deadbeef', 'beefdead'], | ||
message: 'hello', | ||
}) | ||
}) | ||
}) |
106 changes: 106 additions & 0 deletions
106
...es/dapp-toolkit/src/modules/wallet-request/pre-authorization-request/subintent-builder.ts
This file contains 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,106 @@ | ||
import { SubintentRequestItem } from '../../../schemas' | ||
|
||
export type BuildableSubintentRequest = { | ||
toRequestItem: () => SubintentRequestItem | ||
} | ||
/** | ||
* A builder function for creating a SubintentRequest. | ||
* | ||
* @returns An object with methods to configure and build a SubintentRequestItem. | ||
* | ||
* @example | ||
* const builder = SubintentRequestBuilder(); | ||
* const requestItem = builder | ||
* .manifest('some-manifest') | ||
* .setExpiration('atTime', 1234567890) | ||
* .addBlobs('blob1', 'blob2') | ||
* .message('This is a message') | ||
* | ||
* @method setExpiration | ||
* Sets the expiration for the subintent request. | ||
* | ||
* @param type - The type of expiration, either 'atTime' or 'secondsAfterSignature'. | ||
* @param value - The value of the expiration. If type is 'atTime', this is a Unix timestamp. If type is 'secondsAfterSignature', this is the number of seconds after the signature is created. | ||
* @returns The API object for chaining. | ||
* | ||
* @method addBlobs | ||
* Adds blobs to the subintent request. | ||
* | ||
* @param values - The blobs to add. | ||
* @returns The API object for chaining. | ||
* | ||
* @method message | ||
* Sets a message for the subintent request. | ||
* | ||
* @param value - The message to set. | ||
* @returns The API object for chaining. | ||
* | ||
* @method manifest | ||
* Sets the transaction manifest for the subintent request. | ||
* | ||
* @param value - The transaction manifest to set. | ||
* @returns The API object for chaining. | ||
* | ||
* @method toRequestItem | ||
* Converts the current state to a SubintentRequestItem. | ||
* | ||
* @returns The SubintentRequestItem. | ||
* | ||
* @method rawConfig | ||
* Sets the raw configuration for the subintent request. | ||
* | ||
* @param rawConfig - The raw configuration object, excluding the discriminator. | ||
* @returns An object with the toRequestItem method. | ||
*/ | ||
export const SubintentRequestBuilder = () => { | ||
let state: Partial<SubintentRequestItem> = { | ||
discriminator: 'subintent', | ||
version: 1, | ||
transactionManifestVersion: 1, | ||
} | ||
|
||
const setExpiration = ( | ||
type: 'atTime' | 'secondsAfterSignature', | ||
value: number, | ||
) => { | ||
state.expiration = { | ||
discriminator: | ||
type === 'atTime' ? 'expireAtTime' : 'expireAfterSignature', | ||
value, | ||
} | ||
return api | ||
} | ||
|
||
const addBlobs = (...values: string[]) => { | ||
state.blobs = values | ||
return api | ||
} | ||
|
||
const message = (value: string) => { | ||
state.message = value | ||
return api | ||
} | ||
|
||
const manifest = (value: string) => { | ||
state.transactionManifest = value | ||
return api | ||
} | ||
|
||
const toRequestItem = () => state as SubintentRequestItem | ||
|
||
const rawConfig = ( | ||
rawConfig: Omit<SubintentRequestItem, 'discriminator'>, | ||
) => { | ||
state = { ...rawConfig, discriminator: 'subintent' } | ||
return { toRequestItem } | ||
} | ||
|
||
const api = { | ||
setExpiration, | ||
addBlobs, | ||
message, | ||
toRequestItem, | ||
} as const | ||
|
||
return { manifest, rawConfig } | ||
} |
This file contains 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 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