Skip to content

Commit

Permalink
Set Regular Key (#264)
Browse files Browse the repository at this point in the history
* Add setRegularKey endpoint

* Add missing autofill params in LedgerContext

* Fix post rebase

* Add dedicated view to set a regular key

* Add cypress tests

* Display current regular key in the view

* Fix post rebase

* Use in-memory messaging

* Add post review changes

* Fix Permissions view

* Fix cypress tests
  • Loading branch information
ThibautBremand authored Dec 7, 2023
1 parent 57a9933 commit 5c0fd8e
Show file tree
Hide file tree
Showing 26 changed files with 878 additions and 14 deletions.
3 changes: 3 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 './setRegularKey';
export * from './setTrustline';
export * from './signMessage';
export * from './signTransaction';
Expand Down Expand Up @@ -41,6 +42,7 @@ export type {
MintNFTRequest,
SendPaymentRequest,
SetAccountRequest,
SetRegularKeyRequest,
SetTrustlineRequest,
SignMessageRequest,
SignTransactionRequest,
Expand All @@ -64,6 +66,7 @@ export type {
MintNFTResponse,
SendPaymentResponse,
SetAccountResponse,
SetRegularKeyResponse,
SetTrustlineResponse,
SignMessageResponse,
SignTransactionResponse,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/setRegularKey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './setRegularKey';
53 changes: 53 additions & 0 deletions packages/api/src/setRegularKey/setRegularKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
SetRegularKeyRequest,
SetRegularKeyResponse,
GEM_WALLET,
RequestSetRegularKeyMessage,
ResponseType
} from '@gemwallet/constants';

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

export const setRegularKey = async (
payload: SetRegularKeyRequest
): Promise<SetRegularKeyResponse> => {
/* response:
* if the transaction succeeds:
* - type: 'response'
* - result:
* - hash: transaction hash
*
* if the user rejects the transaction:
* - type: 'reject'
* - result: undefined
*
* if the transaction fails:
* - throw an error
*/
let response: SetRegularKeyResponse = {
type: ResponseType.Reject,
result: undefined
};

try {
const message: RequestSetRegularKeyMessage = {
app: GEM_WALLET,
type: 'REQUEST_SET_REGULAR_KEY/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;
};
30 changes: 30 additions & 0 deletions packages/api/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<button onclick="handleGetNetwork()">Get Network (check the console)</button>
<button onclick="handleSignMessage()">Sign message</button>
<button onclick="handleSetAccount()">Set Account (check the console)</button>
<button onclick="handleSetRegularKey()">Set Regular Key (check the console)</button>
<button onclick="handleCreateOffer()">Create Offer (check the console)</button>
<button onclick="handleCancelOffer()">Cancel Offer (check the console)</button>
<button onclick="handleSubmitTransaction()">Submit transaction (check the console)</button>
Expand Down Expand Up @@ -416,6 +417,35 @@
console.error('GemWallet is not connected: ', e);
});
}
function handleSetRegularKey() {
GemWalletApi.isInstalled()
.then(({ result }) => {
if (result.isInstalled) {
const transaction = {
regularKey: 'rNvFCZXpDtGeQ3bVas95wGLN6N2stGmA9o',
memos: [
{
memo: {
memoType: '4465736372697074696f6e',
memoData: '54657374206d656d6f'
}
}
]
};

GemWalletApi.setRegularKey(transaction)
.then((res) => {
console.log('Received response: ', res);
})
.catch((e) => {
console.error('Cannot proceed the transaction: ', e);
});
}
})
.catch((e) => {
console.error('GemWallet is not connected: ', e);
});
}
function handleCreateOffer() {
GemWalletApi.isInstalled()
.then(({ result }) => {
Expand Down
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,
SetRegularKeyRequest,
SetTrustlineRequest,
SetTrustlineRequestDeprecated,
SignMessageRequest,
Expand Down Expand Up @@ -42,6 +43,7 @@ interface MessageEventData {
| SendPaymentRequest
| SendPaymentRequestDeprecated
| SetAccountRequest
| SetRegularKeyRequest
| SetTrustlineRequest
| SetTrustlineRequestDeprecated
| SignMessageRequest
Expand Down Expand Up @@ -278,6 +280,16 @@ export interface SetAccountEventListener extends MessageEvent<MessageEventData>
};
}

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

export interface CreateOfferEventListener extends MessageEvent<MessageEventData> {
data: {
app: typeof GEM_WALLET;
Expand Down Expand Up @@ -317,6 +329,7 @@ export type EventListener =
| PaymentEventListener
| PaymentEventListenerDeprecated
| SetAccountEventListener
| SetRegularKeyEventListener
| SetTrustlineEventListener
| SetTrustlineEventListenerDeprecated
| SignMessageListener
Expand Down
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,
SetRegularKeyResponse,
SetRegularKeyRequest,
SetTrustlineResponse,
SetTrustlineResponseDeprecated,
SetTrustlineRequest,
Expand Down Expand Up @@ -76,6 +78,7 @@ export type RequestMessage =
| 'REQUEST_PUBLIC_KEY'
| 'REQUEST_SEND_PAYMENT/V3'
| 'REQUEST_SET_ACCOUNT/V3'
| 'REQUEST_SET_REGULAR_KEY/V3'
| 'REQUEST_SET_TRUSTLINE/V3'
| 'REQUEST_SIGN_MESSAGE'
| 'REQUEST_SIGN_MESSAGE/V3'
Expand Down Expand Up @@ -104,6 +107,7 @@ export type ReceiveMessage =
| 'RECEIVE_PUBLIC_KEY'
| 'RECEIVE_SEND_PAYMENT/V3'
| 'RECEIVE_SET_ACCOUNT/V3'
| 'RECEIVE_SET_REGULAR_KEY/V3'
| 'RECEIVE_SET_TRUSTLINE/V3'
| 'RECEIVE_SIGN_MESSAGE'
| 'RECEIVE_SIGN_MESSAGE/V3'
Expand Down Expand Up @@ -238,6 +242,12 @@ export interface RequestSetAccountMessage {
payload: SetAccountRequest;
}

export interface RequestSetRegularKeyMessage {
app: typeof GEM_WALLET;
type: 'REQUEST_SET_REGULAR_KEY/V3';
payload: SetRegularKeyRequest;
}

export interface RequestCreateOfferMessage {
app: typeof GEM_WALLET;
type: 'REQUEST_CREATE_OFFER/V3';
Expand Down Expand Up @@ -327,6 +337,7 @@ export type SendPaymentMessagingResponse = MessagingResponse & SendPaymentRespon
export type SendPaymentMessagingResponseDeprecated = MessagingResponse &
SendPaymentResponseDeprecated;
export type SetAccountMessagingResponse = MessagingResponse & SetAccountResponse;
export type SetRegularKeyMessagingResponse = MessagingResponse & SetRegularKeyResponse;
export type SetTrustlineMessagingResponse = MessagingResponse & SetTrustlineResponse;
export type SetTrustlineMessagingResponseDeprecated = MessagingResponse &
SetTrustlineResponseDeprecated;
Expand Down Expand Up @@ -464,6 +475,12 @@ export interface ReceiveSetAccountContentMessage {
payload: SetAccountMessagingResponse;
}

export interface ReceiveSetRegularKeyContentMessage {
app: typeof GEM_WALLET;
type: 'RECEIVE_SET_REGULAR_KEY/V3';
payload: SetRegularKeyMessagingResponse;
}

export interface ReceiveCreateOfferContentMessage {
app: typeof GEM_WALLET;
type: 'RECEIVE_CREATE_OFFER/V3';
Expand Down Expand Up @@ -609,6 +626,9 @@ export type ReceiveBurnNFTBackgroundMessage = ReceiveBurnNFTContentMessage &
export type ReceiveSetAccountBackgroundMessage = ReceiveSetAccountContentMessage &
BackgroundMessagePayload;

export type ReceiveSetRegularKeyBackgroundMessage = ReceiveSetRegularKeyContentMessage &
BackgroundMessagePayload;

export type ReceiveCreateOfferBackgroundMessage = ReceiveCreateOfferContentMessage &
BackgroundMessagePayload;

Expand Down Expand Up @@ -663,6 +683,7 @@ export type BackgroundMessage =
| RequestSendPaymentMessage
| RequestSendPaymentMessageDeprecated
| RequestSetAccountMessage
| RequestSetRegularKeyMessage
| RequestSetTrustlineMessage
| RequestSetTrustlineMessageDeprecated
| RequestSignMessageMessage
Expand Down Expand Up @@ -691,6 +712,7 @@ export type BackgroundMessage =
| ReceivePublicKeyBackgroundMessage
| ReceivePublicKeyBackgroundMessageDeprecated
| ReceiveSetAccountBackgroundMessage
| ReceiveSetRegularKeyBackgroundMessage
| ReceiveSendPaymentBackgroundMessage
| ReceiveSendPaymentBackgroundMessageDeprecated
| ReceiveSetTrustlineBackgroundMessage
Expand Down Expand Up @@ -730,6 +752,7 @@ export type APIMessages =
| RequestIsInstalledMessage
| RequestSendPaymentMessage
| RequestSetAccountMessage
| RequestSetRegularKeyMessage
| RequestSetTrustlineMessage
| RequestSignMessageMessage
| RequestSignTransactionMessage
Expand Down
13 changes: 13 additions & 0 deletions packages/constants/src/payload/payload.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ export interface SetAccountRequest extends BaseTransactionRequest {
tickSize?: number;
}

export interface SetRegularKeyRequest extends BaseTransactionRequest {
// A base-58-encoded Address that indicates the regular key pair to be assigned to the account. If omitted, removes
// any existing regular key pair from the account. Must not match the master key pair for the address.
regularKey?: string;
}

export interface CreateOfferRequest extends BaseTransactionRequest {
flags?: CreateOfferFlags;
// Time after which the Offer is no longer active, in seconds since the Ripple Epoch.
Expand Down Expand Up @@ -309,6 +315,7 @@ export type RequestPayload =
| SendPaymentRequest
| SendPaymentRequestDeprecated
| SetAccountRequest
| SetRegularKeyRequest
| SetTrustlineRequest
| SetTrustlineRequestDeprecated
| SignMessageRequest
Expand Down Expand Up @@ -435,6 +442,11 @@ export interface SetAccountResponse
hash: string;
}> {}

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

export interface CreateOfferResponse
extends BaseResponse<{
hash: string;
Expand Down Expand Up @@ -465,6 +477,7 @@ export type ResponsePayload =
| SendPaymentResponse
| SendPaymentResponseDeprecated
| SetAccountResponse
| SetRegularKeyResponse
| SetTrustlineResponse
| SetTrustlineResponseDeprecated
| SignMessageResponse
Expand Down
Loading

0 comments on commit 5c0fd8e

Please sign in to comment.