forked from near/near-api-js
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Secp256k1 support #15
Merged
Merged
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
cc5a11b
feat: add support for secp256k1
gtsonevv 828c84b
fix: fix linter errors
gtsonevv 196291f
fix: use enum for public key data
gtsonevv 2cc98d9
fix: add schema changes
gtsonevv 7b178a1
fix: remove enums from schema
gtsonevv 5ffc704
fix: lint
gtsonevv 2bd94f9
fix: update PublicKey
gtsonevv ad024f1
chore: fix linter errors
gtsonevv 15d00f3
Merge branch 'master' into add-secp256k1-support
gtsonevv a9051d7
Merge branch 'add-secp256k1-support' into secp256k1-support-new
gtsonevv a559b29
fix: revert enum changes
gtsonevv 4f12396
fix: fix linter errors
gtsonevv 67eb34d
fix: public key schema
gagdiez 3991ef1
fix: update signature class and schema
gtsonevv c16caaa
fix: fix linter errors
gtsonevv c567804
fix: fix a secp256k1 test
gtsonevv 9aa9446
fix: fix linter errors
gtsonevv 9c3b8eb
fix: fix publickey method
gtsonevv 7d5a824
chore: add a changeset file
gtsonevv effe3d4
chore: lock secp256k1 dependency version
gtsonevv fc951f3
test: add a secp156k1 send money test
gtsonevv 518c361
fix: update pnpm/action-setup to v4
gtsonevv b79dbe5
Merge branch 'master' into secp256k1-support-new
gtsonevv 7ce5bda
fix: fix @near-js/crypto/lib/key_pair imports
gtsonevv 2af0435
refactor: remove duplicated Enum
gtsonevv 65714fb
refactor: add getter to PublicKey and Signature classes
gtsonevv 365fa88
Merge branch 'master' into secp256k1-support-new
gtsonevv a85175b
Merge branch 'master' into secp256k1-support-new
gtsonevv 8c4aa1f
chore: remove duplicated test
gtsonevv ac393c7
Merge branch 'master' into secp256k1-support-new
gtsonevv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
/** All supported key types */ | ||
export enum KeyType { | ||
ED25519 = 0, | ||
SECP256K1 = 1, | ||
} | ||
|
||
export enum KeySize { | ||
SECRET_KEY = 32 | ||
SECRET_KEY = 32, | ||
ED25519_PUBLIC_KEY = 32, | ||
SECP256k1_PUBLIC_KEY = 64, | ||
} |
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,5 +1,6 @@ | ||
export { KeyType } from './constants'; | ||
export { KeyPair } from './key_pair'; | ||
export { KeyPair, KeyPairString } from './key_pair'; | ||
export { Signature } from './key_pair_base'; | ||
export { KeyPairEd25519 } from './key_pair_ed25519'; | ||
export { KeyPairSecp256k1 } from './key_pair_secp256k1'; | ||
export { PublicKey } from './public_key'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { KeySize, KeyType } from './constants'; | ||
import { KeyPairBase, Signature } from './key_pair_base'; | ||
import { PublicKey } from './public_key'; | ||
import secp256k1 from 'secp256k1'; | ||
import randombytes from 'randombytes'; | ||
import { KeyPairString } from './key_pair'; | ||
import { baseDecode, baseEncode } from '@near-js/utils'; | ||
/** | ||
* This class provides key pair functionality for secp256k1 curve: | ||
* generating key pairs, encoding key pairs, signing and verifying. | ||
* nearcore expects secp256k1 public keys to be 64 bytes at all times, | ||
* even when string encoded the secp256k1 library returns 65 byte keys | ||
* (including a 1 byte header that indicates how the pubkey was encoded). | ||
* We'll force the secp256k1 library to always encode uncompressed | ||
* keys with the corresponding 0x04 header byte, then manually | ||
* insert/remove that byte as needed. | ||
*/ | ||
export class KeyPairSecp256k1 extends KeyPairBase { | ||
readonly publicKey: PublicKey; | ||
readonly secretKey: string; | ||
readonly extendedSecretKey: string; | ||
|
||
/** | ||
* Construct an instance of key pair given a secret key. | ||
* It's generally assumed that these are encoded in base58. | ||
* @param {string} extendedSecretKey | ||
*/ | ||
constructor(extendedSecretKey: string) { | ||
super(); | ||
const decoded = baseDecode(extendedSecretKey); | ||
const secretKey = new Uint8Array(decoded.slice(0, KeySize.SECRET_KEY)); | ||
const withHeader = secp256k1.publicKeyCreate(new Uint8Array(secretKey), false); | ||
const data = withHeader.subarray(1, withHeader.length); // remove the 0x04 header byte | ||
this.publicKey = new PublicKey({ | ||
keyType: KeyType.SECP256K1, | ||
data | ||
}); | ||
this.secretKey = baseEncode(secretKey); | ||
this.extendedSecretKey = extendedSecretKey; | ||
} | ||
|
||
/** | ||
* Generate a new random keypair. | ||
* @example | ||
* const keyRandom = KeyPair.fromRandom(); | ||
* keyRandom.publicKey | ||
* // returns [PUBLIC_KEY] | ||
* | ||
* keyRandom.secretKey | ||
* // returns [SECRET_KEY] | ||
*/ | ||
static fromRandom() { | ||
// TODO: find better way to generate PK | ||
const secretKey = randombytes(KeySize.SECRET_KEY); | ||
const withHeader = secp256k1.publicKeyCreate(new Uint8Array(secretKey), false); | ||
const publicKey = withHeader.subarray(1, withHeader.length); | ||
const extendedSecretKey = new Uint8Array([...secretKey, ...publicKey]); | ||
return new KeyPairSecp256k1(baseEncode(extendedSecretKey)); | ||
} | ||
|
||
sign(message: Uint8Array): Signature { | ||
// nearcore expects 65 byte signatures formed by appending the recovery id to the 64 byte signature | ||
const { signature, recid } = secp256k1.ecdsaSign(message, baseDecode(this.secretKey)); | ||
return { signature: new Uint8Array([...signature, recid]), publicKey: this.publicKey }; | ||
} | ||
|
||
verify(message: Uint8Array, signature: Uint8Array): boolean { | ||
return this.publicKey.verify(message, signature); | ||
} | ||
|
||
toString(): KeyPairString { | ||
return `secp256k1:${this.extendedSecretKey}`; | ||
} | ||
|
||
getPublicKey(): PublicKey { | ||
return this.publicKey; | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let's lock the version of secp256k1