Skip to content

Commit

Permalink
Merge pull request #57 from nash-io/feature-neo3-support
Browse files Browse the repository at this point in the history
Support NEO3
  • Loading branch information
localhuman authored Jul 28, 2022
2 parents d61295d + 8282e2f commit da441e8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/generateWallet/generateWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum CoinType {
DOT = 354,
ERD = 508,
NEO = 888,
NEO3 = 888,
POLYGON = 966,
AVAXC = 9000
}
Expand Down Expand Up @@ -99,6 +100,7 @@ export const coinTypeFromString = (s: string): CoinType => {
eth: CoinType.ETH,
ltc: CoinType.LTC,
neo: CoinType.NEO,
neo3: CoinType.NEO3,
polygon: CoinType.POLYGON
}

Expand All @@ -109,6 +111,25 @@ export const coinTypeFromString = (s: string): CoinType => {
return m[s]
}

export const blockchainFromString = (name: string): Blockchain => {
switch (name) {
case 'btc':
return Blockchain.BTC
case 'eth':
return Blockchain.ETH
case 'neo':
return Blockchain.NEO
case 'avaxc':
return Blockchain.AVAXC
case 'polygon':
return Blockchain.POLYGON
case 'neo3':
return Blockchain.NEO3
default:
throw new Error('Unsupported name')
}
}

export function neoGetPublicKeyFromPrivateKey(privateKey: string, encode: boolean = true): string {
const privateKeyBuffer = Buffer.from(privateKey, 'hex')
const keypair = curve.keyFromPrivate(privateKeyBuffer, 'hex')
Expand Down
9 changes: 7 additions & 2 deletions src/initialize/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Wallet, Config, InitParams } from '../types'
import decryptSecretKey from '../decryptSecretKey'
import secretKeyToMnemonic from '../secretKeyToMnemonic'
import mnemonicToMasterSeed from '../mnemonicToMasterSeed'
import { generateNashPayloadSigningKey, generateWallet, coinTypeFromString } from '../generateWallet'
import {
generateNashPayloadSigningKey,
generateWallet,
coinTypeFromString,
blockchainFromString
} from '../generateWallet'
// import { cryptoWaitReady } from '@polkadot/util-crypto'

// initialize takes in the init parameters and returns a Config object with all the
Expand All @@ -17,7 +22,7 @@ export default async function initialize(params: InitParams): Promise<Config> {
// if (name.toLowerCase() === 'dot') {
// await cryptoWaitReady()
// }
wallets[name] = generateWallet(masterSeed, coinTypeFromString(name), index, params.net)
wallets[name] = generateWallet(masterSeed, coinTypeFromString(name), index, params.net, blockchainFromString(name))
}

const payloadSigningKey = generateNashPayloadSigningKey(masterSeed, 1)
Expand Down
56 changes: 50 additions & 6 deletions src/mpc/generateAPIKeys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAPIKey } from './createAPIKey'
import { GenerateApiKeysParams, BIP44, APIKey } from '../types/MPC'
import { GenerateApiKeysParams, BIP44, APIKey, Blockchain } from '../types/MPC'
import secretKeyToMnemonic from '../secretKeyToMnemonic'
import bufferize from '../bufferize'
import mnemonicToMasterSeed from '../mnemonicToMasterSeed'
Expand All @@ -11,22 +11,55 @@ export async function generateAPIKeys(params: GenerateApiKeysParams): Promise<AP
const masterSeed = mnemonicToMasterSeed(secretKeyToMnemonic(secretBuff))
const payloadSigningKey = generateNashPayloadSigningKey(masterSeed, 1)

const btcWallet = generateWallet(masterSeed, coinTypeFromString('btc'), params.walletIndices.btc, params.net)
const ethWallet = generateWallet(masterSeed, coinTypeFromString('eth'), params.walletIndices.eth, params.net)
const neoWallet = generateWallet(masterSeed, coinTypeFromString('neo'), params.walletIndices.neo, params.net)
const avaxcWallet = generateWallet(masterSeed, coinTypeFromString('avaxc'), params.walletIndices.avaxc, params.net)
const btcWallet = generateWallet(
masterSeed,
coinTypeFromString('btc'),
params.walletIndices.btc,
params.net,
Blockchain.BTC
)
const ethWallet = generateWallet(
masterSeed,
coinTypeFromString('eth'),
params.walletIndices.eth,
params.net,
Blockchain.ETH
)
const neoWallet = generateWallet(
masterSeed,
coinTypeFromString('neo'),
params.walletIndices.neo,
params.net,
Blockchain.NEO
)
const avaxcWallet = generateWallet(
masterSeed,
coinTypeFromString('avaxc'),
params.walletIndices.avaxc,
params.net,
Blockchain.AVAXC
)
const polygonWallet = generateWallet(
masterSeed,
coinTypeFromString('polygon'),
params.walletIndices.polygon,
params.net
params.net,
Blockchain.POLYGON
)
const neo3Wallet = generateWallet(
masterSeed,
coinTypeFromString('neo3'),
params.walletIndices.neo3,
params.net,
Blockchain.NEO3
)

const btcSecret = btcWallet.privateKey
const ethSecret = ethWallet.privateKey
const neoSecret = neoWallet.privateKey
const avaxcSecret = avaxcWallet.privateKey
const polygonSecret = polygonWallet.privateKey
const neo3Secret = neo3Wallet.privateKey

const btc = await createAPIKey({
...params,
Expand All @@ -53,6 +86,11 @@ export async function generateAPIKeys(params: GenerateApiKeysParams): Promise<AP
curve: 'Secp256k1',
secret: polygonSecret
})
const neo3 = await createAPIKey({
...params,
curve: 'Secp256r1',
secret: neo3Secret
})
return {
child_keys: {
[BIP44.BTC]: {
Expand Down Expand Up @@ -85,6 +123,12 @@ export async function generateAPIKeys(params: GenerateApiKeysParams): Promise<AP
public_key: polygonWallet.publicKey,
server_secret_share_encrypted: polygon.server_secret_share_encrypted
},
[BIP44.NEO3]: {
address: neo3Wallet.address,
client_secret_share: neo3.client_secret_share,
public_key: neo3Wallet.publicKey,
server_secret_share_encrypted: neo3.server_secret_share_encrypted
}
},
paillier_pk: btc.paillier_pk,
payload_public_key: payloadSigningKey.publicKey,
Expand Down
4 changes: 3 additions & 1 deletion src/types/MPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const BlockchainCurve: Record<Blockchain, Curve> = {
[Blockchain.NEO]: 'Secp256r1',
[Blockchain.AVAXC]: 'Secp256k1',
[Blockchain.POLYGON]: 'Secp256k1',
[Blockchain.NEO3]: 'Secp256r1',
[Blockchain.NEO3]: 'Secp256r1'
}

export interface PallierPK {
Expand Down Expand Up @@ -87,6 +87,7 @@ export enum BIP44 {
BTC = "m/44'/0'/0'/0/0",
ETH = "m/44'/60'/0'/0/0",
NEO = "m/44'/888'/0'/0/0",
NEO3 = "m/44'/888'/1'/0/0",
POLYGON = "m/44'/966'/0'/0/0",
AVAXC = "m/44'/9000'/0'/0/0"
}
Expand All @@ -111,6 +112,7 @@ export interface APIKey {
*/
[BIP44.AVAXC]?: ChildKey
[BIP44.POLYGON]?: ChildKey
[BIP44.NEO3]?: ChildKey
}
payload_signing_key: string
payload_public_key: string
Expand Down

0 comments on commit da441e8

Please sign in to comment.