Skip to content

Commit

Permalink
feat: session needs to be aware of the whole tree
Browse files Browse the repository at this point in the history
to generate proof paths
  • Loading branch information
janek26 committed Aug 18, 2022
1 parent eea363f commit 11e10bd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 2 additions & 0 deletions __tests__/utils/__snapshots__/ellipticalCurve.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pedersen() 1`] = `"0x5ed2703dfdb505c587700ce2ebfcab5b3515cd7e6114817e6026ec9d4b364ca"`;

exports[`pedersen() with 0 1`] = `"0x1a5c561f97b52c17a19f34c4499a745cd4e8412e29e4ed5e91e4481c7d53935"`;
5 changes: 5 additions & 0 deletions __tests__/utils/ellipticalCurve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ test('pedersen()', () => {
expect(own).toMatchSnapshot();
});

test('pedersen() with 0', () => {
const own = pedersen(['0x12773', '0x0']);
expect(own).toMatchSnapshot();
});

test('computeHashOnElements()', () => {
const array = ['1', '2', '3', '4'];
expect(computeHashOnElements(array)).toBe(
Expand Down
9 changes: 8 additions & 1 deletion src/account/session.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from 'minimalistic-assert';

import { ZERO } from '../constants';
import { ProviderInterface, ProviderOptions } from '../provider';
import { SignerInterface } from '../signer';
Expand All @@ -12,8 +14,9 @@ import {
KeyPair,
} from '../types';
import { feeTransactionVersion, transactionVersion } from '../utils/hash';
import { MerkleTree } from '../utils/merkle';
import { BigNumberish, toBN } from '../utils/number';
import type { SignedSession } from '../utils/session';
import { SignedSession, createMerkleTreeForPolicies } from '../utils/session';
import { compileCalldata, estimatedFeeToMaxFee } from '../utils/stark';
import { fromCallsToExecuteCalldataWithNonce } from '../utils/transaction';
import { Account } from './default';
Expand All @@ -23,13 +26,17 @@ const SESSION_PLUGIN_CLASS_HASH =
'0x6a184757e350de1fe3a544037efbef6434724980a572f294c90555dadc20052';

export class SessionAccount extends Account implements AccountInterface {
public merkleTree: MerkleTree;

constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
address: string,
keyPairOrSigner: KeyPair | SignerInterface,
public signedSession: SignedSession
) {
super(providerOrOptions, address, keyPairOrSigner);
this.merkleTree = createMerkleTreeForPolicies(signedSession.policies);
assert(signedSession.root === this.merkleTree.root, 'Invalid session');
}

private async sessionToCall(session: SignedSession): Promise<Call> {
Expand Down
14 changes: 8 additions & 6 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ export function pedersen(input: [BigNumberish, BigNumberish]) {
for (let i = 0; i < input.length; i += 1) {
let x = toBN(input[i]);
assert(x.gte(ZERO) && x.lt(toBN(addHexPrefix(FIELD_PRIME))), `Invalid input: ${input[i]}`);
for (let j = 0; j < 252; j += 1) {
const pt = constantPoints[2 + i * 252 + j];
assert(!point.getX().eq(pt.getX()));
if (x.and(ONE).toNumber() !== 0) {
point = point.add(pt);
if (!x.isZero()) {
for (let j = 0; j < 252; j += 1) {
const pt = constantPoints[2 + i * 252 + j];
assert(!point.getX().eq(pt.getX()));
if (x.and(ONE).toNumber() !== 0) {
point = point.add(pt);
}
x = x.shrn(1);
}
x = x.shrn(1);
}
}
return addHexPrefix(point.getX().toString(16));
Expand Down
19 changes: 10 additions & 9 deletions src/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ interface Policy {
selector: string;
}

interface BaseSession {
export interface RequestSession {
key: string;
expires: number;
}

export interface RequestSession extends BaseSession {
policies: Policy[];
}

export interface PreparedSession extends BaseSession {
export interface PreparedSession extends RequestSession {
root: string;
}

Expand All @@ -30,18 +27,21 @@ function preparePolicy({ contractAddress, selector }: Policy): string {
return pedersen([contractAddress, prepareSelector(selector)]);
}

export function createMerkleTreeForPolicies(policies: Policy[]): MerkleTree {
return new MerkleTree(policies.map(preparePolicy));
}

export function prepareSession(session: RequestSession): PreparedSession {
const { policies, ...rest } = session;
const { root } = new MerkleTree(policies.map(preparePolicy));
return { ...rest, root };
const { root } = createMerkleTreeForPolicies(session.policies);
return { ...session, root };
}

export async function createSession(
session: RequestSession,
account: AccountInterface,
domain: StarkNetDomain = {}
): Promise<SignedSession> {
const { key, expires, root } = prepareSession(session);
const { expires, key, policies, root } = prepareSession(session);
const signature = await account.signMessage({
primaryType: 'Session',
types: {
Expand Down Expand Up @@ -69,6 +69,7 @@ export async function createSession(
});
return {
key,
policies,
expires,
root,
signature,
Expand Down

0 comments on commit 11e10bd

Please sign in to comment.