|
| 1 | +import { secp256k1 } from '@noble/curves/secp256k1'; |
| 2 | +import { randomBytes } from '@noble/hashes/utils'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { recoverUpdateMessageSigner, signedUpdateMessage } from '../../src/messages/index.js'; |
| 5 | +import { bytesToHex, hexToBytes } from '../../src/utils/index.js'; |
| 6 | + |
| 7 | +describe('sign updates and recover key', () => { |
| 8 | + it('creates a signed message from which you can recover a signing key', () => { |
| 9 | + const accountId = bytesToHex(randomBytes(20)); |
| 10 | + const secretKey = bytesToHex(new Uint8Array(32).fill(1)); |
| 11 | + const signaturePrivateKeyBytes = secp256k1.utils.randomPrivateKey(); |
| 12 | + const signaturePrivateKey = bytesToHex(signaturePrivateKeyBytes); |
| 13 | + const signaturePublicKey = bytesToHex(secp256k1.getPublicKey(signaturePrivateKeyBytes)); |
| 14 | + const spaceId = '0x1234'; |
| 15 | + const ephemeralId = bytesToHex(randomBytes(32)); |
| 16 | + |
| 17 | + const message = hexToBytes('0x01234abcdef01234'); |
| 18 | + |
| 19 | + const msg = signedUpdateMessage({ |
| 20 | + accountId, |
| 21 | + ephemeralId, |
| 22 | + spaceId, |
| 23 | + message, |
| 24 | + secretKey, |
| 25 | + signaturePrivateKey, |
| 26 | + }); |
| 27 | + |
| 28 | + // The signer should be recoverable without needing anything |
| 29 | + // outside of what's included in the message |
| 30 | + const recoveredSigner = recoverUpdateMessageSigner({ |
| 31 | + update: msg.update, |
| 32 | + spaceId: msg.spaceId, |
| 33 | + ephemeralId: msg.ephemeralId, |
| 34 | + signature: msg.signature, |
| 35 | + accountId: msg.accountId, |
| 36 | + }); |
| 37 | + |
| 38 | + expect(recoveredSigner).to.eq(signaturePublicKey); |
| 39 | + }); |
| 40 | +}); |
0 commit comments