-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dcd): refactor and pull useSubmit hook apart
- Loading branch information
1 parent
a18a165
commit ac73283
Showing
14 changed files
with
130 additions
and
127 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/components/[guild]/AccessHub/hooks/useRemoveGuildPlatform.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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const DEFAULT_MESSAGE = "Please sign this message" | ||
export const DEFAULT_SIGN_LOADING_TEXT = "Check your wallet" |
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,27 @@ | ||
import { ValidationMethod } from "types" | ||
import { DEFAULT_MESSAGE } from "./constants" | ||
import { FuelSignProps, Validation } from "./types" | ||
import { createMessageParams, getMessage, signWithKeyPair } from "./utils" | ||
|
||
export const fuelSign = async ({ | ||
wallet, | ||
address, | ||
payload, | ||
keyPair, | ||
forcePrompt, | ||
msg = DEFAULT_MESSAGE, | ||
ts, | ||
}: FuelSignProps): Promise<[string, Validation]> => { | ||
const params = createMessageParams(address, ts, msg, payload) | ||
let sig = null | ||
|
||
if (!!keyPair && !forcePrompt) { | ||
params.method = ValidationMethod.KEYPAIR | ||
sig = await signWithKeyPair(keyPair, params) | ||
} else { | ||
params.method = ValidationMethod.FUEL | ||
sig = await wallet.signMessage(getMessage(params)) | ||
} | ||
|
||
return [payload, { params, sig }] | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { default, sign, useSubmitWithSign } from "./useSubmit" | ||
export type { SignedValidation, Validation } from "./useSubmit" | ||
export type { SignedValidation, Validation } from "./types" |
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,46 @@ | ||
import { Account } from "fuels" | ||
import { ValidationMethod } from "types" | ||
import { PublicClient, WalletClient } from "viem" | ||
|
||
type SignBaseProps = { | ||
address: `0x${string}` | ||
payload: string | ||
chainId?: string | ||
forcePrompt: boolean | ||
keyPair?: CryptoKeyPair | ||
msg?: string | ||
ts?: number | ||
getMessageToSign?: (params: MessageParams) => string | ||
} | ||
|
||
export type SignProps = SignBaseProps & { | ||
publicClient: PublicClient | ||
walletClient: WalletClient | ||
} | ||
|
||
export type FuelSignProps = SignBaseProps & { wallet: Account } | ||
|
||
export type SignedValidation = { signedPayload: string; validation: Validation } | ||
|
||
export type Validation = { | ||
params: MessageParams | ||
sig: string | ||
} | ||
|
||
export type MessageParams = { | ||
msg: string | ||
addr: string | ||
method: ValidationMethod | ||
chainId?: string | ||
hash?: string | ||
nonce: string | ||
ts: string | ||
} | ||
|
||
export type UseSubmitOptions<ResponseType = void> = { | ||
onSuccess?: (response: ResponseType) => void | ||
onError?: (error: any) => void | ||
|
||
// Use catefully! If this is set to true, a .onSubmit() call can reject! | ||
allowThrow?: boolean | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import randomBytes from "randombytes" | ||
import { keccak256, stringToBytes } from "viem" | ||
import { MessageParams } from "./types" | ||
|
||
export const signWithKeyPair = (keyPair: CryptoKeyPair, params: MessageParams) => | ||
window.crypto.subtle | ||
.sign( | ||
{ name: "ECDSA", hash: "SHA-512" }, | ||
keyPair.privateKey, | ||
Buffer.from(getMessage(params)) | ||
) | ||
.then((signatureBuffer) => Buffer.from(signatureBuffer).toString("hex")) | ||
|
||
export const getMessage = ({ | ||
msg, | ||
addr, | ||
method, | ||
chainId, | ||
hash, | ||
nonce, | ||
ts, | ||
}: MessageParams) => | ||
`${msg}\n\nAddress: ${addr}\nMethod: ${method}${ | ||
chainId ? `\nChainId: ${chainId}` : "" | ||
}${hash ? `\nHash: ${hash}` : ""}\nNonce: ${nonce}\nTimestamp: ${ts}` | ||
|
||
export const createMessageParams = ( | ||
address: `0x${string}`, | ||
ts: number, | ||
msg: string, | ||
payload: string | ||
): MessageParams => ({ | ||
addr: address.toLowerCase(), | ||
nonce: randomBytes(32).toString("hex"), | ||
ts: ts.toString(), | ||
hash: payload !== "{}" ? keccak256(stringToBytes(payload)) : undefined, | ||
method: null, | ||
msg, | ||
chainId: undefined, | ||
}) |
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