forked from DimensionDev/Maskbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: NextID plugin (DimensionDev#5458)
* feat: init next id plugin * feat: persona sign and wallet sign happy path * feat: ui * fix: unused i18n * feat: impl proof binding * feat: impl proof unbinding * feat: bound item ui * feat: add loading skeleton * refactor: extract panel componet * style: copy button tooltip color * fix: should send identity address when unbind * feat: use snackbar to notify * feat: should disable bind when not on evm * feat: improve snackbar ui * feat: should clear wallet sign when change account * fix: use last recognized identity to bind * feat: should show connect persona button when not active persona for current profile * refactor: ui improve * refactor: code review feedback * fix: first tab in web3 tabs * feat: style * feat: update production url * fix: wrong params to proof server * Revert "fix: wrong params to proof server" This reverts commit ea6538b. * fix: use current persona * feat: should query from kv sever when in other twitter user profile * fix: cspell * fix: eslint * refactor: review feedback * feat: remove kv in next id * fix: handle | char in compress point * fix: should retry when connect persona
- Loading branch information
Showing
42 changed files
with
1,393 additions
and
164 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
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
119 changes: 119 additions & 0 deletions
119
packages/mask/src/extension/background-script/HelperService/nextId.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,119 @@ | ||
import { | ||
toHex, | ||
PersonaIdentifier, | ||
compressSecp256k1Point, | ||
fromHex, | ||
toBase64, | ||
decompressSecp256k1Key, | ||
} from '@masknet/shared-base' | ||
import urlcat from 'urlcat' | ||
import { first } from 'lodash-unified' | ||
|
||
const BASE_URL = | ||
process.env.channel === 'stable' && process.env.NODE_ENV === 'production' | ||
? 'https://proof-service.next.id/' | ||
: 'https://js43x8ol17.execute-api.ap-east-1.amazonaws.com/api/' | ||
|
||
interface QueryBinding { | ||
platform: string | ||
identity: string | ||
} | ||
|
||
interface QueryBindingIDResponse { | ||
persona: string | ||
proofs: { | ||
platform: string | ||
identity: string | ||
}[] | ||
} | ||
|
||
interface QueryBindingResponse { | ||
ids: QueryBindingIDResponse[] | ||
} | ||
|
||
interface CreatePayloadBody { | ||
action: string | ||
platform: string | ||
identity: string | ||
public_key: string | ||
} | ||
|
||
interface PayloadResponse { | ||
post_content: string | ||
sign_payload: string | ||
} | ||
|
||
export async function bindProof( | ||
persona: PersonaIdentifier, | ||
action: 'create' | 'delete', | ||
platform: string, | ||
identity: string, | ||
walletSignature?: string, | ||
signature?: string, | ||
) { | ||
const publicKey = await queryPersonaHexPublicKey(persona) | ||
if (!publicKey) return | ||
|
||
const requestBody = { | ||
action, | ||
platform, | ||
identity, | ||
public_key: publicKey, | ||
extra: { | ||
...(walletSignature ? { wallet_signature: toBase64(fromHex(walletSignature)) } : {}), | ||
...(signature ? { signature: toBase64(fromHex(signature)) } : {}), | ||
}, | ||
} | ||
|
||
return fetch(urlcat(BASE_URL, '/v1/proof'), { | ||
body: JSON.stringify(requestBody), | ||
method: 'POST', | ||
mode: 'cors', | ||
}) | ||
} | ||
|
||
async function queryPersonaHexPublicKey(persona: PersonaIdentifier) { | ||
const key256 = decompressSecp256k1Key(persona.compressedPoint.replace(/\|/g, '/')) | ||
if (!key256.x || !key256.y) return null | ||
const arr = compressSecp256k1Point(key256.x, key256.y) | ||
|
||
return `0x${toHex(arr)}` | ||
} | ||
|
||
export async function queryExistedBinding(persona: PersonaIdentifier) { | ||
const publicKey = await queryPersonaHexPublicKey(persona) | ||
if (!publicKey) return | ||
|
||
const response = await fetch(urlcat(BASE_URL, '/v1/proof', { platform: 'nextid', identity: publicKey }), { | ||
mode: 'cors', | ||
}) | ||
|
||
const result = (await response.json()) as QueryBindingResponse | ||
return first(result.ids) | ||
} | ||
|
||
export async function createPersonaPayload( | ||
persona: PersonaIdentifier, | ||
action: 'create' | 'delete', | ||
identity: string, | ||
platform: string, | ||
) { | ||
const publicKey = await queryPersonaHexPublicKey(persona) | ||
if (!publicKey) return | ||
|
||
const requestBody: CreatePayloadBody = { | ||
action, | ||
platform, | ||
identity, | ||
public_key: publicKey, | ||
} | ||
|
||
const response = await fetch(urlcat(BASE_URL, '/v1/proof/payload'), { | ||
body: JSON.stringify(requestBody), | ||
method: 'POST', | ||
mode: 'cors', | ||
}) | ||
|
||
const result: PayloadResponse = await response.json() | ||
return JSON.stringify(JSON.parse(result.sign_payload)) | ||
} |
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
Oops, something went wrong.