Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 52ff33f

Browse files
authored
Merge pull request #163 from dadepo/add-privkey-pubkey-pair-validation
Added verifyPublicKey to verify publicKey, privateKey pair.
2 parents 83c1b1a + 23f389d commit 52ff33f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,15 @@ export default class Wallet {
646646
public async toV3String(password: string, opts?: Partial<V3Params>): Promise<string> {
647647
return JSON.stringify(await this.toV3(password, opts))
648648
}
649+
650+
/**
651+
* Verify the publicKey, privateKey pair
652+
*
653+
* @param publicKey the public key to verify against the private key of the wallet
654+
*/
655+
public verifyPublicKey(publicKey: Buffer): boolean {
656+
return privateToPublic(this.privateKey as Buffer).equals(publicKey)
657+
}
649658
}
650659

651660
// helpers

test/index.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ describe('.getChecksumAddressString()', function () {
8484
})
8585
})
8686

87+
describe('.verifyPublicKey()', function () {
88+
it('should return true if publicKey, privateKey pair is valid', function () {
89+
assert.strictEqual(fixtureWallet.verifyPublicKey(fixturePublicKeyBuffer), true)
90+
})
91+
it('should return false if publicKey, privateKey pair is invalid', function () {
92+
assert.strictEqual(fixtureWallet.verifyPublicKey(Buffer.alloc(64, 0)), false)
93+
})
94+
})
95+
8796
describe('public key only wallet', function () {
8897
const pubKey = Buffer.from(fixturePublicKey, 'hex')
8998
it('.fromPublicKey() should work', function () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy