-
Notifications
You must be signed in to change notification settings - Fork 11
server side signing #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c243cd1
55650dd
80b1b38
92f037a
e382afc
ae1466d
bec85a9
34894c1
e7d7e20
ad12001
bbc81bb
468fa21
ffb7887
bde9f43
ce21110
eb68747
1e695ed
46bc250
da43d26
5561ccc
9e47618
8fba501
766a99d
6d101c4
fd6e97a
32208be
7116c7a
8643554
5c86cb8
718e3f9
e1dd2e8
cf48efb
266146e
f6845f3
0ac55a4
5ce6c1d
a733144
ff3dd9c
013ae2d
8c895fa
2f11057
7d64456
ecf04a3
7a24170
416aa13
3b507a2
08ea3b3
3457595
cde6c21
bbe11fc
c625c06
96f353d
f620f85
f033702
ee51182
092cdd0
8f8aecd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { generatePrivate } from "@toruslabs/eccrypto"; | ||
| import { post } from "@toruslabs/http-helpers"; | ||
| import { keccak256 } from "@toruslabs/metadata-helpers"; | ||
| import { log } from "@web3auth/base"; | ||
| import BN from "bn.js"; | ||
| import type { ec } from "elliptic"; | ||
| import base32 from "hi-base32"; | ||
|
|
||
| import { CURVE } from "../../constants"; | ||
| import { IRemoteClientState, Web3AuthMPCCoreKit } from "../../index"; | ||
|
|
||
| export class AuthenticatorService { | ||
| private backendUrl: string; | ||
|
|
||
| private coreKitInstance: Web3AuthMPCCoreKit; | ||
|
|
||
| private authenticatorType: string = "authenticator"; | ||
|
|
||
| private factorPub: string = ""; | ||
|
|
||
| private tssIndex: number; | ||
|
|
||
| constructor(params: { backendUrl: string; coreKitInstance: Web3AuthMPCCoreKit; authenticatorType?: string }) { | ||
| const { backendUrl } = params; | ||
| this.backendUrl = backendUrl; | ||
| this.authenticatorType = params.authenticatorType || "authenticator"; | ||
| this.coreKitInstance = params.coreKitInstance; | ||
| // this.remoteClient = remoteClient || false; | ||
| } | ||
|
|
||
| getDescriptionsAndUpdate() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specify return type |
||
| const arrayOfDescriptions = Object.entries(this.coreKitInstance.getKeyDetails().shareDescriptions).map(([key, value]) => { | ||
| const parsedDescription = (value || [])[0] ? JSON.parse(value[0]) : {}; | ||
| return { | ||
| key, | ||
| description: parsedDescription, | ||
| }; | ||
| }); | ||
|
|
||
| const shareDescriptionsMobile = arrayOfDescriptions.find(({ description }) => description.authenticator === this.authenticatorType); | ||
| log.info("shareDescriptionsMobile", shareDescriptionsMobile); | ||
|
|
||
| if (shareDescriptionsMobile) { | ||
| this.factorPub = shareDescriptionsMobile.key; | ||
| this.tssIndex = shareDescriptionsMobile.description.tssShareIndex; | ||
| } | ||
|
|
||
| return shareDescriptionsMobile; | ||
| } | ||
|
|
||
| generateSecretKey(): string { | ||
| const key = generatePrivate().subarray(0, 20); | ||
| return base32.encode(key).toString().replace(/=/g, ""); | ||
| } | ||
|
|
||
| async register(privKey: BN, secretKey: string): Promise<{ success: boolean; message?: string }> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whts the diff b/w private key and secret key? is their any example or flow diagram where i can see usage of this class |
||
| const privKeyPair: ec.KeyPair = CURVE.keyFromPrivate(privKey.toString(16, 64)); | ||
| const pubKey = privKeyPair.getPublic(); | ||
| const sig = CURVE.sign(keccak256(Buffer.from(secretKey, "utf8")), Buffer.from(privKey.toString(16, 64), "hex")); | ||
|
|
||
| const data = { | ||
| pubKey: { | ||
| x: pubKey.getX().toString(16, 64), | ||
| y: pubKey.getY().toString(16, 64), | ||
| }, | ||
| sig: { | ||
| r: sig.r.toString(16, 64), | ||
| s: sig.s.toString(16, 64), | ||
| v: new BN(sig.recoveryParam as number).toString(16, 2), | ||
| }, | ||
| secretKey, | ||
| }; | ||
|
|
||
| const resp = await post<{ | ||
| success: boolean; | ||
| message: string; | ||
| }>(`${this.backendUrl}/api/v3/register`, data); | ||
|
|
||
| return resp; | ||
| } | ||
|
|
||
| async addRecovery(address: string, code: string, factorKey: BN) { | ||
| if (!factorKey) throw new Error("factorKey is not defined"); | ||
| if (!address) throw new Error("address is not defined"); | ||
| if (!code) throw new Error("code is not defined"); | ||
|
|
||
| const data = { | ||
| address, | ||
| code, | ||
| data: { | ||
| // If the verification is complete, we save the factorKey for the user address. | ||
| // This factorKey is used to verify the user in the future on a new device and recover tss share. | ||
| factorKey: factorKey.toString(16, 64), | ||
| }, | ||
| }; | ||
|
|
||
| await post(`${this.backendUrl}/api/v3/verify`, data); | ||
| } | ||
|
|
||
| async verifyRecovery(address: string, code: string): Promise<BN | undefined> { | ||
| const verificationData = { | ||
| address, | ||
| code, | ||
| }; | ||
|
|
||
| const response = await post<{ data?: Record<string, string> }>(`${this.backendUrl}/api/v3/verify`, verificationData); | ||
| const { data } = response; | ||
| return data ? new BN(data.factorKey, "hex") : undefined; | ||
| } | ||
|
|
||
| async verifyRemoteSetup(address: string, code: string): Promise<IRemoteClientState & { tssShareIndex: string }> { | ||
| const verificationData = { | ||
| address, | ||
| code, | ||
| }; | ||
|
|
||
| const response = await post<{ data?: Record<string, string> }>(`${this.backendUrl}/api/v3/verify_remote`, verificationData); | ||
| const { data } = response; | ||
|
|
||
| return { | ||
| tssShareIndex: this.tssIndex.toString(), | ||
| remoteClientUrl: this.backendUrl, | ||
| remoteFactorPub: this.factorPub, | ||
| metadataShare: data.metadataShare, | ||
| remoteClientToken: data.signature, | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change the type of authenticatorType variable based on accepted values