Skip to content

Commit

Permalink
Check the multisig address length
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Jun 11, 2024
1 parent 6238ad4 commit c067b42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit c067b42

Please sign in to comment.