-
Notifications
You must be signed in to change notification settings - Fork 45
Add verifyMessage and verifyTransaction on account #572
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| import { Message, Transaction } from "../core"; | ||
| import { Address } from "../core/address"; | ||
|
|
||
| export interface IAccount { | ||
| readonly address: Address; | ||
|
|
||
| sign(data: Uint8Array): Promise<Uint8Array>; | ||
| signTransaction(transaction: Transaction): Promise<Uint8Array>; | ||
| verifyTransactionSignature(transaction: Transaction, signature: Uint8Array): Promise<boolean>; | ||
| signMessage(message: Message): Promise<Uint8Array>; | ||
| verifyMessageSignature(message: Message, signature: Uint8Array): Promise<boolean>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,26 @@ | ||
| import crypto from "crypto"; | ||
| import nacl from "tweetnacl"; | ||
| import ed2curve from "ed2curve"; | ||
| import { X25519EncryptedData } from "./x25519EncryptedData"; | ||
| import nacl from "tweetnacl"; | ||
| import { UserPublicKey, UserSecretKey } from "../userKeys"; | ||
| import { X25519EncryptedData } from "./x25519EncryptedData"; | ||
|
|
||
| export class PubkeyDecryptor { | ||
| static decrypt(data: X25519EncryptedData, decryptorSecretKey: UserSecretKey): Buffer { | ||
| const ciphertext = Buffer.from(data.ciphertext, 'hex'); | ||
| const edhPubKey = Buffer.from(data.identities.ephemeralPubKey, 'hex'); | ||
| const originatorPubKeyBuffer = Buffer.from(data.identities.originatorPubKey, 'hex'); | ||
| static async decrypt(data: X25519EncryptedData, decryptorSecretKey: UserSecretKey): Promise<Buffer> { | ||
|
Contributor
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. Minor breaking change. Let's mention it in the PR description, as well, so that we don't forget to document it in the release notes. |
||
| const ciphertext = Buffer.from(data.ciphertext, "hex"); | ||
| const edhPubKey = Buffer.from(data.identities.ephemeralPubKey, "hex"); | ||
| const originatorPubKeyBuffer = Buffer.from(data.identities.originatorPubKey, "hex"); | ||
| const originatorPubKey = new UserPublicKey(originatorPubKeyBuffer); | ||
|
|
||
| const authMessage = crypto.createHash('sha256').update( | ||
| Buffer.concat([ciphertext, edhPubKey]) | ||
| ).digest(); | ||
| const authMessage = crypto | ||
| .createHash("sha256") | ||
| .update(Buffer.concat([ciphertext, edhPubKey])) | ||
| .digest(); | ||
|
|
||
| if (!originatorPubKey.verify(authMessage, Buffer.from(data.mac, 'hex'))) { | ||
| if (!(await originatorPubKey.verify(authMessage, Buffer.from(data.mac, "hex")))) { | ||
| throw new Error("Invalid authentication for encrypted message originator"); | ||
| } | ||
|
|
||
| const nonce = Buffer.from(data.nonce, 'hex'); | ||
| const nonce = Buffer.from(data.nonce, "hex"); | ||
| const x25519Secret = ed2curve.convertSecretKey(decryptorSecretKey.valueOf()); | ||
| const x25519EdhPubKey = ed2curve.convertPublicKey(edhPubKey); | ||
| if (x25519EdhPubKey === null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ export class UserVerifier { | |
| * @param signature the signature to be verified | ||
| * @returns true if the signature is valid, false otherwise | ||
| */ | ||
| verify(data: Buffer | Uint8Array, signature: Buffer | Uint8Array): boolean { | ||
| async verify(data: Buffer | Uint8Array, signature: Buffer | Uint8Array): Promise<boolean> { | ||
|
Contributor
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. Minor breaking change, we should document (not forget about) it. |
||
| return this.publicKey.verify(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.
In account, we have
verifyTransactionandverifyMessage. Here, we also have the suffixsignature. Keep it or drop it? (either way should be fine, but we must also reflect this in the specs).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.
I kind of like having
signaturein the name, but if we decide to drop it, it's fine