Skip to content

Commit 8a36950

Browse files
authored
Merge pull request #2 from EIPs-CodeLab/feat/impl-structs
feat: add data structs
2 parents c1d5034 + 1d7b075 commit 8a36950

2 files changed

Lines changed: 178 additions & 0 deletions

File tree

src/structs/PackageStructs.sol

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
/// @title PackageStructs
5+
/// @notice All data structures from EIP-2678 (EthPM v3)
6+
/// @dev Based on https://eips.ethereum.org/EIPS/eip-2678
7+
library PackageStructs {
8+
9+
/// @notice Package Meta Object from EIP-2678
10+
struct PackageMeta {
11+
string[] authors; // Human readable author names
12+
string license; // SPDX format license
13+
string description; // Package description
14+
string[] keywords; // Relevant keywords
15+
string website; // Primary website
16+
string documentation; // Documentation link
17+
string repository; // Source code location
18+
}
19+
20+
/// @notice Compiler Information Object from EIP-2678
21+
struct CompilerInfo {
22+
string name; // Compiler name (e.g., "solc")
23+
string version; // Semver or commit hash (e.g., "0.8.0" or "0.4.8-commit.60cc1668")
24+
bytes settings; // Compiler settings as bytes
25+
}
26+
27+
/// @notice Contract Type from EIP-2678
28+
struct ContractType {
29+
string contractName; // Name from source code (e.g., "Wallet")
30+
bytes abi; // JSON ABI as bytes
31+
bytes bytecode; // Unlinked deployment bytecode
32+
bytes runtimeBytecode; // Unlinked runtime bytecode
33+
string sourceId; // Reference to source file
34+
uint256 compilerIndex; // Index in compilers array
35+
}
36+
37+
/// @notice Contract Instance Object from EIP-2678
38+
struct ContractInstance {
39+
string contractType; // Reference to Contract Type (e.g., "Wallet" or "owned:Owned")
40+
address instanceAddress; // Deployed contract address
41+
bytes32 transaction; // Deployment transaction hash
42+
uint256 blockNumber; // Deployment block number
43+
bytes runtimeBytecode; // Linked runtime bytecode
44+
}
45+
46+
/// @notice Link Reference Object from EIP-2678
47+
struct LinkReference {
48+
uint256[] offsets; // Start positions in bytecode (0-indexed)
49+
uint256 length; // Length in bytes of the reference
50+
string name; // Identifier for linking
51+
}
52+
53+
/// @notice Link Value Object from EIP-2678
54+
struct LinkValue {
55+
uint256[] offsets; // Locations where value was written
56+
string valueType; // "literal" for bytecode literals, "reference" for named references
57+
string value; // 0x-prefixed hex string or contract instance name
58+
}
59+
60+
/// @notice Deployment information per chain from EIP-2678
61+
struct DeploymentInfo {
62+
string chainURI; // BIP122 URI (e.g., blockchain://<chain_id>/block/<block_hash>)
63+
ContractInstance[] instances; // Array of deployed contract instances
64+
}
65+
66+
/// @notice Source file information from EIP-2678
67+
struct SourceInfo {
68+
string sourceId; // Unique identifier for source file
69+
string content; // Source code content or IPFS hash
70+
string license; // SPDX license (overrides package license)
71+
}
72+
}

test/structs/PackageStructs.t.sol

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import "forge-std/Test.sol";
5+
import "src/structs/PackageStructs.sol";
6+
7+
contract PackageStructsTest is Test {
8+
9+
function testPackageMeta() public {
10+
PackageStructs.PackageMeta memory meta;
11+
meta.authors = new string[](2);
12+
meta.authors[0] = "Satoshi";
13+
meta.authors[1] = "Vitalik";
14+
meta.license = "MIT";
15+
meta.description = "A safe math library";
16+
meta.keywords = new string[](2);
17+
meta.keywords[0] = "math";
18+
meta.keywords[1] = "library";
19+
meta.website = "https://example.com";
20+
21+
assertEq(meta.authors[0], "Satoshi");
22+
assertEq(meta.license, "MIT");
23+
assertEq(meta.keywords.length, 2);
24+
}
25+
26+
function testCompilerInfo() public {
27+
PackageStructs.CompilerInfo memory compiler;
28+
compiler.name = "solc";
29+
compiler.version = "0.8.0";
30+
31+
assertEq(compiler.name, "solc");
32+
assertEq(compiler.version, "0.8.0");
33+
}
34+
35+
function testContractType() public {
36+
PackageStructs.ContractType memory cType;
37+
cType.contractName = "Wallet";
38+
cType.sourceId = "contracts/Wallet.sol";
39+
cType.compilerIndex = 0;
40+
41+
assertEq(cType.contractName, "Wallet");
42+
assertEq(cType.sourceId, "contracts/Wallet.sol");
43+
}
44+
45+
function testContractInstance() public {
46+
PackageStructs.ContractInstance memory instance;
47+
instance.contractType = "Wallet";
48+
instance.instanceAddress = address(0x1234567890123456789012345678901234567890);
49+
instance.transaction = keccak256("deployment_tx");
50+
instance.blockNumber = 12345;
51+
52+
assertEq(instance.contractType, "Wallet");
53+
assertEq(instance.instanceAddress, address(0x1234567890123456789012345678901234567890));
54+
assertEq(instance.blockNumber, 12345);
55+
}
56+
57+
function testLinkReference() public {
58+
PackageStructs.LinkReference memory ref;
59+
ref.offsets = new uint256[](2);
60+
ref.offsets[0] = 10;
61+
ref.offsets[1] = 50;
62+
ref.length = 20;
63+
ref.name = "SafeMath";
64+
65+
assertEq(ref.offsets[0], 10);
66+
assertEq(ref.offsets[1], 50);
67+
assertEq(ref.length, 20);
68+
assertEq(ref.name, "SafeMath");
69+
}
70+
71+
function testLinkValue() public {
72+
PackageStructs.LinkValue memory val;
73+
val.offsets = new uint256[](1);
74+
val.offsets[0] = 100;
75+
val.valueType = "literal";
76+
val.value = "0x1234";
77+
78+
assertEq(val.offsets[0], 100);
79+
assertEq(val.valueType, "literal");
80+
assertEq(val.value, "0x1234");
81+
}
82+
83+
function testDeploymentInfo() public {
84+
PackageStructs.DeploymentInfo memory deployment;
85+
deployment.chainURI = "blockchain://1/block/0x123abc";
86+
deployment.instances = new PackageStructs.ContractInstance[](1);
87+
88+
deployment.instances[0].contractType = "Token";
89+
deployment.instances[0].instanceAddress = address(0x999);
90+
deployment.instances[0].blockNumber = 99999;
91+
92+
assertEq(deployment.chainURI, "blockchain://1/block/0x123abc");
93+
assertEq(deployment.instances.length, 1);
94+
assertEq(deployment.instances[0].contractType, "Token");
95+
}
96+
97+
function testSourceInfo() public {
98+
PackageStructs.SourceInfo memory source;
99+
source.sourceId = "contracts/Token.sol";
100+
source.content = "ipfs://Qm...";
101+
source.license = "GPL-3.0";
102+
103+
assertEq(source.sourceId, "contracts/Token.sol");
104+
assertEq(source.license, "GPL-3.0");
105+
}
106+
}

0 commit comments

Comments
 (0)