-
Notifications
You must be signed in to change notification settings - Fork 4
Initial Handshake Implementation #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba8311c
implemented handshake rosen-extractor code and tests
AcoSmrkas e055947
rosen-extractor handshake types.ts commit
AcoSmrkas f0fa196
implemented handshake address-codec code and tests
AcoSmrkas 24ebdf5
updated transaction handling to match new lock structure
AcoSmrkas 9ec5917
updated handshake data extraction method to use bitcoin-runes-like me…
AcoSmrkas 81ec3c5
merged latest dev -> handshake, resolved conflicts
AcoSmrkas ba895c4
update based on requested changes
AcoSmrkas 1c43e22
Merge branch 'dev' into handshake
AcoSmrkas 6d06463
.npmrc new line
AcoSmrkas fb93a5c
better lock output search, added changeset, lint
AcoSmrkas 2cf8228
Merge branch 'dev' into handshake
AcoSmrkas c6039da
test data refactor
AcoSmrkas 1345eaa
Merge branch 'dev' into handshake
AcoSmrkas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@rosen-bridge/address-codec': minor | ||
| --- | ||
|
|
||
| Add support for Handshake chain |
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@rosen-bridge/rosen-extractor': minor | ||
| --- | ||
|
|
||
| Add Handshake chain rosen-extractor |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
packages/rosen-extractor/lib/getRosenData/handshake/constants.ts
This file contains hidden or 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,13 @@ | ||
| export const HANDSHAKE_NETWORK = { | ||
| messagePrefix: '\x18Handshake Signed Message:\n', | ||
| bech32: 'hs', | ||
| bip32: { | ||
| public: 0x0488b21e, | ||
| private: 0x0488ade4, | ||
| }, | ||
| pubKeyHash: -1, | ||
| scriptHash: -1, | ||
| wif: -1, | ||
| }; | ||
|
|
||
| export const MIN_UTXO_VALUE = 1000; |
140 changes: 140 additions & 0 deletions
140
packages/rosen-extractor/lib/getRosenData/handshake/handshakeRosenExtractor.ts
This file contains hidden or 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,140 @@ | ||
| import { RosenData, TokenTransformation } from '../abstract/types'; | ||
| import AbstractRosenDataExtractor from '../abstract/abstractRosenDataExtractor'; | ||
| import { HANDSHAKE_CHAIN, HANDSHAKE_NATIVE_TOKEN } from '../const'; | ||
| import { HandshakeTx, HandshakeTxOutput, HandshakeRosenData } from './types'; | ||
| import { TokenMap } from '@rosen-bridge/tokens'; | ||
| import { AbstractLogger } from '@rosen-bridge/abstract-logger'; | ||
| import { addressToHash, extractDataFromOutputs } from './utils'; | ||
| import { parseRosenData } from '../../utils'; | ||
| import JsonBigInt from '@rosen-bridge/json-bigint'; | ||
|
|
||
| export class HandshakeRosenExtractor extends AbstractRosenDataExtractor<string> { | ||
| readonly chain = HANDSHAKE_CHAIN; | ||
| protected lockAddressHash: string; | ||
|
|
||
| constructor(lockAddress: string, tokens: TokenMap, logger?: AbstractLogger) { | ||
| super(lockAddress, tokens, logger); | ||
| this.lockAddressHash = addressToHash(lockAddress); | ||
| } | ||
|
|
||
| /** | ||
| * extracts RosenData from given lock transaction in HandshakeTx format | ||
| * @param serializedTransaction stringified transaction in HandshakeTx format | ||
| */ | ||
| extractData = (serializedTransaction: string): RosenData | undefined => { | ||
| let transaction: HandshakeTx; | ||
| try { | ||
| transaction = JsonBigInt.parse(serializedTransaction); | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Failed to parse transaction json to HandshakeTx format while extracting rosen data: ${e}`, | ||
| ); | ||
| } | ||
|
|
||
| const baseError = `No rosen data found for tx [${transaction.id}]`; | ||
| try { | ||
| const outputs = transaction.outputs; | ||
| if (outputs.length < 2) { | ||
| this.logger.debug(baseError + `: Insufficient number of outputs`); | ||
| return undefined; | ||
| } | ||
|
|
||
| // Find lock output and position | ||
| const lockOutputIndex = outputs.findIndex( | ||
| (output) => output.address?.hash === this.lockAddressHash, | ||
| ); | ||
|
|
||
| if (lockOutputIndex === -1) { | ||
| this.logger.debug(baseError + `: Lock output not found`); | ||
| return undefined; | ||
| } | ||
|
|
||
| const lockOutput = outputs[lockOutputIndex]; | ||
|
|
||
| // Extract data from outputs using utility function | ||
| const reconstructedData = extractDataFromOutputs( | ||
| outputs, | ||
| lockOutputIndex, | ||
| ); | ||
|
|
||
| if (!reconstructedData) { | ||
| this.logger.debug(baseError + `: No data chunks found`); | ||
| return undefined; | ||
| } | ||
|
|
||
| // Parse the reconstructed data | ||
| let rosenData: HandshakeRosenData | undefined; | ||
| try { | ||
| rosenData = parseRosenData(reconstructedData); | ||
| this.logger.debug( | ||
| `Successfully extracted Rosen data for [${rosenData.toChain}]`, | ||
| ); | ||
| } catch (e) { | ||
| this.logger.debug( | ||
| baseError + `: Failed to parse reconstructed data: ${e}`, | ||
| ); | ||
| return undefined; | ||
| } | ||
|
|
||
| // Find asset transformation using the lock output | ||
| const assetTransformation = this.getAssetTransformation( | ||
| lockOutput, | ||
| rosenData.toChain, | ||
| ); | ||
|
|
||
| if (!assetTransformation) { | ||
| this.logger.debug( | ||
| baseError + `: Failed to find rosen asset transformation`, | ||
| ); | ||
| return undefined; | ||
| } | ||
|
|
||
| const fromAddress = `box:${transaction.inputs[0].txId}.${transaction.inputs[0].index}`; | ||
| return { | ||
| toChain: rosenData.toChain, | ||
| toAddress: rosenData.toAddress, | ||
| bridgeFee: rosenData.bridgeFee, | ||
| networkFee: rosenData.networkFee, | ||
| fromAddress: fromAddress, | ||
| sourceChainTokenId: assetTransformation.from, | ||
| amount: assetTransformation.amount, | ||
| targetChainTokenId: assetTransformation.to, | ||
| sourceTxId: transaction.id, | ||
| rawData: outputs | ||
| .map((output) => `${output.address?.hash}:${output.value}`) | ||
| .join(','), | ||
| }; | ||
| } catch (e) { | ||
| this.logger.debug( | ||
| `An error occurred while getting Handshake rosen data: ${e}`, | ||
| ); | ||
| if (e instanceof Error && e.stack) { | ||
| this.logger.debug(e.stack); | ||
| } | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * extracts and builds token transformation from UTXO and tokenMap | ||
| * @param box transaction output | ||
| * @param toChain event target chain | ||
| */ | ||
| getAssetTransformation = ( | ||
| box: HandshakeTxOutput, | ||
| toChain: string, | ||
| ): TokenTransformation | undefined => { | ||
| const wrappedHns = this.tokens.search(HANDSHAKE_CHAIN, { | ||
| tokenId: HANDSHAKE_NATIVE_TOKEN, | ||
| }); | ||
|
|
||
| if (wrappedHns.length > 0 && Object.hasOwn(wrappedHns[0], toChain)) { | ||
| return { | ||
| from: HANDSHAKE_NATIVE_TOKEN, | ||
| to: this.tokens.getID(wrappedHns[0], toChain), | ||
| amount: box.value.toString(), | ||
| }; | ||
| } | ||
| return undefined; | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.