-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] rewrite Election model with Web Crypto API (#381)
Signed-off-by: TechQuery <shiy2008@gmail.com>
- Loading branch information
Showing
7 changed files
with
269 additions
and
220 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,80 @@ | ||
import { VoteTicket } from '@kaiyuanshe/kys-service'; | ||
import { BaseModel, toggle } from 'mobx-restful'; | ||
import { observable } from 'mobx'; | ||
import { BaseModel, persist, restore, toggle } from 'mobx-restful'; | ||
|
||
import { isServer } from '../Base'; | ||
import userStore from '../Base/User'; | ||
|
||
export const buffer2hex = (buffer: ArrayBufferLike) => | ||
Array.from(new Uint8Array(buffer), x => x.toString(16).padStart(2, '0')).join( | ||
'', | ||
); | ||
|
||
export class ElectionModel extends BaseModel { | ||
client = userStore.client; | ||
algorithm = { name: 'ECDSA', namedCurve: 'P-384', hash: { name: 'SHA-256' } }; | ||
|
||
@persist() | ||
@observable | ||
accessor privateKey: CryptoKey | undefined; | ||
|
||
@persist() | ||
@observable | ||
accessor publicKey = ''; | ||
|
||
@persist() | ||
@observable | ||
accessor currentVoteTicket: VoteTicket | undefined; | ||
|
||
restored = !isServer() && restore(this, 'Electron'); | ||
|
||
@toggle('uploading') | ||
async createVoteTicket(electionName: string) { | ||
const { body } = await this.client.post<VoteTicket>( | ||
`election/${electionName}/vote/ticket`, | ||
async makePublicKey() { | ||
await this.restored; | ||
|
||
if (this.publicKey) return this.publicKey; | ||
|
||
const { publicKey, privateKey } = await crypto.subtle.generateKey( | ||
this.algorithm, | ||
true, | ||
['sign', 'verify'], | ||
); | ||
this.privateKey = privateKey; | ||
|
||
const JWK = await crypto.subtle.exportKey('jwk', publicKey); | ||
|
||
return (this.publicKey = btoa(JSON.stringify(JWK))); | ||
} | ||
|
||
@toggle('uploading') | ||
async savePublicKey(electionName: string, jsonWebKey = this.publicKey) { | ||
await userStore.restored; | ||
|
||
const { body } = await this.client.post( | ||
`election/${electionName}/public-key`, | ||
{ jsonWebKey }, | ||
); | ||
return body!; | ||
} | ||
|
||
@toggle('uploading') | ||
async signVoteTicket(electionName: string) { | ||
await this.restored; | ||
|
||
if (this.currentVoteTicket) return this.currentVoteTicket; | ||
|
||
await this.makePublicKey(); | ||
await this.savePublicKey(electionName); | ||
|
||
const signature = await crypto.subtle.sign( | ||
this.algorithm, | ||
this.privateKey!, | ||
new TextEncoder().encode(electionName), | ||
); | ||
return (this.currentVoteTicket = { | ||
electionName, | ||
publicKey: this.publicKey, | ||
signature: buffer2hex(signature), | ||
}); | ||
} | ||
} |
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.
9607cd4
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.
Deploy preview for kaiyuanshe ready!
✅ Preview
https://kaiyuanshe-qv2lg867g-techquerys-projects.vercel.app
Built with commit 9607cd4.
This pull request is being automatically deployed with vercel-action