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.

48 lines
866 B

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const assert = require("assert");
  2. function writeUint32(h, val) {
  3. h.dataView.setUint32(h.offset, val, true);
  4. h.offset += 4;
  5. }
  6. function writeBigInt(h, bi) {
  7. for (let i=0; i<8; i++) {
  8. const v = bi.shiftRight(i*32).and(0xFFFFFFFF).toJSNumber();
  9. writeUint32(h, v);
  10. }
  11. }
  12. function calculateBuffLen(witness) {
  13. let size = 0;
  14. // beta2, delta2
  15. size += witness.length * 32;
  16. return size;
  17. }
  18. function buildWitness(witness) {
  19. const buffLen = calculateBuffLen(witness);
  20. const buff = new ArrayBuffer(buffLen);
  21. const h = {
  22. dataView: new DataView(buff),
  23. offset: 0
  24. };
  25. // writeUint32(h, witness.length);
  26. for (let i=0; i<witness.length; i++) {
  27. writeBigInt(h, witness[i]);
  28. }
  29. assert.equal(h.offset, buffLen);
  30. return Buffer.from(buff);
  31. }
  32. module.exports = buildWitness;