-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0de9432
commit 04fb410
Showing
5 changed files
with
139 additions
and
90 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
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,58 @@ | ||
import { ethers } from 'ethers' | ||
import { GcpKmsSigner } from 'ethers-gcp-kms-signer' | ||
|
||
import { getChainIdForNetwork } from '@layerzerolabs/lz-definitions' | ||
|
||
export interface Signature { | ||
signature: string | ||
address: string | ||
} | ||
|
||
export function getVId(chainName: string, environment: string): string { | ||
// By convention the vid is always the endpointV1 chainId | ||
if (['solana', 'ton', 'initia', 'movement'].includes(chainName)) { | ||
const eid = getChainIdForNetwork(chainName, environment, '302') | ||
return (parseInt(eid) % 30000).toString() | ||
} | ||
return getChainIdForNetwork(chainName, environment, '2') | ||
} | ||
|
||
export function hashCallData( | ||
target: string, | ||
vId: string, | ||
expiration: number, | ||
callData: string, | ||
): string { | ||
return ethers.utils.keccak256( | ||
ethers.utils.solidityPack( | ||
['uint32', 'address', 'uint', 'bytes'], | ||
[vId, target, expiration, callData], | ||
), | ||
) | ||
} | ||
|
||
export async function getSignatures( | ||
signers: GcpKmsSigner[], | ||
hash: string, | ||
): Promise<Signature[]> { | ||
return await Promise.all( | ||
signers.map(async (signer) => ({ | ||
signature: await signer.signMessage(ethers.utils.arrayify(hash)), | ||
address: await signer.getAddress(), | ||
})), | ||
) | ||
} | ||
|
||
export function getSignaturesPayload( | ||
signatures: Signature[], | ||
quorum: number, | ||
): string { | ||
signatures.sort((a: Signature, b: Signature) => | ||
a.address.localeCompare(b.address), | ||
) | ||
const signaturesForQuorum = signatures.slice(0, quorum) | ||
return ethers.utils.solidityPack( | ||
signaturesForQuorum.map(() => 'bytes'), | ||
signaturesForQuorum.map((s: Signature) => s.signature), | ||
) | ||
} |