Skip to content

Commit 8997a89

Browse files
CoveMBericglau
andauthored
Stellar add security contract field (#563)
Co-authored-by: Eric Lau <[email protected]>
1 parent 4da5a6f commit 8997a89

File tree

11 files changed

+120
-10457
lines changed

11 files changed

+120
-10457
lines changed

.changeset/busy-banks-shout.md

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,22 @@ test('contract with sorted use clauses', t => {
8282
Foo.addUseClause('another::library', 'self', { alias: 'custom1' });
8383
t.snapshot(printContract(Foo));
8484
});
85+
86+
test('contract with documentation', t => {
87+
const Foo = new ContractBuilder('Foo');
88+
Foo.addDocumentation('Some documentation');
89+
t.snapshot(printContract(Foo));
90+
});
91+
92+
test('contract with security info', t => {
93+
const Foo = new ContractBuilder('Foo');
94+
Foo.addSecurityTag('[email protected]');
95+
t.snapshot(printContract(Foo));
96+
});
97+
98+
test('contract with security info and documentation', t => {
99+
const Foo = new ContractBuilder('Foo');
100+
Foo.addSecurityTag('[email protected]');
101+
Foo.addDocumentation('Some documentation');
102+
t.snapshot(printContract(Foo));
103+
});

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,51 @@ Generated by [AVA](https://avajs.dev).
140140
#[contract]␊
141141
pub struct Foo;␊
142142
`
143+
144+
## contract with documentation
145+
146+
> Snapshot 1
147+
148+
`// SPDX-License-Identifier: MIT␊
149+
// Compatible with OpenZeppelin Stellar Soroban Contracts ^0.2.0␊
150+
151+
//! Some documentation␊
152+
#![no_std]␊
153+
154+
#[contract]␊
155+
pub struct Foo;␊
156+
`
157+
158+
## contract with security info
159+
160+
> Snapshot 1
161+
162+
`// SPDX-License-Identifier: MIT␊
163+
// Compatible with OpenZeppelin Stellar Soroban Contracts ^0.2.0␊
164+
165+
//! # Security␊
166+
//!␊
167+
//! For security issues, please contact: [email protected]
168+
#![no_std]␊
169+
170+
#[contract]␊
171+
pub struct Foo;␊
172+
`
173+
174+
## contract with security info and documentation
175+
176+
> Snapshot 1
177+
178+
`// SPDX-License-Identifier: MIT␊
179+
// Compatible with OpenZeppelin Stellar Soroban Contracts ^0.2.0␊
180+
181+
//! Some documentation␊
182+
183+
//! # Security␊
184+
//!␊
185+
//! For security issues, please contact: [email protected]
186+
#![no_std]␊
187+
188+
#[contract]␊
189+
pub struct Foo;␊
190+
`
117 Bytes
Binary file not shown.

packages/core/stellar/src/contract.ts

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

33
export interface Contract {
44
license: string;
5+
securityContact: string;
6+
documentations: string[];
57
name: string;
68
useClauses: UseClause[];
79
constructorCode: string[];
@@ -72,8 +74,11 @@ export interface Argument {
7274
export class ContractBuilder implements Contract {
7375
readonly name: string;
7476
license = 'MIT';
77+
securityContact = '';
7578
ownable = false;
7679

80+
readonly documentations: string[] = [];
81+
7782
readonly constructorArgs: Argument[] = [];
7883
readonly constructorCode: string[] = [];
7984

@@ -250,4 +255,12 @@ export class ContractBuilder implements Contract {
250255
addDerives(derive: string): void {
251256
this.derivesSet.add(derive);
252257
}
258+
259+
addDocumentation(description: string) {
260+
this.documentations.push(description);
261+
}
262+
263+
addSecurityTag(securityContact: string) {
264+
this.securityContact = securityContact;
265+
}
253266
}

packages/core/stellar/src/print.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export function printContract(contract: Contract): string {
2323
[
2424
`// SPDX-License-Identifier: ${contract.license}`,
2525
`// Compatible with OpenZeppelin Stellar Soroban Contracts ${compatibleContractsSemver}`,
26+
...(contract.documentations.length ? ['', ...printDocumentations(contract.documentations)] : []),
27+
...(contract.securityContact ? ['', ...printSecurityTag(contract.securityContact)] : []),
2628
...createLevelAttributes,
2729
],
2830
spaceBetween(
@@ -360,3 +362,11 @@ function printArgument(arg: Argument): string {
360362
return `${arg.name}`;
361363
}
362364
}
365+
366+
function printDocumentations(documentations: string[]): string[] {
367+
return documentations.map(documentation => `//! ${documentation}`);
368+
}
369+
370+
function printSecurityTag(securityContact: string) {
371+
return ['//! # Security', '//!', `//! For security issues, please contact: ${securityContact}`];
372+
}

packages/core/stellar/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 TAG_SECURITY_CONTACT = `@custom: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.addSecurityTag(securityContact);
19+
}
1320

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

0 commit comments

Comments
 (0)