You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.2 KiB
123 lines
3.2 KiB
3 years ago
|
import { assert } from 'chai';
|
||
|
import { ethers } from 'ethers';
|
||
|
|
||
|
import * as types from './types';
|
||
|
|
||
|
const typeToByte = (type: number): string => `0x0${type}`;
|
||
|
|
||
|
const MESSAGE_LEN = {
|
||
|
identifier: 1,
|
||
|
tokenId: 36,
|
||
|
transfer: 65,
|
||
|
details: 66,
|
||
3 years ago
|
requestDetails: 1,
|
||
|
};
|
||
3 years ago
|
|
||
|
// Formats Transfer Message
|
||
3 years ago
|
export function formatTransfer(
|
||
|
to: ethers.BytesLike,
|
||
|
amnt: number | ethers.BytesLike,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
return ethers.utils.solidityPack(
|
||
|
['bytes1', 'bytes32', 'uint256'],
|
||
3 years ago
|
[types.BridgeMessageTypes.TRANSFER, to, amnt],
|
||
3 years ago
|
);
|
||
|
}
|
||
|
|
||
|
// Formats Details Message
|
||
3 years ago
|
export function formatDetails(
|
||
|
name: string,
|
||
|
symbol: string,
|
||
|
decimals: number,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
return ethers.utils.solidityPack(
|
||
|
['bytes1', 'bytes32', 'bytes32', 'uint8'],
|
||
3 years ago
|
[types.BridgeMessageTypes.DETAILS, name, symbol, decimals],
|
||
3 years ago
|
);
|
||
|
}
|
||
|
|
||
|
// Formats Request Details message
|
||
|
export function formatRequestDetails(): ethers.BytesLike {
|
||
3 years ago
|
return ethers.utils.solidityPack(
|
||
|
['bytes1'],
|
||
3 years ago
|
[types.BridgeMessageTypes.REQUEST_DETAILS],
|
||
3 years ago
|
);
|
||
3 years ago
|
}
|
||
|
|
||
|
// Formats the Token ID
|
||
|
export function formatTokenId(domain: number, id: string): ethers.BytesLike {
|
||
|
return ethers.utils.solidityPack(['uint32', 'bytes32'], [domain, id]);
|
||
|
}
|
||
|
|
||
3 years ago
|
export function formatMessage(
|
||
|
tokenId: ethers.BytesLike,
|
||
|
action: ethers.BytesLike,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
return ethers.utils.solidityPack(['bytes', 'bytes'], [tokenId, action]);
|
||
|
}
|
||
|
|
||
3 years ago
|
export function serializeTransferAction(
|
||
|
transferAction: types.TransferAction,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
const { type, recipient, amount } = transferAction;
|
||
|
|
||
3 years ago
|
assert(type === types.BridgeMessageTypes.TRANSFER);
|
||
3 years ago
|
return formatTransfer(recipient, amount);
|
||
|
}
|
||
|
|
||
3 years ago
|
export function serializeDetailsAction(
|
||
|
detailsAction: types.DetailsAction,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
const { type, name, symbol, decimals } = detailsAction;
|
||
3 years ago
|
|
||
3 years ago
|
assert(type === types.BridgeMessageTypes.DETAILS);
|
||
3 years ago
|
return formatDetails(name, symbol, decimals);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
export function serializeRequestDetailsAction(
|
||
|
requestDetailsAction: types.RequestDetailsAction,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
assert(
|
||
|
requestDetailsAction.type === types.BridgeMessageTypes.REQUEST_DETAILS,
|
||
|
);
|
||
3 years ago
|
return formatRequestDetails();
|
||
|
}
|
||
|
|
||
|
export function serializeAction(action: types.Action): ethers.BytesLike {
|
||
|
let actionBytes: ethers.BytesLike = [];
|
||
3 years ago
|
switch (action.type) {
|
||
3 years ago
|
case types.BridgeMessageTypes.TRANSFER: {
|
||
|
actionBytes = serializeTransferAction(action as types.TransferAction);
|
||
3 years ago
|
break;
|
||
|
}
|
||
3 years ago
|
case types.BridgeMessageTypes.DETAILS: {
|
||
|
actionBytes = serializeDetailsAction(action as types.DetailsAction);
|
||
3 years ago
|
break;
|
||
|
}
|
||
3 years ago
|
case types.BridgeMessageTypes.REQUEST_DETAILS: {
|
||
3 years ago
|
actionBytes = serializeRequestDetailsAction(action);
|
||
|
break;
|
||
|
}
|
||
|
default: {
|
||
3 years ago
|
console.error('Invalid action');
|
||
3 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
return actionBytes;
|
||
|
}
|
||
|
|
||
3 years ago
|
export function serializeTokenId(
|
||
|
tokenId: types.TokenIdentifier,
|
||
|
): ethers.BytesLike {
|
||
3 years ago
|
if (typeof tokenId.domain !== 'number' || typeof tokenId.id !== 'string') {
|
||
|
throw new Error('!types');
|
||
|
}
|
||
|
return formatTokenId(tokenId.domain as number, tokenId.id as string);
|
||
3 years ago
|
}
|
||
|
|
||
|
export function serializeMessage(message: types.Message): ethers.BytesLike {
|
||
|
const tokenId = serializeTokenId(message.tokenId);
|
||
|
const action = serializeAction(message.action);
|
||
3 years ago
|
return formatMessage(tokenId, action);
|
||
3 years ago
|
}
|