Skip to content

Commit

Permalink
Merge pull request #90 from initia-labs/feat/ethsecp256k1
Browse files Browse the repository at this point in the history
Add eth option for mnemonic and raw key
  • Loading branch information
joon9823 authored Oct 31, 2024
2 parents e7c724e + daaad17 commit 0c83620
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@initia/initia.js",
"version": "0.2.19",
"version": "0.2.20",
"description": "The JavaScript SDK for Initia",
"license": "Apache-2.0",
"author": "Initia Foundation",
Expand Down
10 changes: 8 additions & 2 deletions src/key/MnemonicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ interface MnemonicKeyOptions {
* Coin type. Default is INIT, 118.
*/
coinType?: number

/**
* Whether to use eth pubkey
*/
eth?: boolean
}

const DEFAULT_OPTIONS = {
account: 0,
index: 0,
coinType: INIT_COIN_TYPE,
eth: false,
}

/**
Expand Down Expand Up @@ -66,7 +72,7 @@ export class MnemonicKey extends RawKey {
* @param options
*/
constructor(options: MnemonicKeyOptions = {}) {
const { account, index, coinType } = {
const { account, index, coinType, eth } = {
...DEFAULT_OPTIONS,
...options,
}
Expand All @@ -84,7 +90,7 @@ export class MnemonicKey extends RawKey {
throw new Error('Failed to derive key pair')
}

super(privateKey)
super(privateKey, eth)
this.mnemonic = mnemonic
}
}
17 changes: 14 additions & 3 deletions src/key/RawKey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SHA256, Word32Array } from 'jscrypto'
import * as secp256k1 from 'secp256k1'
import { Key } from './Key'
import { SimplePublicKey } from '../core'
import { EthPublicKey, SimplePublicKey } from '../core'
import keccak256 from 'keccak256'

/**
Expand All @@ -13,12 +13,21 @@ export class RawKey extends Key {
*/
public privateKey: Buffer

constructor(privateKey: Buffer) {
constructor(
privateKey: Buffer,
public eth = false
) {
const publicKey = secp256k1.publicKeyCreate(
new Uint8Array(privateKey),
true
)
super(new SimplePublicKey(Buffer.from(publicKey).toString('base64')))

if (eth) {
super(new EthPublicKey(Buffer.from(publicKey).toString('base64')))
} else {
super(new SimplePublicKey(Buffer.from(publicKey).toString('base64')))
}

this.privateKey = privateKey
}

Expand All @@ -30,6 +39,8 @@ export class RawKey extends Key {

// eslint-disable-next-line @typescript-eslint/require-await
public async sign(payload: Buffer): Promise<Buffer> {
if (this.eth) return this.signWithKeccak256(payload)

const hash = Buffer.from(
SHA256.hash(new Word32Array(payload)).toString(),
'hex'
Expand Down

0 comments on commit 0c83620

Please sign in to comment.