Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transaction insight caveat for accessing transaction origin (#902)
* Add transaction insight caveat for accessing transaction origin * Fix tests * Simplify caveat * Fix a validation issue * Fix tests * Update packages/snaps-controllers/src/snaps/endowments/transaction-insight.test.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
- Loading branch information
1 parent
ff21440
commit d8ae445
Showing
11 changed files
with
320 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 167 additions & 5 deletions
172
packages/snaps-controllers/src/snaps/endowments/transaction-insight.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,180 @@ | ||
import { PermissionType } from '@metamask/controllers'; | ||
import { transactionInsightEndowmentBuilder } from './transaction-insight'; | ||
import { PermissionConstraint, PermissionType } from '@metamask/controllers'; | ||
import { SnapCaveatType } from '@metamask/snaps-utils'; | ||
import { | ||
getTransactionInsightCaveatMapper, | ||
getTransactionOriginCaveat, | ||
transactionInsightCaveatSpecifications, | ||
transactionInsightEndowmentBuilder, | ||
} from './transaction-insight'; | ||
import { SnapEndowments } from '.'; | ||
|
||
describe('endowment:transaction-insight', () => { | ||
const specification = transactionInsightEndowmentBuilder.specificationBuilder( | ||
{}, | ||
); | ||
it('builds the expected permission specification', () => { | ||
const specification = | ||
transactionInsightEndowmentBuilder.specificationBuilder({}); | ||
expect(specification).toStrictEqual({ | ||
permissionType: PermissionType.Endowment, | ||
targetKey: SnapEndowments.TransactionInsight, | ||
allowedCaveats: [SnapCaveatType.TransactionOrigin], | ||
endowmentGetter: expect.any(Function), | ||
allowedCaveats: null, | ||
validator: expect.any(Function), | ||
}); | ||
|
||
expect(specification.endowmentGetter()).toBeUndefined(); | ||
}); | ||
|
||
describe('validator', () => { | ||
it('allows no caveats', () => { | ||
expect(() => | ||
// @ts-expect-error Missing required permission types. | ||
specification.validator({}), | ||
).not.toThrow(); | ||
}); | ||
|
||
it('throws if the caveat is not a single "transactionOrigin"', () => { | ||
expect(() => | ||
// @ts-expect-error Missing other required permission types. | ||
specification.validator({ | ||
caveats: [{ type: 'foo', value: 'bar' }], | ||
}), | ||
).toThrow('Expected a single "transactionOrigin" caveat.'); | ||
|
||
expect(() => | ||
// @ts-expect-error Missing other required permission types. | ||
specification.validator({ | ||
caveats: [ | ||
{ type: 'transactionOrigin', value: [] }, | ||
{ type: 'transactionOrigin', value: [] }, | ||
], | ||
}), | ||
).toThrow('Expected a single "transactionOrigin" caveat.'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getTransactionOriginCaveat', () => { | ||
it('returns the value from a transaction insight permission', () => { | ||
const permission: PermissionConstraint = { | ||
date: 0, | ||
parentCapability: 'foo', | ||
invoker: 'bar', | ||
id: 'baz', | ||
caveats: [ | ||
{ | ||
type: SnapCaveatType.TransactionOrigin, | ||
value: true, | ||
}, | ||
], | ||
}; | ||
|
||
expect(getTransactionOriginCaveat(permission)).toStrictEqual(true); | ||
}); | ||
|
||
it('returns null if the input is undefined', () => { | ||
expect(getTransactionOriginCaveat(undefined)).toBeNull(); | ||
}); | ||
|
||
it('returns null if the permission does not have caveats', () => { | ||
const permission: PermissionConstraint = { | ||
date: 0, | ||
parentCapability: 'foo', | ||
invoker: 'bar', | ||
id: 'baz', | ||
caveats: null, | ||
}; | ||
|
||
expect(getTransactionOriginCaveat(permission)).toBeNull(); | ||
}); | ||
|
||
it('throws if the permission does not have exactly one caveat', () => { | ||
const permission: PermissionConstraint = { | ||
date: 0, | ||
parentCapability: 'foo', | ||
invoker: 'bar', | ||
id: 'baz', | ||
caveats: [ | ||
{ | ||
type: SnapCaveatType.TransactionOrigin, | ||
value: true, | ||
}, | ||
{ | ||
type: SnapCaveatType.TransactionOrigin, | ||
value: true, | ||
}, | ||
], | ||
}; | ||
|
||
expect(() => getTransactionOriginCaveat(permission)).toThrow( | ||
'Assertion failed', | ||
); | ||
}); | ||
|
||
it('throws if the first caveat is not a "snapKeyring" caveat', () => { | ||
const permission: PermissionConstraint = { | ||
date: 0, | ||
parentCapability: 'foo', | ||
invoker: 'bar', | ||
id: 'baz', | ||
caveats: [ | ||
{ | ||
type: SnapCaveatType.PermittedCoinTypes, | ||
value: 'foo', | ||
}, | ||
], | ||
}; | ||
|
||
expect(() => getTransactionOriginCaveat(permission)).toThrow( | ||
'Assertion failed', | ||
); | ||
}); | ||
}); | ||
|
||
describe('getTransactionInsightCaveatMapper', () => { | ||
it('maps input to a caveat', () => { | ||
expect( | ||
getTransactionInsightCaveatMapper({ | ||
allowTransactionOrigin: true, | ||
}), | ||
).toStrictEqual({ | ||
caveats: [ | ||
{ | ||
type: 'transactionOrigin', | ||
value: true, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('does not include caveat if input is empty object', () => { | ||
expect(getTransactionInsightCaveatMapper({})).toStrictEqual({ | ||
caveats: null, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('transactionInsightCaveatSpecifications', () => { | ||
describe('validator', () => { | ||
it('throws if the caveat values are invalid', () => { | ||
expect(() => | ||
transactionInsightCaveatSpecifications[ | ||
SnapCaveatType.TransactionOrigin | ||
].validator?.( | ||
// @ts-expect-error Missing value type. | ||
{ | ||
type: SnapCaveatType.TransactionOrigin, | ||
}, | ||
), | ||
).toThrow('Expected a plain object.'); | ||
|
||
expect(() => | ||
transactionInsightCaveatSpecifications[ | ||
SnapCaveatType.TransactionOrigin | ||
].validator?.({ | ||
type: SnapCaveatType.TransactionOrigin, | ||
value: undefined, | ||
}), | ||
).toThrow('Expected caveat value to have type "boolean"'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.