Skip to content

Commit d3bc0e1

Browse files
CoveMBericglau
andauthored
Cairo add security contract field (#558)
Co-authored-by: Eric Lau <[email protected]>
1 parent 4871cb4 commit d3bc0e1

File tree

17 files changed

+126
-2
lines changed

17 files changed

+126
-2
lines changed

.changeset/whole-buses-call.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@openzeppelin/wizard-cairo': patch
3+
---
4+
5+
Add security contact in contract info
6+

packages/core/cairo/src/contract.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from 'ava';
33
import type { BaseFunction, BaseImplementedTrait, Component } from './contract';
44
import { ContractBuilder } from './contract';
55
import { printContract } from './print';
6+
import { SECURITY_CONTACT_DOCUMENTATION } from './set-info';
67

78
const FOO_COMPONENT: Component = {
89
name: 'FooComponent',
@@ -106,3 +107,9 @@ test('contract with sorted use clauses', t => {
106107
Foo.addUseClause('another::library', 'Foo', { alias: 'Custom1' });
107108
t.snapshot(printContract(Foo));
108109
});
110+
111+
test('contract with info', t => {
112+
const Foo = new ContractBuilder('Foo');
113+
Foo.addDocumentation(`${SECURITY_CONTACT_DOCUMENTATION}[email protected]`);
114+
t.snapshot(printContract(Foo));
115+
});

packages/core/cairo/src/contract.test.ts.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,19 @@ Generated by [AVA](https://avajs.dev).
231231
}␊
232232
}␊
233233
`
234+
235+
## contract with info
236+
237+
> Snapshot 1
238+
239+
`// SPDX-License-Identifier: MIT␊
240+
// Compatible with OpenZeppelin Contracts for Cairo ^1.0.0␊
241+
242+
// Security contact: [email protected]
243+
#[starknet::contract]␊
244+
mod Foo {␊
245+
#[storage]␊
246+
struct Storage {␊
247+
}␊
248+
}␊
249+
`
51 Bytes
Binary file not shown.

packages/core/cairo/src/contract.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { toIdentifier } from './utils/convert-strings';
22

33
export interface Contract {
44
license: string;
5+
documentations: string[];
56
name: string;
67
account: boolean;
78
useClauses: UseClause[];
@@ -103,6 +104,8 @@ export class ContractBuilder implements Contract {
103104
license = 'MIT';
104105
upgradeable = false;
105106

107+
readonly documentations: string[] = [];
108+
106109
readonly constructorArgs: Argument[] = [];
107110
readonly constructorCode: string[] = [];
108111

@@ -298,4 +301,8 @@ export class ContractBuilder implements Contract {
298301
addInterfaceFlag(flag: string): void {
299302
this.interfaceFlagsSet.add(flag);
300303
}
304+
305+
addDocumentation(description: string) {
306+
this.documentations.push(description);
307+
}
301308
}

packages/core/cairo/src/print.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function printContract(contract: Contract): string {
2929
],
3030
printSuperVariables(contract),
3131
[
32+
...printDocumentations(contract.documentations),
3233
`${contractAttribute}`,
3334
`mod ${contract.name} {`,
3435
spaceBetween(
@@ -427,3 +428,7 @@ function printArgument(arg: Argument): string {
427428
return `${arg.name}`;
428429
}
429430
}
431+
432+
function printDocumentations(documentations: string[]): string[] {
433+
return documentations.map(documentation => `// ${documentation}`);
434+
}

packages/core/cairo/src/set-info.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import type { ContractBuilder } from './contract';
22

3+
export const SECURITY_CONTACT_DOCUMENTATION = `Security contact: `;
4+
35
export const infoOptions = [{}, { license: 'WTFPL' }] as const;
46

57
export const defaults: Info = { license: 'MIT' };
68

79
export type Info = {
810
license?: string;
11+
securityContact?: string;
912
};
1013

1114
export function setInfo(c: ContractBuilder, info: Info): void {
12-
const { license } = info;
15+
const { securityContact, license } = info;
16+
17+
if (securityContact) {
18+
c.addDocumentation(`${SECURITY_CONTACT_DOCUMENTATION}${securityContact}`);
19+
}
1320

1421
if (license) {
1522
c.license = license;

packages/core/cairo_alpha/src/contract.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from 'ava';
33
import type { BaseFunction, BaseImplementedTrait, Component } from './contract';
44
import { ContractBuilder } from './contract';
55
import { printContract } from './print';
6+
import { SECURITY_CONTACT_DOCUMENTATION } from './set-info';
67

78
const FOO_COMPONENT: Component = {
89
name: 'FooComponent',
@@ -106,3 +107,9 @@ test('contract with sorted use clauses', t => {
106107
Foo.addUseClause('another::library', 'Foo', { alias: 'Custom1' });
107108
t.snapshot(printContract(Foo));
108109
});
110+
111+
test('contract with info', t => {
112+
const Foo = new ContractBuilder('Foo');
113+
Foo.addDocumentation(`${SECURITY_CONTACT_DOCUMENTATION}[email protected]`);
114+
t.snapshot(printContract(Foo));
115+
});

packages/core/cairo_alpha/src/contract.test.ts.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,19 @@ Generated by [AVA](https://avajs.dev).
231231
}␊
232232
}␊
233233
`
234+
235+
## contract with info
236+
237+
> Snapshot 1
238+
239+
`// SPDX-License-Identifier: MIT␊
240+
// Compatible with OpenZeppelin Contracts for Cairo 2.0.0-alpha.1␊
241+
242+
// Security contact: [email protected]
243+
#[starknet::contract]␊
244+
mod Foo {␊
245+
#[storage]␊
246+
struct Storage {␊
247+
}␊
248+
}␊
249+
`
Binary file not shown.

0 commit comments

Comments
 (0)