Skip to content

Commit

Permalink
Merge pull request #369 from alephium/check-method-index
Browse files Browse the repository at this point in the history
Check if the contract method indices are the same
  • Loading branch information
polarker authored Jun 11, 2024
2 parents 6238ad4 + 0eb3a81 commit 1fcc020
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
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"
}
}
}
20 changes: 20 additions & 0 deletions packages/cli/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,24 @@ 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(
`The newly compiled contract ${newArtifact.artifact.name} has different method indexes compared to the existing deployment on mainnet/testnet`
)
}
})
}

private async saveArtifactsToFile(
projectRootDir: string,
forceRecompile: boolean,
Expand All @@ -453,6 +471,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.

0 comments on commit 1fcc020

Please sign in to comment.