Skip to content

Commit 63ac406

Browse files
committed
feat(examples): add example scripts for address handling and Ethereum derivation
1 parent ac7a3f8 commit 63ac406

File tree

5 files changed

+101
-20
lines changed

5 files changed

+101
-20
lines changed

examples/address.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { address, mockWallets } from '@autonomys/auto-utils'
2+
3+
export const addressFunction = async () => {
4+
const [alice, bob] = await mockWallets()
5+
// Alice's Addresses
6+
console.log('\x1b[33m%s\x1b[0m', 'Alice Raw Address:', alice.accounts[0].address)
7+
const aliceAddress = address(alice.accounts[0].address)
8+
console.log('\x1b[32m%s\x1b[0m', 'Alice Autonomys (6094) Address:', aliceAddress, '\n')
9+
10+
// Bob's Addresses
11+
console.log('\x1b[33m%s\x1b[0m', 'Bob Raw Address:', bob.accounts[0].address)
12+
const bobAddress = address(bob.accounts[0].address)
13+
console.log('\x1b[32m%s\x1b[0m', 'Bob Autonomys (6094) Address:', bobAddress, '\n')
14+
}
15+
16+
addressFunction()
17+
.then(() => {
18+
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
19+
process.exit(0)
20+
})
21+
.catch((e) => {
22+
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
23+
process.exit(1)
24+
})

examples/eth-derivation-demo.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { setupWallet } from '@autonomys/auto-utils'
2+
import { HDNodeWallet, Mnemonic, Wallet } from 'ethers'
3+
4+
const MNEMONIC = 'test test test test test test test test test test test junk'
5+
6+
const main = async (): Promise<void> => {
7+
const master = setupWallet({ mnemonic: MNEMONIC, type: 'ethereum' })
8+
9+
const bip44Path = "m/44'/60'/0'/0/0"
10+
const bip44 = setupWallet({
11+
mnemonic: MNEMONIC,
12+
type: 'ethereum',
13+
derivationPath: bip44Path,
14+
})
15+
16+
const ethersMnemonic = Mnemonic.fromPhrase(MNEMONIC)
17+
const ethersWallet = Wallet.fromPhrase(MNEMONIC)
18+
const ethersMaster = HDNodeWallet.fromMnemonic(ethersMnemonic, 'm')
19+
const ethersBip44 = HDNodeWallet.fromMnemonic(ethersMnemonic, bip44Path)
20+
21+
console.log('Mnemonic:', MNEMONIC)
22+
console.log('Master (m) address:', master.address)
23+
console.log(`BIP44 (${bip44Path}) address:`, bip44.address)
24+
console.log('Ethers Wallet address:', ethersWallet.address)
25+
console.log('Ethers Master (m) address:', ethersMaster.address)
26+
console.log(`Ethers BIP44 (${bip44Path}) address:`, ethersBip44.address)
27+
console.log('Match?', master.address.toLowerCase() === bip44.address.toLowerCase())
28+
console.log(
29+
'Auto (m) === Ethers (m)?',
30+
master.address.toLowerCase() === ethersMaster.address.toLowerCase(),
31+
)
32+
console.log(
33+
`Auto BIP44 === Ethers BIP44?`,
34+
bip44.address.toLowerCase() === ethersBip44.address.toLowerCase(),
35+
)
36+
console.log(
37+
'Note: Current SDK behavior derives Ethereum from master key (m) unless a path is provided.',
38+
)
39+
}
40+
41+
main().catch((error) => {
42+
console.error(error)
43+
process.exit(1)
44+
})

examples/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "examples",
3+
"version": "0.1",
4+
"private": true,
5+
"license": "MIT",
6+
"packageManager": "[email protected]",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/autonomys/auto-sdk"
10+
},
11+
"author": {
12+
"name": "Autonomys",
13+
"url": "https://www.autonomys.net"
14+
},
15+
"scripts": {
16+
"eth-derivation-demo": "npx ts-node ./eth-derivation-demo.ts",
17+
"address": "npx ts-node ./address.ts"
18+
},
19+
"dependencies": {
20+
"@autonomys/auto-utils": "workspace:*",
21+
"ethers": "^6.13.5"
22+
}
23+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"workspaces": [
77
"packages/**/*",
8-
"examples/*",
8+
"examples",
99
"integration-tests"
1010
],
1111
"scripts": {

yarn.lock

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10339,13 +10339,6 @@ __metadata:
1033910339
languageName: node
1034010340
linkType: hard
1034110341

10342-
"commander@npm:^12.1.0":
10343-
version: 12.1.0
10344-
resolution: "commander@npm:12.1.0"
10345-
checksum: 10c0/6e1996680c083b3b897bfc1cfe1c58dfbcd9842fd43e1aaf8a795fbc237f65efcc860a3ef457b318e73f29a4f4a28f6403c3d653d021d960e4632dd45bde54a9
10346-
languageName: node
10347-
linkType: hard
10348-
1034910342
"commander@npm:^2.20.0":
1035010343
version: 2.20.3
1035110344
resolution: "commander@npm:2.20.3"
@@ -12547,6 +12540,15 @@ __metadata:
1254712540
languageName: node
1254812541
linkType: hard
1254912542

12543+
"examples@workspace:examples":
12544+
version: 0.0.0-use.local
12545+
resolution: "examples@workspace:examples"
12546+
dependencies:
12547+
"@autonomys/auto-utils": "workspace:*"
12548+
ethers: "npm:^6.13.5"
12549+
languageName: unknown
12550+
linkType: soft
12551+
1255012552
"execa@npm:5.0.0":
1255112553
version: 5.0.0
1255212554
resolution: "execa@npm:5.0.0"
@@ -17117,18 +17119,6 @@ __metadata:
1711717119
languageName: node
1711817120
linkType: hard
1711917121

17120-
"node@workspace:examples/node":
17121-
version: 0.0.0-use.local
17122-
resolution: "node@workspace:examples/node"
17123-
dependencies:
17124-
"@autonomys/auto-consensus": "workspace:*"
17125-
"@autonomys/auto-utils": "workspace:*"
17126-
commander: "npm:^12.1.0"
17127-
dotenv: "npm:^16.4.5"
17128-
ethers: "npm:^6.13.5"
17129-
languageName: unknown
17130-
linkType: soft
17131-
1713217122
"nopt@npm:^7.0.0, nopt@npm:^7.2.1":
1713317123
version: 7.2.1
1713417124
resolution: "nopt@npm:7.2.1"

0 commit comments

Comments
 (0)