-
Notifications
You must be signed in to change notification settings - Fork 49
feat: include support for ink v6 contracts #571
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
Open
AlexD10S
wants to merge
28
commits into
master
Choose a base branch
from
feat/support-ink-v6
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8e7f600
feat(instantiate): spike, instantiate on revive. Doesn't import deplo…
peterwht 2a26924
wip: add contract and calling contracts works. Modifies @polkadot/api…
peterwht b49629e
feat: contract address calculation
peterwht 426b6fd
feat: create1 address calc works and autorecognize contract
peterwht 0a210bf
fix: build errors and add warning banner
peterwht a82cf94
build: script to install forked polkadot-js
peterwht 93857f7
build: add prebuilt api-contract
peterwht ea11eec
build: update yarn.lock
peterwht 89573a8
westend asset hub only
peterwht 34b1986
chore: include pop network
AlexD10S f4d47d9
fix: update yarn
AlexD10S 7f96476
feat: version context
AlexD10S 0cff94e
chore: api-revive and api-contract in the same repo
AlexD10S 66473ed
chore: ink-node or substrate-contracts-node depending on version
AlexD10S 8ae5daa
chore: update api-contracts
AlexD10S 1224753
chore: display endpoints depending on the version
AlexD10S 72fdb63
chore: update api-contract
AlexD10S 7a5b5c9
chore: getVersion
AlexD10S e69736f
feat: isValidAddress for inkv6
AlexD10S 8196feb
chore: remove polkadot-js packages
AlexD10S 5a71edf
refactor: remove dotAddress
AlexD10S 24c0b77
chore: alpha version only for v6
AlexD10S 7e171db
chore: interact and steps
AlexD10S 208340f
refactor: useNewContract
AlexD10S dce7d7b
test: address tests
AlexD10S f1f0fa9
fix: checkOnChainCode for v6
AlexD10S 980abef
fix: calculate account when salt is provided
AlexD10S 0cb7d75
refactor: clean up
AlexD10S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
compressionLevel: mixed | ||
|
||
enableGlobalCache: false | ||
|
||
nodeLinker: node-modules | ||
yarnPath: .yarn/releases/yarn-3.1.1.cjs | ||
|
||
yarnPath: .yarn/releases/yarn-4.6.0.cjs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2022-2024 use-ink/contracts-ui authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
import { getAddress } from 'ethers'; | ||
import { decodeAddress } from '@polkadot/keyring'; | ||
import { create1, create2, toEthAddress } from './address'; | ||
|
||
// Similar to pallet_revive tests: https://github.com/paritytech/polkadot-sdk/blob/65ade498b63bf2216d1c444f28c1b48085417f13/substrate/frame/revive/src/address.rs#L257 | ||
describe('address utilities', () => { | ||
const deployer = '0x' + '01'.repeat(20); | ||
const code = Uint8Array.from([0x60, 0x00, 0x60, 0x00, 0x55, 0x60, 0x01, 0x60, 0x00]); | ||
const inputData = Uint8Array.from([0x55]); | ||
const salt = '0x1234567890123456789012345678901234567890123456789012345678901234'; | ||
|
||
it('should compute correct address with create1', () => { | ||
const address = create1(deployer, 1); | ||
expect(getAddress(address)).toBe(getAddress('0xc851da37e4e8d3a20d8d56be2963934b4ad71c3b')); | ||
}); | ||
|
||
it('should compute correct address with create2', () => { | ||
const address = create2(deployer, code, inputData, salt); | ||
expect(getAddress(address)).toBe(getAddress('0x7f31e795e5836a19a8f919ab5a9de9a197ecd2b6')); | ||
}); | ||
|
||
it('should convert Substrate account ID to Ethereum address', () => { | ||
const accountId = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; | ||
const ethAddress = toEthAddress(decodeAddress(accountId)); | ||
|
||
expect(ethAddress.startsWith('0x')).toBe(true); | ||
expect(getAddress(ethAddress)).toBe(getAddress('0x9621dde636de098b43efb0fa9b61facfe328f99d')); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2022-2024 use-ink/contracts-ui authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { BigNumberish, ethers } from 'ethers'; | ||
import { hexToU8a, stringToU8a, u8aToHex } from '@polkadot/util'; | ||
import { keccak256 } from 'ethers'; | ||
|
||
/** | ||
* TypeScript equivalent of H160 (20-byte Ethereum address) | ||
*/ | ||
type Address = string; | ||
|
||
/** | ||
* Determine the address of a contract using CREATE semantics. | ||
* @param deployer The address of the deployer | ||
* @param nonce The nonce value | ||
* @returns The contract address | ||
*/ | ||
export function create1(deployer: string, nonce: number): Address { | ||
// Convert deployer to bytes (remove 0x prefix if present) | ||
const deployerBytes = ethers.hexlify(deployer); | ||
ethers.toBeHex(nonce as BigNumberish); | ||
// Convert nonce to hex (minimal encoding) | ||
const nonceBytes = ethers.toBeHex(nonce as BigNumberish); | ||
|
||
// RLP encode [deployer, nonce] | ||
const encodedData = ethers.encodeRlp([deployerBytes, nonceBytes]); | ||
|
||
// Calculate keccak256 hash of the RLP encoded data | ||
const hash = ethers.keccak256(encodedData); | ||
|
||
// Take the last 20 bytes (40 hex chars + 0x prefix) | ||
return ethers.getAddress('0x' + hash.substring(26)); | ||
} | ||
|
||
/** | ||
* Determine the address of a contract using CREATE2 semantics. | ||
* @param deployer The address of the deployer | ||
* @param code The contract code (WASM or EVM bytecode) | ||
* @param inputData The constructor arguments or init input | ||
* @param salt A 32-byte salt value (as hex string) | ||
* @returns The deterministic contract address | ||
*/ | ||
export function create2( | ||
deployer: string, | ||
code: Uint8Array, | ||
inputData: Uint8Array, | ||
salt: string, | ||
): Address { | ||
const initCode = new Uint8Array([...code, ...inputData]); | ||
const initCodeHash = hexToU8a(keccak256(initCode)); | ||
|
||
const parts = new Uint8Array(1 + 20 + 32 + 32); // 0xff + deployer + salt + initCodeHash | ||
parts[0] = 0xff; | ||
parts.set(hexToU8a(deployer), 1); | ||
parts.set(hexToU8a(salt), 21); | ||
parts.set(initCodeHash, 53); | ||
|
||
const hash = keccak256(parts); | ||
|
||
// Return last 20 bytes as 0x-prefixed hex string | ||
return ethers.getAddress('0x' + hash.substring(26)); | ||
} | ||
|
||
/** | ||
* Converts an account ID to an Ethereum address (H160) | ||
* @param accountId The account ID bytes | ||
* @returns The Ethereum address | ||
*/ | ||
export function toEthAddress(accountId: Uint8Array | string): string { | ||
// Convert string input to Uint8Array if needed | ||
const accountBytes = typeof accountId === 'string' ? stringToU8a(accountId) : accountId; | ||
|
||
// Create a 32-byte buffer and copy account bytes into it | ||
const accountBuffer = new Uint8Array(32); | ||
accountBuffer.set(accountBytes.slice(0, 32)); | ||
|
||
if (isEthDerived(accountBytes)) { | ||
// This was originally an eth address | ||
// We just strip the 0xEE suffix to get the original address | ||
return '0x' + Buffer.from(accountBuffer.slice(0, 20)).toString('hex'); | ||
} else { | ||
// This is an (ed|sr)25519 derived address | ||
// Avoid truncating the public key by hashing it first | ||
const accountHash = ethers.keccak256(accountBuffer); | ||
return ethers.getAddress('0x' + accountHash.slice(2 + 24, 2 + 24 + 40)); // Skip '0x' prefix, then skip 12 bytes, take 20 bytes | ||
} | ||
} | ||
|
||
export function fromEthAddress(ethAddress: string): string { | ||
// Remove '0x' prefix if it exists | ||
const cleanAddress = ethAddress.startsWith('0x') ? ethAddress.slice(2) : ethAddress; | ||
|
||
// Convert the hex string to bytes | ||
const addressBytes = Buffer.from(cleanAddress, 'hex'); | ||
|
||
// Check if the address is the expected length (20 bytes) | ||
if (addressBytes.length !== 20) { | ||
throw new Error('Invalid Ethereum address: must be 20 bytes'); | ||
} | ||
|
||
// Create a 32-byte buffer | ||
const result = new Uint8Array(32).fill(0xee); | ||
|
||
// Set the first 20 bytes to the Ethereum address | ||
result.set(addressBytes, 0); | ||
|
||
return u8aToHex(result); | ||
} | ||
|
||
/** | ||
* Determines if an account ID is derived from an Ethereum address | ||
* @param accountId The account ID bytes | ||
* @returns True if the account is derived from an Ethereum address | ||
*/ | ||
export function isEthDerived(accountId: Uint8Array): boolean { | ||
if (accountId.length >= 32) { | ||
return accountId[20] === 0xee && accountId[21] === 0xee; | ||
} | ||
return false; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To update once this polkadot-js/api#6158 gets merged and released