createLocalnetGovernanceVaa.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const abi = require("web3-eth-abi");
  2. const utils = require("web3-utils");
  3. const elliptic = require("elliptic");
  4. const testSigner1PK =
  5. "cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
  6. const testGovernanceChain = "1"; // ethereum
  7. const testGovernanceEmitter =
  8. "0x0000000000000000000000000000000000000000000000000000000000001234";
  9. function zeroPadBytes(value, length) {
  10. while (value.length < 2 * length) {
  11. value = "0" + value;
  12. }
  13. return value;
  14. }
  15. const signAndEncodeVM = function (
  16. timestamp,
  17. nonce,
  18. emitterChainId,
  19. emitterAddress,
  20. sequence,
  21. data,
  22. signers,
  23. guardianSetIndex,
  24. consistencyLevel,
  25. ) {
  26. const body = [
  27. abi.encodeParameter("uint32", timestamp).substring(2 + (64 - 8)),
  28. abi.encodeParameter("uint32", nonce).substring(2 + (64 - 8)),
  29. abi.encodeParameter("uint16", emitterChainId).substring(2 + (64 - 4)),
  30. abi.encodeParameter("bytes32", emitterAddress).substring(2),
  31. abi.encodeParameter("uint64", sequence).substring(2 + (64 - 16)),
  32. abi.encodeParameter("uint8", consistencyLevel).substring(2 + (64 - 2)),
  33. data.substr(2),
  34. ];
  35. const hash = utils.soliditySha3(utils.soliditySha3("0x" + body.join("")));
  36. let signatures = "";
  37. for (let i in signers) {
  38. const ec = new elliptic.ec("secp256k1");
  39. const key = ec.keyFromPrivate(signers[i]);
  40. const signature = key.sign(hash.substr(2), { canonical: true });
  41. const packSig = [
  42. abi.encodeParameter("uint8", i).substring(2 + (64 - 2)),
  43. zeroPadBytes(signature.r.toString(16), 32),
  44. zeroPadBytes(signature.s.toString(16), 32),
  45. abi
  46. .encodeParameter("uint8", signature.recoveryParam)
  47. .substr(2 + (64 - 2)),
  48. ];
  49. signatures += packSig.join("");
  50. }
  51. const vm = [
  52. abi.encodeParameter("uint8", 1).substring(2 + (64 - 2)),
  53. abi.encodeParameter("uint32", guardianSetIndex).substring(2 + (64 - 8)),
  54. abi.encodeParameter("uint8", signers.length).substring(2 + (64 - 2)),
  55. signatures,
  56. body.join(""),
  57. ].join("");
  58. return vm;
  59. };
  60. function createVAAFromUint8Array(
  61. dataBuffer,
  62. emitterChainId,
  63. emitterAddress,
  64. sequence,
  65. ) {
  66. const dataHex = "0x" + dataBuffer.toString("hex");
  67. return (
  68. "0x" +
  69. signAndEncodeVM(
  70. 0,
  71. 0,
  72. emitterChainId,
  73. emitterAddress,
  74. sequence,
  75. dataHex,
  76. [testSigner1PK],
  77. 0,
  78. 0,
  79. )
  80. );
  81. }
  82. module.exports = function createLocalnetGovernanceVAA(dataBuffer, sequence) {
  83. return createVAAFromUint8Array(
  84. dataBuffer,
  85. testGovernanceChain,
  86. testGovernanceEmitter,
  87. sequence,
  88. );
  89. };