Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the contract method indices are the same #369

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"@babel/traverse": ">=7.23.2",
"browserify-sign": ">=4.2.2",
"axios": ">=1.6.0",
"word-wrap": ">=1.2.4"
"word-wrap": ">=1.2.4",
"braces@<3.0.3": ">=3.0.3"
}
}
}
18 changes: 18 additions & 0 deletions packages/cli/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@ export class Project {
return forceRecompile || changedSources.includes(name)
}

private async checkMethodIndex(newArtifact: Compiled<Contract>) {
const artifactPath = newArtifact.sourceInfo.getArtifactPath(this.artifactsRootDir)
let oldArtifact: Contract
try {
oldArtifact = await Contract.fromArtifactFile(artifactPath, '', '')
} catch (error) {
throw new Error(`Failed to load contract artifact, error: ${error}, contract: ${newArtifact.sourceInfo.name}`)
}
newArtifact.artifact.functions.forEach((newFuncSig, index) => {
const oldFuncSig = oldArtifact.functions[`${index}`]
if (oldFuncSig.name !== newFuncSig.name) {
throw new Error(`Function ${newFuncSig.name} has invalid index ${index}`)
}
})
}

private async saveArtifactsToFile(
projectRootDir: string,
forceRecompile: boolean,
Expand All @@ -453,6 +469,8 @@ export class Project {
for (const [_, contract] of this.contracts) {
if (Project.needToUpdate(forceRecompile, changedSources, contract.sourceInfo.name)) {
await saveToFile(contract)
} else {
await this.checkMethodIndex(contract)
}
}
for (const [_, script] of this.scripts) {
Expand Down
6 changes: 6 additions & 0 deletions packages/web3/src/address/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe('address', function () {
expect(validateAddress('yya86C6UemCeLs5Ztwjcf2Mp2Kkt4mwzzRpBiG6qQ9kj')).toBeUndefined()
expect(() => validateAddress('yya86C6UemCeLs5Ztwjcf2Mp2Kkt4mwzzRpBiG6qQ9k')).toThrow('Invalid address:')
expect(() => validateAddress('asd')).toThrow('Invalid multisig address: asd')
expect(() => validateAddress('asdasdf')).toThrow('Invalid multisig address')
expect(() =>
validateAddress('2jW1n2icPtc55Cdm8TF9FjGH681cWthsaZW3gaUFekFZepJoeyY3ZbY7y5SCtAjyCjLL24c4L2Vnfv3KDdAypCddfAY1')
).toThrow('Invalid address:')
// both n and m are 0
expect(() => validateAddress('LUw')).toThrow('Invalid multisig address')
expect(() =>
validateAddress('2jVWAcAPphJ8ueZNG1BPwbfPFjjbvorprceuqzgmJQ1ZRyELRpWgARvdB3T9trqpiJs7f4GkudPt6rQLnGbQYqq2NCi')
).toThrow('Invalid multisig address, n: 2, m: 3')
Expand Down
8 changes: 6 additions & 2 deletions packages/web3/src/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MultiSig, lockupScriptCodec } from '../codec/lockup-script-codec'
import { compactSignedIntCodec } from '../codec'

const ec = new EC('secp256k1')
const PublicKeyHashSize = 32

export enum AddressType {
P2PKH = 0x00,
Expand Down Expand Up @@ -59,10 +60,13 @@ function decodeAndValidateAddress(address: string): Uint8Array {
}
const n = multisig.publicKeyHashes.value.length
const m = compactSignedIntCodec.toI32(multisig.m)
if (n < m) {
if (n < m || m <= 0) {
throw new Error(`Invalid multisig address, n: ${n}, m: ${m}`)
}
return decoded
const encodedNSize = compactSignedIntCodec.encodeI32(n).length
const encodedMSize = multisig.m.rest.length + 1
const size = encodedNSize + PublicKeyHashSize * n + encodedMSize + 1 // 1 for the P2MPKH prefix
if (decoded.length === size) return decoded
} else if (addressType === AddressType.P2PKH || addressType === AddressType.P2SH || addressType === AddressType.P2C) {
// [type, ...hash]
if (decoded.length === 33) return decoded
Expand Down
15 changes: 8 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading