Skip to content

Commit

Permalink
jellyfish-wallet should provide non-hd WalletNode by default (#453)
Browse files Browse the repository at this point in the history
* jellyfish-wallet should be non-hd by default

* revert HdWallet rename

* Renamed and additional refactor

Co-authored-by: Ahmed Hilali <[email protected]>
  • Loading branch information
fuxingloh and monstrobishi authored Jul 9, 2021
1 parent b39bb0f commit dc897c7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/packages/jellyfish-transaction-builder/ @fuxingloh @ivan-zynesis @monstrobishi
/packages/jellyfish-transaction-signature/ @fuxingloh @ivan-zynesis
/packages/jellyfish-wallet/ @fuxingloh @ivan-zynesis
/packages/jellyfish-wallet/ @fuxingloh @ivan-zynesis @monstrobishi
/packages/jellyfish-wallet-mnemonic/ @fuxingloh @ivan-zynesis
/packages/testcontainers/ @fuxingloh
/packages/testing/ @fuxingloh @canonbrother
Expand Down
3 changes: 2 additions & 1 deletion .idea/jellyfish.iml

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

6 changes: 3 additions & 3 deletions packages/jellyfish-wallet/__tests__/account.mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletAccount, WalletAccountProvider, WalletHdNode } from '../src'
import { WalletAccount, WalletAccountProvider, WalletEllipticPair } from '../src'
import { Network, RegTest } from '@defichain/jellyfish-network'

/**
Expand All @@ -13,7 +13,7 @@ import { Network, RegTest } from '@defichain/jellyfish-network'
* 02acf1d65943ce391c5c7a6319050c71ece26f5815f1a69445edd35b8d8dac13be
*/
export class TestAccount extends WalletAccount {
constructor (hdNode: WalletHdNode, readonly provider: TestAccountProvider) {
constructor (hdNode: WalletEllipticPair, readonly provider: TestAccountProvider) {
super(hdNode, provider.network)
}

Expand All @@ -29,7 +29,7 @@ export class TestAccountProvider implements WalletAccountProvider<TestAccount> {
constructor (public readonly addresses: string[]) {
}

provide (hdNode: WalletHdNode): TestAccount {
provide (hdNode: WalletEllipticPair): TestAccount {
return new TestAccount(hdNode, this)
}
}
8 changes: 2 additions & 6 deletions packages/jellyfish-wallet/__tests__/node.mock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { WalletHdNode, WalletHdNodeProvider } from '../src'
import {
Transaction,
TransactionSegWit,
Vout
} from '@defichain/jellyfish-transaction'
import { Transaction, TransactionSegWit, Vout } from '@defichain/jellyfish-transaction'
import { SignInputOption, TransactionSigner } from '@defichain/jellyfish-transaction-signature'
import { EllipticPair, Elliptic } from '@defichain/jellyfish-crypto'
import { Elliptic, EllipticPair } from '@defichain/jellyfish-crypto'

/**
* This is for testing only, please don't use this for anything else.
Expand Down
1 change: 1 addition & 0 deletions packages/jellyfish-wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './wallet'
export * from './wallet_account'
export * from './wallet_hd_node'
export * from './wallet_elliptic_pair'
26 changes: 13 additions & 13 deletions packages/jellyfish-wallet/src/wallet_account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Script, OP_CODES, Transaction, Vout, TransactionSegWit } from '@defichain/jellyfish-transaction'
import { WalletHdNode } from './wallet_hd_node'
import { OP_CODES, Script, Transaction, TransactionSegWit, Vout } from '@defichain/jellyfish-transaction'
import { WalletEllipticPair } from './wallet_elliptic_pair'
import { Bech32, HASH160 } from '@defichain/jellyfish-crypto'
import { Network } from '@defichain/jellyfish-network'
import { DeFiAddress } from '@defichain/jellyfish-address'
Expand All @@ -11,9 +11,9 @@ import { DeFiAddress } from '@defichain/jellyfish-address'
*
* WalletAccount implementation uses NATIVE SEGWIT redeem script exclusively.
*/
export abstract class WalletAccount implements WalletHdNode {
export abstract class WalletAccount implements WalletEllipticPair {
protected constructor (
private readonly hdNode: WalletHdNode,
private readonly walletEllipticPair: WalletEllipticPair,
private readonly network: Network
) {
}
Expand All @@ -22,15 +22,15 @@ export abstract class WalletAccount implements WalletHdNode {
* @return {Promise<string>} Bech32 address of this account. (NATIVE SEGWIT)
*/
async getAddress (): Promise<string> {
const pubKey = await this.hdNode.publicKey()
const pubKey = await this.walletEllipticPair.publicKey()
return Bech32.fromPubKey(pubKey, this.network.bech32.hrp, 0x00)
}

/**
* @return {Promise<Script>} redeem script of this account. (NATIVE SEGWIT)
*/
async getScript (): Promise<Script> {
const pubKey = await this.hdNode.publicKey()
const pubKey = await this.walletEllipticPair.publicKey()
return {
stack: [
OP_CODES.OP_0,
Expand Down Expand Up @@ -58,23 +58,23 @@ export abstract class WalletAccount implements WalletHdNode {
abstract isActive (): Promise<boolean>

async publicKey (): Promise<Buffer> {
return await this.hdNode.publicKey()
return await this.walletEllipticPair.publicKey()
}

async privateKey (): Promise<Buffer> {
return await this.hdNode.privateKey()
return await this.walletEllipticPair.privateKey()
}

async sign (hash: Buffer): Promise<Buffer> {
return await this.hdNode.sign(hash)
return await this.walletEllipticPair.sign(hash)
}

async signTx (transaction: Transaction, prevouts: Vout[]): Promise<TransactionSegWit> {
return await this.hdNode.signTx(transaction, prevouts)
return await this.walletEllipticPair.signTx(transaction, prevouts)
}

async verify (hash: Buffer, derSignature: Buffer): Promise<boolean> {
return await this.hdNode.verify(hash, derSignature)
return await this.walletEllipticPair.verify(hash, derSignature)
}
}

Expand All @@ -85,8 +85,8 @@ export abstract class WalletAccount implements WalletHdNode {
export interface WalletAccountProvider<T extends WalletAccount> {

/**
* @param {WalletHdNode} hdNode of this wallet account
* @param {WalletEllipticPair} hdNode of this wallet account
* @return WalletAccount
*/
provide: (hdNode: WalletHdNode) => T
provide: (hdNode: WalletEllipticPair) => T
}
21 changes: 21 additions & 0 deletions packages/jellyfish-wallet/src/wallet_elliptic_pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Transaction, TransactionSegWit, Vout } from '@defichain/jellyfish-transaction'
import { EllipticPair } from '@defichain/jellyfish-crypto'

/**
* WalletEllipticPair extends EllipticPair with additional interface to sign transaction.
*
* WalletEllipticPair uses a managed wallet design where defaults are decided by the implementation.
* Keeping the WalletEllipticPair to conventional defaults and options to none.
*/
export interface WalletEllipticPair extends EllipticPair {

/**
* WalletEllipticPair transaction signing.
*
* @param {Transaction} transaction to sign
* @param {Vout[]} prevouts of the transaction to fund this transaction
* @return {TransactionSegWit} a signed transaction
*/
signTx: (transaction: Transaction, prevouts: Vout[]) => Promise<TransactionSegWit>

}
18 changes: 6 additions & 12 deletions packages/jellyfish-wallet/src/wallet_hd_node.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { Transaction, TransactionSegWit, Vout } from '@defichain/jellyfish-transaction'
import { EllipticPair } from '@defichain/jellyfish-crypto'
import { WalletEllipticPair } from './wallet_elliptic_pair'

/**
* WalletHdNode extends EllipticPair with additional interface to sign transaction.
*
* WalletHdNode uses a managed wallet design where defaults are decided by the implementation.
* Keeping the WalletHdNode to conventional defaults and options to none.
*
*
* @see BIP32 Hierarchical Deterministic Wallets
* @see BIP44 Multi-Account Hierarchy for Deterministic Wallets
*/
export interface WalletHdNode extends EllipticPair {

/**
* WalletHdNode transaction signing.
*
* @param {Transaction} transaction to sign
* @param {Vout[]} prevouts of the transaction to fund this transaction
* @return {TransactionSegWit} a signed transaction
*/
signTx: (transaction: Transaction, prevouts: Vout[]) => Promise<TransactionSegWit>
export interface WalletHdNode extends WalletEllipticPair {

}

Expand Down

0 comments on commit dc897c7

Please sign in to comment.