import { NetworkContext } from "@counterfactual/types" ; import { JsonRpcProvider } from "ethers/providers" ; import { SigningKey } from "ethers/utils" ; import { HDNode } from "ethers/utils/hdnode" ; import { Opcode , ProtocolRunner } from "../../../src/engine" ; import { EthereumCommitment } from "../../../src/ethereum/types" ; import { Store } from "../../../src/store" ; import { MemoryStoreService } from "../../services/memory-store-service" ; import { getRandomHDNodes } from "./random-signing-keys" ; const makeSigner = ( hdNode ) => { return async ( args ) => { if ( args . length !== 1 && args . length !== 2 ) { throw Error ( "OP_SIGN middleware received wrong number of arguments." ) ; } const [ commitment , overrideKeyIndex ] = args ; const keyIndex = overrideKeyIndex || 0 ; const signingKey = new SigningKey ( hdNode . derivePath ( `${ keyIndex }` ) . privateKey ) ; return signingKey . signDigest ( commitment . hashToSign ( ) ) ; } ; } ; export class MiniNode { private readonly hdNode : HDNode ; public readonly protocolRunner : ProtocolRunner ; public store : Store ; public readonly xpub : string ; constructor ( readonly networkContext , readonly provider ) { [ this . hdNode ] = getRandomHDNodes ( 1 ) ; this . xpub = this . hdNode . neuter ( ) . extendedKey ; this . store = new Store ( new MemoryStoreService ( ) , "magic" ) ; this . protocolRunner = new ProtocolRunner ( this . store , networkContext , provider ) ; this . protocolRunner . register ( Opcode . OP_SIGN , makeSigner ( this . hdNode ) ) ; } public async dispatchMessage ( message ) { await this . protocolRunner . runProtocolWithMessage ( message ) ; } }