Skip to content

Commit

Permalink
Add setHook endpoint (#338)
Browse files Browse the repository at this point in the history
* Add setHook endpoint

* Adapt to new logic

* Refactor SetHook view & parameters

* Fix post rebase

* Cleanup yarn.lock

* Remove obsolete code

---------

Co-authored-by: Florian Bouron <[email protected]>
  • Loading branch information
ThibautBremand and FlorianBouron authored Jan 17, 2024
1 parent a2973e4 commit d717463
Show file tree
Hide file tree
Showing 37 changed files with 787 additions and 42 deletions.
6 changes: 6 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './isInstalled';
export * from './mintNFT';
export * from './sendPayment';
export * from './setAccount';
export * from './setHook';
export * from './setRegularKey';
export * from './setTrustline';
export * from './signMessage';
Expand All @@ -23,6 +24,9 @@ export * from './submitBulkTransactions';
export type { Amount } from 'xrpl/dist/npm/models/common';
export type {
AccountNFToken,
Hook,
HookGrant,
HookParameter,
Memo,
Network,
PaymentFlags,
Expand All @@ -42,6 +46,7 @@ export type {
MintNFTRequest,
SendPaymentRequest,
SetAccountRequest,
SetHookRequest,
SetRegularKeyRequest,
SetTrustlineRequest,
SignMessageRequest,
Expand All @@ -66,6 +71,7 @@ export type {
MintNFTResponse,
SendPaymentResponse,
SetAccountResponse,
SetHookResponse,
SetRegularKeyResponse,
SetTrustlineResponse,
SignMessageResponse,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/setHook/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './setHook';
38 changes: 38 additions & 0 deletions packages/api/src/setHook/setHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
SetHookRequest,
SetHookResponse,
GEM_WALLET,
RequestSetHookMessage,
ResponseType
} from '@gemwallet/constants';

import { deserializeError } from '../helpers/errors';
import { sendMessageToContentScript } from '../helpers/extensionMessaging';

export const setHook = async (payload: SetHookRequest): Promise<SetHookResponse> => {
let response: SetHookResponse = {
type: ResponseType.Reject,
result: undefined
};

try {
const message: RequestSetHookMessage = {
app: GEM_WALLET,
type: 'REQUEST_SET_HOOK/V3',
payload
};
const { result, error } = await sendMessageToContentScript(message);
const parsedError = error ? deserializeError(error) : undefined;
if (parsedError) {
throw parsedError;
}

if (result) {
response.type = ResponseType.Response;
response.result = result;
}
} catch (e) {
throw e;
}
return response;
};
55 changes: 55 additions & 0 deletions packages/api/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Submit transactions (Bulk) (check the console)
</button>
<button onclick="handleEvents()">Activate events (check the console)</button>
<button onclick="handleSetHook()">Set hook - Xahau only (check the console)</button>
<div id="address"></div>
</div>
</body>
Expand Down Expand Up @@ -665,4 +666,58 @@
console.error('GemWallet is not connected: ', e);
});
}
function handleSetHook() {
GemWalletApi.isInstalled()
.then(({ result }) => {
if (result.isInstalled) {
const payload = {
hooks: [
{
Hook: {
CreateCode:
'0061736D01000000011C0460057F7F7F7F7F017E60037F7F7E017E60027F7F017F60017F017E02230303656E76057472616365000003656E7606616363657074000103656E76025F670002030201030503010002062B077F0141B088040B7F004180080B7F0041A6080B7F004180080B7F0041B088040B7F0041000B7F0041010B07080104686F6F6B00030AC4800001C0800001017F230041106B220124002001200036020C41920841134180084112410010001A410022002000420010011A41012200200010021A200141106A240042000B0B2C01004180080B254163636570742E633A2043616C6C65642E00224163636570742E633A2043616C6C65642E22',
Flags: 1,
HookApiVersion: 0,
HookOn: 'F'.repeat(58) + 'BFFFFE',
HookNamespace: '3963ADEB1B0E8934C0963680531202FD511FF1E16D5864402C2DA63861C420A8',
HookParameters: [
{
HookParameter: {
HookParameterName: 'ABCDEF12',
HookParameterValue: '12345678'
}
}
],
HookGrants: [
{
HookGrant: {
HookHash: '78CAF69EEE950A6C55A450AC2A980DE434D624CD1B13148E007E28B7B6461CC8'
}
}
]
}
}
],
memos: [
{
memo: {
memoType: '4465736372697074696f6e',
memoData: '54657374206d656d6f'
}
}
]
};
GemWalletApi.setHook(payload)
.then((res) => {
console.log('Received response: ', res);
})
.catch((e) => {
console.error('Something went wrong: ', e);
});
}
})
.catch((e) => {
console.error('GemWallet is not connected: ', e);
});
}
</script>
13 changes: 13 additions & 0 deletions packages/constants/src/event/event.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
SendPaymentRequest,
SendPaymentRequestDeprecated,
SetAccountRequest,
SetHookRequest,
SetRegularKeyRequest,
SetTrustlineRequest,
SetTrustlineRequestDeprecated,
Expand Down Expand Up @@ -43,6 +44,7 @@ interface MessageEventData {
| SendPaymentRequest
| SendPaymentRequestDeprecated
| SetAccountRequest
| SetHookRequest
| SetRegularKeyRequest
| SetTrustlineRequest
| SetTrustlineRequestDeprecated
Expand Down Expand Up @@ -310,6 +312,16 @@ export interface CancelOfferEventListener extends MessageEvent<MessageEventData>
};
}

export interface SetHookEventListener extends MessageEvent<MessageEventData> {
data: {
app: typeof GEM_WALLET;
type: 'REQUEST_SET_HOOK/V3';
source: 'GEM_WALLET_MSG_REQUEST';
messageId: number;
payload: SetHookRequest;
};
}

export type EventListener =
| AcceptNFTOfferEventListener
| AddressEventListener
Expand All @@ -329,6 +341,7 @@ export type EventListener =
| PaymentEventListener
| PaymentEventListenerDeprecated
| SetAccountEventListener
| SetHookEventListener
| SetRegularKeyEventListener
| SetTrustlineEventListener
| SetTrustlineEventListenerDeprecated
Expand Down
1 change: 1 addition & 0 deletions packages/constants/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './network/network.constant';
export * from './global/global.constant';
export * from './payload/payload.types';
export * from './xrpl/basic.types';
export * from './xrpl/hooks.types';
export * from './xrpl/nft.types';
23 changes: 23 additions & 0 deletions packages/constants/src/message/message.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
SendPaymentResponseDeprecated,
SetAccountResponse,
SetAccountRequest,
SetHookResponse,
SetHookRequest,
SetRegularKeyResponse,
SetRegularKeyRequest,
SetTrustlineResponse,
Expand Down Expand Up @@ -78,6 +80,7 @@ export type RequestMessage =
| 'REQUEST_PUBLIC_KEY'
| 'REQUEST_SEND_PAYMENT/V3'
| 'REQUEST_SET_ACCOUNT/V3'
| 'REQUEST_SET_HOOK/V3'
| 'REQUEST_SET_REGULAR_KEY/V3'
| 'REQUEST_SET_TRUSTLINE/V3'
| 'REQUEST_SIGN_MESSAGE'
Expand Down Expand Up @@ -107,6 +110,7 @@ export type ReceiveMessage =
| 'RECEIVE_PUBLIC_KEY'
| 'RECEIVE_SEND_PAYMENT/V3'
| 'RECEIVE_SET_ACCOUNT/V3'
| 'RECEIVE_SET_HOOK/V3'
| 'RECEIVE_SET_REGULAR_KEY/V3'
| 'RECEIVE_SET_TRUSTLINE/V3'
| 'RECEIVE_SIGN_MESSAGE'
Expand Down Expand Up @@ -278,6 +282,12 @@ export interface RequestSubmitBulkTransactionsMessage {
payload: SubmitBulkTransactionsWithKeysRequest;
}

export interface RequestSetHookMessage {
app: typeof GEM_WALLET;
type: 'REQUEST_SET_HOOK/V3';
payload: SetHookRequest;
}

// Internal
export interface InternalRequestPasswordMessage {
app: typeof GEM_WALLET;
Expand Down Expand Up @@ -337,6 +347,7 @@ export type SendPaymentMessagingResponse = MessagingResponse & SendPaymentRespon
export type SendPaymentMessagingResponseDeprecated = MessagingResponse &
SendPaymentResponseDeprecated;
export type SetAccountMessagingResponse = MessagingResponse & SetAccountResponse;
export type SetHookMessagingResponse = MessagingResponse & SetHookResponse;
export type SetRegularKeyMessagingResponse = MessagingResponse & SetRegularKeyResponse;
export type SetTrustlineMessagingResponse = MessagingResponse & SetTrustlineResponse;
export type SetTrustlineMessagingResponseDeprecated = MessagingResponse &
Expand Down Expand Up @@ -511,6 +522,12 @@ export interface ReceiveSubmitBulkTransactionsContentMessage {
payload: SubmitBulkTransactionsMessagingResponse;
}

export interface ReceiveSetHookContentMessage {
app: typeof GEM_WALLET;
type: 'RECEIVE_SET_HOOK/V3';
payload: SetHookMessagingResponse;
}

// Internal
export interface InternalReceivePasswordContentMessage {
app: typeof GEM_WALLET;
Expand Down Expand Up @@ -644,6 +661,9 @@ export type ReceiveSubmitTransactionBackgroundMessage = ReceiveSubmitTransaction
export type ReceiveSubmitBulkTransactionsBackgroundMessage =
ReceiveSubmitBulkTransactionsContentMessage & BackgroundMessagePayload;

export type ReceiveSetHookBackgroundMessage = ReceiveSetHookContentMessage &
BackgroundMessagePayload;

export type InternalReceivePasswordBackgroundMessage = InternalReceivePasswordContentMessage &
BackgroundMessagePayload;

Expand Down Expand Up @@ -683,6 +703,7 @@ export type BackgroundMessage =
| RequestSendPaymentMessage
| RequestSendPaymentMessageDeprecated
| RequestSetAccountMessage
| RequestSetHookMessage
| RequestSetRegularKeyMessage
| RequestSetTrustlineMessage
| RequestSetTrustlineMessageDeprecated
Expand Down Expand Up @@ -712,6 +733,7 @@ export type BackgroundMessage =
| ReceivePublicKeyBackgroundMessage
| ReceivePublicKeyBackgroundMessageDeprecated
| ReceiveSetAccountBackgroundMessage
| ReceiveSetHookBackgroundMessage
| ReceiveSetRegularKeyBackgroundMessage
| ReceiveSendPaymentBackgroundMessage
| ReceiveSendPaymentBackgroundMessageDeprecated
Expand Down Expand Up @@ -752,6 +774,7 @@ export type APIMessages =
| RequestIsInstalledMessage
| RequestSendPaymentMessage
| RequestSetAccountMessage
| RequestSetHookMessage
| RequestSetRegularKeyMessage
| RequestSetTrustlineMessage
| RequestSignMessageMessage
Expand Down
16 changes: 15 additions & 1 deletion packages/constants/src/payload/payload.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
SetAccountFlags,
CreateOfferFlags
} from '../xrpl/basic.types';
import { AccountNFToken, AccountNFTokenResponse, NFTokenIDResponse } from './../xrpl/nft.types';
import { Hook } from '../xrpl/hooks.types';
import { AccountNFToken, AccountNFTokenResponse, NFTokenIDResponse } from '../xrpl/nft.types';

/*
* Request Payloads
Expand Down Expand Up @@ -304,6 +305,12 @@ export interface SubmitStorageKeyRequest {
storageKey: string;
}

export interface SetHookRequest extends BaseTransactionRequest {
// Hooks array, which mirrors the Hook Chain installed on the account. Position 0 in the array corresponds to position
// 0 in the Hook Chain, etc.
hooks: Hook[];
}

export type RequestPayload =
| AcceptNFTOfferRequest
| BurnNFTRequest
Expand All @@ -318,6 +325,7 @@ export type RequestPayload =
| SendPaymentRequest
| SendPaymentRequestDeprecated
| SetAccountRequest
| SetHookRequest
| SetRegularKeyRequest
| SetTrustlineRequest
| SetTrustlineRequestDeprecated
Expand Down Expand Up @@ -460,6 +468,11 @@ export interface CancelOfferResponse
hash: string;
}> {}

export interface SetHookResponse
extends BaseResponse<{
hash: string;
}> {}

export type ResponsePayload =
| AcceptNFTOfferResponse
| BurnNFTResponse
Expand All @@ -480,6 +493,7 @@ export type ResponsePayload =
| SendPaymentResponse
| SendPaymentResponseDeprecated
| SetAccountResponse
| SetHookResponse
| SetRegularKeyResponse
| SetTrustlineResponse
| SetTrustlineResponseDeprecated
Expand Down
10 changes: 9 additions & 1 deletion packages/constants/src/xrpl/basic.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import {
AccountSetFlagsInterface,
OfferCreateFlagsInterface,
AMMDepositFlagsInterface,
AMMWithdrawFlagsInterface
AMMWithdrawFlagsInterface,
Transaction as XRPLTx
} from 'xrpl';

import { SetHook } from './hooks.types';

export type Transaction = XRPLTransaction | XahauTransaction;

export type XRPLTransaction = XRPLTx;
export type XahauTransaction = XRPLTx | SetHook;

export interface Memo {
memo: {
memoType?: string;
Expand Down
Loading

0 comments on commit d717463

Please sign in to comment.