Skip to content
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

feat: update utility functions to accomodate permissionless and permissioned handlers #132

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/sdk/src/Sygma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import {
import { EvmBridge } from './chains';
import { calculateBasicfee, calculateFeeData } from './fee';
import Connector from './connectors/Connectors';
import { createGenericDepositDataV1, toHex } from './utils/helpers';
import {
createPermissionedGenericDepositData,
createPermissionlessGenericDepositData,
toHex,
} from './utils/helpers';

/**
* @description Sygma is the main class that allows you to have bridging capabilities
Expand Down Expand Up @@ -720,15 +724,15 @@ export class Sygma implements SygmaSDK {
return await listTokensOfOwner({ token, account, signer });
}

public createGenericDepositDataV1(
public formatPermissionlessGenericDepositData(
executeFunctionSignature: string,
executeContractAddress: string,
maxFee: string,
depositor: string,
executionData: string,
depositorCheck = true,
depositorCheck: boolean = true,
): string {
const depositData = createGenericDepositDataV1(
const depositData = createPermissionlessGenericDepositData(
executeFunctionSignature,
executeContractAddress,
maxFee,
Expand All @@ -740,6 +744,12 @@ export class Sygma implements SygmaSDK {
return depositData;
}

public formatPermissionedGenericDepositData(hexMetaData: string): string {
const depositData = createPermissionedGenericDepositData(hexMetaData);

return depositData;
}

public toHex(toConvert: string, padding: number): string {
return toHex(toConvert, padding);
}
Expand Down
33 changes: 20 additions & 13 deletions packages/sdk/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,36 @@ export const createERCDepositData = (
); // recipientAddress (?? bytes)
};

export const createGenericDepositDataV1 = (
export const createPermissionedGenericDepositData = (hexMetaData: string): string => {
if (hexMetaData === null) {
return '0x' + toHex(0, 32).substr(2); // len(metaData) (32 bytes)
}
const hexMetaDataLength = hexMetaData.substr(2).length / 2;
return '0x' + toHex(hexMetaDataLength, 32).substr(2) + hexMetaData.substr(2);
};

export const createPermissionlessGenericDepositData = (
executeFunctionSignature: string,
executeContractAddress: string,
maxFee: string,
depositor: string,
executionData: string,
depositorCheck = true,
depositorCheck: boolean = true,
): string => {
let metaData = toHex(depositor, 32).substr(2) + executionData.substr(2);

if (depositorCheck) {
// if "depositorCheck" is true -> append depositor address for destination chain check
metaData = metaData.concat(toHex(depositor, 32).substr(2));
executionData = executionData.concat(toHex(depositor, 32).substr(2));
}

const metaDataLength = metaData.length / 2;

return (
'0x' +
toHex(metaDataLength, 32).substr(2) + // len(metaData) (32 bytes)
toHex(executeFunctionSignature, 32).substr(2) + // bytes4 (padded to 32 bytes)
toHex(executeContractAddress, 32).substr(2) + // address (padded to 32 bytes)
toHex(maxFee, 32).substr(2) + // uint256
metaData
); // bytes
toHex(executeFunctionSignature.substr(2).length / 2, 2).substr(2) + // uint16
executeFunctionSignature.substr(2) + // bytes
toHex(executeContractAddress.substr(2).length / 2, 1).substr(2) + // uint8
executeContractAddress.substr(2) + // bytes
toHex(32, 1).substr(2) + // uint8
toHex(depositor, 32).substr(2) + // bytes32
executionData.substr(2)
) // bytes
.toLowerCase();
};
2 changes: 1 addition & 1 deletion packages/sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
},
"include": [
"src",
"integration"
// "integration"
],
}