File tree Expand file tree Collapse file tree 7 files changed +106
-5
lines changed
packages/ethereum-cryptography
hdkey-without-crypto-config Expand file tree Collapse file tree 7 files changed +106
-5
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ export default {
19
19
]
20
20
} ) ,
21
21
commonjs ( {
22
- ignore : [ "../shims/hdkey-crypto" , "../secp256k1 " ]
22
+ ignore : [ "../shims/hdkey-crypto" , "../shims/hdkey-secp256k1v3 " ]
23
23
} )
24
24
]
25
25
} ;
Original file line number Diff line number Diff line change 1
- module . exports = require ( "../secp256k1 " ) ;
1
+ module . exports = require ( "../shims/hdkey-secp256k1v3 " ) ;
Original file line number Diff line number Diff line change
1
+ import { ripemd160 } from "../ripemd160" ;
2
+ import { sha256 } from "../sha256" ;
3
+
1
4
export const createHmac = require ( "create-hmac" ) ;
2
- export const createHash = require ( "create-hash" ) ;
3
5
export const randomBytes = require ( "randombytes" ) ;
6
+
7
+ class Hash {
8
+ private buffers : Buffer [ ] = [ ] ;
9
+
10
+ constructor ( private readonly hashFunction : ( msg : Buffer ) => Buffer ) { }
11
+
12
+ public update ( buffer : Buffer ) : this {
13
+ if ( ! Buffer . isBuffer ( buffer ) ) {
14
+ throw new Error ( "hdkey-crypto shim is outdated" ) ;
15
+ }
16
+
17
+ this . buffers . push ( buffer ) ;
18
+
19
+ return this ;
20
+ }
21
+
22
+ public digest ( param : any ) : Buffer {
23
+ if ( param ) {
24
+ throw new Error ( "hdkey-crypto shim is outdated" ) ;
25
+ }
26
+
27
+ return this . hashFunction ( Buffer . concat ( this . buffers ) ) ;
28
+ }
29
+ }
30
+
31
+ export const createHash = ( name : string ) => {
32
+ if ( name === "ripemd160" ) {
33
+ return new Hash ( ripemd160 ) ;
34
+ }
35
+
36
+ if ( name === "sha256" ) {
37
+ return new Hash ( sha256 ) ;
38
+ }
39
+
40
+ throw new Error ( "hdkey-crypto shim is outdated" ) ;
41
+ } ;
Original file line number Diff line number Diff line change
1
+ export * from "../../shims/hdkey-secp256k1v3" ;
Original file line number Diff line number Diff line change 1
- export { createHmac , createHash , randomBytes } from "crypto" ;
1
+ export { createHash , createHmac , randomBytes } from "crypto" ;
Original file line number Diff line number Diff line change
1
+ import * as secp256k1 from "secp256k1" ;
2
+
3
+ export function privateKeyVerify ( privateKey : Buffer ) : boolean {
4
+ return secp256k1 . privateKeyVerify ( privateKey ) ;
5
+ }
6
+
7
+ export function publicKeyCreate ( privateKey : Buffer , compressed = true ) : Buffer {
8
+ return Buffer . from ( secp256k1 . publicKeyCreate ( privateKey , compressed ) ) ;
9
+ }
10
+
11
+ export function publicKeyVerify ( publicKey : Buffer ) : boolean {
12
+ return secp256k1 . publicKeyVerify ( publicKey ) ;
13
+ }
14
+
15
+ export function publicKeyConvert ( publicKey : Buffer , compressed = true ) : Buffer {
16
+ return Buffer . from ( secp256k1 . publicKeyConvert ( publicKey , compressed ) ) ;
17
+ }
18
+
19
+ export function privateKeyTweakAdd ( publicKey : Buffer , tweak : Buffer ) : Buffer {
20
+ return Buffer . from (
21
+ secp256k1 . privateKeyTweakAdd ( Buffer . from ( publicKey ) , tweak )
22
+ ) ;
23
+ }
24
+
25
+ export function publicKeyTweakAdd (
26
+ publicKey : Buffer ,
27
+ tweak : Buffer ,
28
+ compressed = true
29
+ ) : Buffer {
30
+ return Buffer . from (
31
+ secp256k1 . publicKeyTweakAdd ( Buffer . from ( publicKey ) , tweak , compressed )
32
+ ) ;
33
+ }
34
+
35
+ export function sign (
36
+ message : Buffer ,
37
+ privateKey : Buffer
38
+ ) : { signature : Buffer ; recovery : number } {
39
+ const ret = secp256k1 . ecdsaSign ( message , privateKey ) ;
40
+ return { signature : Buffer . from ( ret . signature ) , recovery : ret . recid } ;
41
+ }
42
+
43
+ export function verify (
44
+ message : Buffer ,
45
+ signature : Buffer ,
46
+ publicKey : Buffer
47
+ ) : boolean {
48
+ return secp256k1 . ecdsaVerify ( signature , message , publicKey ) ;
49
+ }
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { assert } from "chai";
2
2
3
3
export function createTests ( HDKey : any ) {
4
4
describe ( "hdkey" , function ( ) {
5
- it ( "Should derive correctly" , function ( ) {
5
+ it ( "Should derive private key correctly" , function ( ) {
6
6
const seed =
7
7
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542" ;
8
8
const hdkey = HDKey . fromMasterSeed ( Buffer . from ( seed , "hex" ) ) ;
@@ -13,5 +13,18 @@ export function createTests(HDKey: any) {
13
13
"xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef"
14
14
) ;
15
15
} ) ;
16
+
17
+ it ( "Should derive public key correctly" , function ( ) {
18
+ const seed =
19
+ "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542" ;
20
+ const hdkey = HDKey . fromMasterSeed ( Buffer . from ( seed , "hex" ) ) ;
21
+ const expected = hdkey . derive ( "m/0/2147483647'/1" ) ;
22
+ const parentkey = hdkey . derive ( "m/0/2147483647'" ) ;
23
+ parentkey . wipePrivateData ( ) ;
24
+
25
+ const childkey = parentkey . derive ( "m/1" ) ;
26
+
27
+ assert . equal ( childkey . publicExtendedKey , expected . publicExtendedKey ) ;
28
+ } ) ;
16
29
} ) ;
17
30
}
You can’t perform that action at this time.
0 commit comments