-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> Currently the examples need to use a lot of code to get signer for the two types of transactions. Since we already have some utils to fetch UTXO's, it could useful to have some minimal wallet utils that allows have more cleaner examples. ## Related Issue Or Context <!--- If suggesting a new feature or change, please discuss it in an issue first --> <!--- If fixing a bug, there should be an issue describing it with steps to reproduce --> <!--- Otherwise, describe context and motivation for change herre --> Closes: #547 ## How Has This Been Tested? Testing details. <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have ensured that all acceptance criteria (or expected behavior) from issue are met - [ ] I have updated the documentation locally and in chainbridge-docs. - [ ] I have added tests to cover my changes. - [ ] I have ensured that all the checks are passing and green, I've signed the CLA bot
- Loading branch information
Showing
11 changed files
with
130 additions
and
23 deletions.
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { BIP32Interface } from 'bip32'; | ||
import { BIP32Factory } from 'bip32'; | ||
import * as bip39 from 'bip39'; | ||
import * as bitcoinjs from 'bitcoinjs-lib'; | ||
import * as tinysecp from 'tiny-secp256k1'; | ||
|
||
import { TypeOfAddress } from '../types.js'; | ||
import { getPublicKey } from '../utils/wallet.js'; | ||
|
||
bitcoinjs.initEccLib(tinysecp); | ||
|
||
describe('getPublicKey', () => { | ||
const bip32 = BIP32Factory(tinysecp); | ||
|
||
it('should return derivedNode for non-P2TR addresses', async () => { | ||
const p2wpkhDerivationPath = "m/84'/0'/0'/0/0"; | ||
const mnemonic = bip39.generateMnemonic(256); | ||
const result = (await getPublicKey({ | ||
bip32, | ||
mnemonic: mnemonic, | ||
derivationPath: p2wpkhDerivationPath, | ||
network: bitcoinjs.networks.testnet, | ||
typeOfAddress: TypeOfAddress.P2WPKH, | ||
})) as { derivedNode: BIP32Interface }; | ||
|
||
expect(result.derivedNode).toBeDefined(); | ||
}); | ||
|
||
it('should return tweakedSigner and publicKeyDropedDERHeader for P2TR addresses', async () => { | ||
const p2trDerivationPath = "m/86'/0'/0'/0/0"; | ||
const mnemonic = bip39.generateMnemonic(256); | ||
const result = (await getPublicKey({ | ||
bip32, | ||
mnemonic: mnemonic, | ||
derivationPath: p2trDerivationPath, | ||
network: bitcoinjs.networks.testnet, | ||
typeOfAddress: TypeOfAddress.P2TR, | ||
})) as unknown as { tweakedSigner: Buffer; publicKeyDropedDERHeader: Buffer }; | ||
|
||
expect(result.tweakedSigner).toBeDefined(); | ||
expect(result.publicKeyDropedDERHeader).toBeDefined(); | ||
}); | ||
}); |
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
File renamed without changes.
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,36 @@ | ||
import { mnemonicToSeed } from 'bip39'; | ||
import { crypto } from 'bitcoinjs-lib'; | ||
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371'; | ||
|
||
import { TypeOfAddress } from '../types.js'; | ||
import type { GetPublicKeyResult, PublicKeyParams } from '../types.js'; | ||
|
||
/** | ||
* @category Bitcoin Wallet Helpers | ||
* @description Return either tweakedSigner and publicKeyDropedDERHeader or derivedNode to sign a transaction | ||
* @param {PublicKeyParams} - bip32, mnemonic, derivationPath, network, typeOfAddress | ||
* @returns {Promise<GetPublicKeyResult>} | ||
*/ | ||
export const getPublicKey = async ({ | ||
bip32, | ||
mnemonic, | ||
derivationPath, | ||
network, | ||
typeOfAddress, | ||
}: PublicKeyParams): Promise<GetPublicKeyResult> => { | ||
const seed = await mnemonicToSeed(mnemonic); | ||
const rootKey = bip32.fromSeed(seed, network); | ||
const derivedNode = rootKey.derivePath(derivationPath); | ||
|
||
if (typeOfAddress === TypeOfAddress.P2TR) { | ||
const publicKeyDropedDERHeader = toXOnly(derivedNode.publicKey); | ||
|
||
const tweakedSigner = derivedNode.tweak( | ||
crypto.taggedHash('TapTweak', publicKeyDropedDERHeader), | ||
); | ||
|
||
return { tweakedSigner, publicKeyDropedDERHeader }; | ||
} | ||
|
||
return { derivedNode }; | ||
}; |
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 was deleted.
Oops, something went wrong.
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,2 +1 @@ | ||
export * from './liquidity.js'; | ||
export * from './bitcoin/index.js'; |
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