This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useMessageSigner and useSignatureVerification (#114)
- Loading branch information
1 parent
f93af9a
commit 4350cf1
Showing
14 changed files
with
244 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type { Signer } from '@polkadot/api/types'; | ||
|
||
export type SignatureResult = `0x${string}`; |
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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
export { getWalletBySource, getWallets } from '@talismn/connect-wallets'; | ||
export type { WalletAccount } from '@talismn/connect-wallets'; | ||
import { Signer } from './api'; | ||
import type { WalletAccount as TalismanWalletAccount } from '@talismn/connect-wallets'; | ||
|
||
export interface WalletAccount extends TalismanWalletAccount { | ||
// Talisman sets the type as unknown so we must manually set it to Signer | ||
signer?: Signer; | ||
} |
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
64 changes: 64 additions & 0 deletions
64
packages/useink/src/react/hooks/contracts/useMessageSigner.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,64 @@ | ||
import { SignatureResult } from '../../../core/index.ts'; | ||
import { useWallet } from '../wallets/useWallet.ts'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
export type Sign = (data?: string) => void; | ||
|
||
export enum SignerError { | ||
AccountNotConnected = 'No accounts are connected.', | ||
SignatureRejected = 'Signature rejected.', | ||
SignatureFailed = 'Signature failed.', | ||
} | ||
|
||
export interface MessageSigner { | ||
sign: Sign; | ||
signature: string | undefined; | ||
resetState: () => void; | ||
error: SignerError | undefined; | ||
} | ||
|
||
export function useMessageSigner(): MessageSigner { | ||
const { account } = useWallet(); | ||
const [signature, setSignature] = useState<SignatureResult>(); | ||
const [error, setError] = useState<SignerError>(); | ||
|
||
const sign: Sign = useCallback( | ||
async (data = '') => { | ||
if (!account || !account.signer?.signRaw) { | ||
setError(SignerError.AccountNotConnected); | ||
return; | ||
} | ||
|
||
setError(undefined); | ||
|
||
account.signer | ||
?.signRaw?.({ | ||
address: account.address, | ||
data, | ||
type: 'bytes', | ||
}) | ||
.then(({ signature }) => setSignature(signature)) | ||
.catch((e) => { | ||
if (e.toString() === 'Error: Cancelled') { | ||
setError(SignerError.SignatureRejected); | ||
return; | ||
} | ||
|
||
setError(SignerError.SignatureFailed); | ||
}); | ||
}, | ||
[account, account?.wallet?.extension?.signer], | ||
); | ||
|
||
const resetState = useCallback(() => { | ||
setSignature(undefined); | ||
setError(undefined); | ||
}, []); | ||
|
||
return { | ||
sign, | ||
signature, | ||
resetState, | ||
error, | ||
}; | ||
} |
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
50 changes: 50 additions & 0 deletions
50
packages/useink/src/react/hooks/contracts/useSignatureVerifier.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,50 @@ | ||
import { signatureVerify } from '../../../utils'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
type VerificationParams = Parameters<typeof signatureVerify>; | ||
|
||
export type Verify = ( | ||
data: VerificationParams[0], | ||
signature: VerificationParams[1], | ||
addressOrPublicKey: VerificationParams[2], | ||
) => void; | ||
|
||
export enum VerificationState { | ||
Unchecked = 'Unchecked', | ||
Valid = 'Valid signature', | ||
Invalid = 'Invalid signature', | ||
} | ||
|
||
export interface SignatureVerifier { | ||
verify: Verify; | ||
result: VerificationState; | ||
resetState: () => void; | ||
} | ||
|
||
export function useSignatureVerifier(): SignatureVerifier { | ||
const [result, setVerificationResult] = useState(VerificationState.Unchecked); | ||
|
||
const verify: Verify = useCallback<Verify>( | ||
(data, signature, addressOrPublicKey) => { | ||
const { isValid } = signatureVerify(data, signature, addressOrPublicKey); | ||
|
||
if (isValid) { | ||
setVerificationResult(VerificationState.Valid); | ||
return; | ||
} | ||
|
||
setVerificationResult(VerificationState.Invalid); | ||
}, | ||
[], | ||
); | ||
|
||
const resetState = useCallback(() => { | ||
setVerificationResult(VerificationState.Unchecked); | ||
}, []); | ||
|
||
return { | ||
verify, | ||
result, | ||
resetState, | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.