Skip to content

Commit

Permalink
feat: Support load env from env file. (#3226)
Browse files Browse the repository at this point in the history
If we have set the env that includes scripts info, use the env value to replace the lumos config.
  • Loading branch information
yanguoyu authored Aug 14, 2024
1 parent 3c34ab3 commit e9eab5e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 17 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ $ yarn start:ui
$ yarn start:wallet
```

If you want to develop with devnet, replace the testnet script with the dev script in the `packages/neuron-wallet/.env` file.

Here is a script env example, it is constructed by network、script name and filed name.

```
TESTNET_SUDT_DEP_TXHASH=0xe12877ebd2c3c364dc46c5c992bcfaf4fee33fa13eebdf82c591fc9825aab769
TESTNET_SUDT_DEP_INDEX=0
TESTNET_SUDT_DEP_TYPE=code
TESTNET_SUDT_SCRIPT_CODEHASH=0xc5e5dcf215925f7ef4dfaf5f4b4f105bc321c02776d6e7d52a1db3fcd9d011a4
TESTNET_SUDT_SCRIPT_HASHTYPE=type
```

### Test

```shell
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/models/asset-account-info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bytes, struct, createFixedBytesCodec } from '@ckb-lumos/lumos/codec'
import { predefined } from '@ckb-lumos/config-manager'
import CellDep, { DepType } from './chain/cell-dep'
import Script, { ScriptHashType } from './chain/script'
import OutPoint from './chain/out-point'
Expand All @@ -9,6 +8,7 @@ import SystemScriptInfo from './system-script-info'
import { Address } from './address'
import { UDTType } from '../utils/const'
import { predefinedSporeConfigs, SporeConfig, SporeScript } from '@spore-sdk/core'
import { AGGRON4, LINA } from '../utils/systemScripts'

const createFixedHexBytesCodec = (byteLength: number) =>
createFixedBytesCodec({ byteLength, pack: bytes.bytify, unpack: bytes.hexify })
Expand Down Expand Up @@ -48,7 +48,7 @@ export default class AssetAccountInfo {

constructor(genesisBlockHash: string = NetworksService.getInstance().getCurrent().genesisHash) {
const isMainnet = genesisBlockHash === AssetAccountInfo.MAINNET_GENESIS_BLOCK_HASH
const { XUDT, SUDT, ANYONE_CAN_PAY } = isMainnet ? predefined.LINA.SCRIPTS : predefined.AGGRON4.SCRIPTS
const { XUDT, SUDT, ANYONE_CAN_PAY } = isMainnet ? LINA.SCRIPTS : AGGRON4.SCRIPTS
this.xudt = {
cellDep: new CellDep(new OutPoint(XUDT.TX_HASH, XUDT.INDEX), XUDT.DEP_TYPE as DepType),
codeHash: XUDT.CODE_HASH,
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/utils/scriptAndAddress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Script, hd } from '@ckb-lumos/lumos'
import { predefined } from '@ckb-lumos/lumos/config'
import { encodeToAddress, parseAddress } from '@ckb-lumos/lumos/helpers'
import { systemScripts } from './systemScripts'
import { AGGRON4, LINA } from '../utils/systemScripts'

export enum DefaultAddressNumber {
Change = 10,
Expand All @@ -22,7 +22,7 @@ export const publicKeyToAddress = (publicKey: string, isMainnet = false) => {
}

export const scriptToAddress = (script: CKBComponents.Script, isMainnet = true): string => {
const lumosConfig = !isMainnet ? predefined.AGGRON4 : predefined.LINA
const lumosConfig = !isMainnet ? AGGRON4 : LINA
return encodeToAddress(
// omit keys other than codeHash, args and hashType
{
Expand All @@ -39,6 +39,6 @@ export const addressToScript = (address: string): Script => {
if (prefix !== 'ckt' && prefix !== 'ckb') {
throw new Error('Invalid address prefix')
}
const lumosConfig = prefix === 'ckt' ? predefined.AGGRON4 : predefined.LINA
const lumosConfig = prefix === 'ckt' ? AGGRON4 : LINA
return parseAddress(address, { config: lumosConfig })
}
46 changes: 45 additions & 1 deletion packages/neuron-wallet/src/utils/systemScripts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { predefined } from '@ckb-lumos/lumos/config'
import { predefined, createConfig, type ScriptConfig } from '@ckb-lumos/config-manager'

const systemScriptsMainnet = predefined.LINA.SCRIPTS
const systemScriptsTestnet = predefined.AGGRON4.SCRIPTS
Expand All @@ -25,3 +25,47 @@ export const systemScripts = {
HASH_TYPE: systemScriptsTestnet.ANYONE_CAN_PAY.HASH_TYPE,
},
}

function getPredefinedFromEnv(
isMainnet: boolean,
envScriptName: 'SUDT' | 'ACP' | 'XUDT',
scriptConfigKey: keyof typeof predefined.LINA.SCRIPTS
): Partial<typeof predefined.LINA.SCRIPTS> | undefined {
const prefix = `${isMainnet ? 'MAINNET_' : 'TESTNET_'}${envScriptName}_`
const CODE_HASH = process.env[`${prefix}SCRIPT_CODEHASH`]
const HASH_TYPE = process.env[`${prefix}SCRIPT_HASHTYPE`] as ScriptConfig['HASH_TYPE']
const TX_HASH = process.env[`${prefix}DEP_TXHASH`]
const INDEX = process.env[`${prefix}DEP_INDEX`]
const DEP_TYPE = process.env[`${prefix}DEP_TYPE`] as ScriptConfig['DEP_TYPE']
if (CODE_HASH && HASH_TYPE && TX_HASH && INDEX && DEP_TYPE) {
return {
[scriptConfigKey]: {
CODE_HASH,
HASH_TYPE,
TX_HASH,
INDEX,
DEP_TYPE,
},
}
}
}

export const LINA = createConfig({
PREFIX: predefined.LINA.PREFIX,
SCRIPTS: {
...predefined.LINA.SCRIPTS,
...getPredefinedFromEnv(true, 'SUDT', 'SUDT'),
...getPredefinedFromEnv(true, 'XUDT', 'XUDT'),
...getPredefinedFromEnv(true, 'ACP', 'ANYONE_CAN_PAY'),
},
})

export const AGGRON4 = createConfig({
PREFIX: predefined.AGGRON4.PREFIX,
SCRIPTS: {
...predefined.AGGRON4.SCRIPTS,
...getPredefinedFromEnv(false, 'SUDT', 'SUDT'),
...getPredefinedFromEnv(false, 'XUDT', 'XUDT'),
...getPredefinedFromEnv(false, 'ACP', 'ANYONE_CAN_PAY'),
},
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { predefined } from '@ckb-lumos/config-manager'
import { AGGRON4 } from '../../src/utils/systemScripts'
import AssetAccountInfo from '../../src/models/asset-account-info'
import CellDep, { DepType } from '../../src/models/chain/cell-dep'
import OutPoint from '../../src/models/chain/out-point'
Expand All @@ -8,7 +8,7 @@ import AddressMeta from '../../src/database/address/meta'
const { AddressType } = hd

describe('AssetAccountInfo', () => {
const { SUDT, ANYONE_CAN_PAY } = predefined.AGGRON4.SCRIPTS
const { SUDT, ANYONE_CAN_PAY } = AGGRON4.SCRIPTS
const testnetSudtInfo = {
cellDep: new CellDep(new OutPoint(SUDT.TX_HASH, SUDT.INDEX), SUDT.DEP_TYPE as DepType),
codeHash: SUDT.CODE_HASH,
Expand Down
26 changes: 17 additions & 9 deletions packages/neuron-wallet/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import '../src/locales/i18n'
import { LedgerHID, LedgerCkbApp } from './mock/hardware'
import { systemScripts } from '../src/utils/systemScripts'

jest.mock('levelup', () => {
return () => ({
Expand All @@ -17,14 +16,18 @@ jest.mock('dotenv', () => ({
process.env.MAINNET_SUDT_DEP_TXHASH = '0x0000000000000000000000000000000000000000000000000000000000000000'
process.env.MAINNET_SUDT_DEP_INDEX = '0'
process.env.MAINNET_SUDT_DEP_TYPE = 'code'
process.env.MAINNET_SUDT_SCRIPT_CODEHASH = '0x0000000000000000000000000000000000000000000000000000000000000000'
process.env.MAINNET_SUDT_SCRIPT_HASHTYPE = 'data'
process.env.MAINNET_XUDT_DEP_TXHASH = '0x0000000000000000000000000000000000000000000000000000000000000001'
process.env.MAINNET_XUDT_DEP_INDEX = '0'
process.env.MAINNET_XUDT_DEP_TYPE = 'code'
process.env.MAINNET_XUDT_SCRIPT_CODEHASH = '0x0000000000000000000000000000000000000000000000000000000000000001'
process.env.MAINNET_XUDT_SCRIPT_HASHTYPE = 'data'

process.env.MAINNET_ACP_DEP_TXHASH = '0x0000000000000000000000000000000000000000000000000000000000000000'
process.env.MAINNET_ACP_DEP_INDEX = '0'
process.env.MAINNET_ACP_DEP_TYPE = 'code'
process.env.MAINNET_ACP_SCRIPT_CODEHASH = systemScripts.ANYONE_CAN_PAY_MAINNET.CODE_HASH
process.env.MAINNET_ACP_SCRIPT_HASHTYPE = systemScripts.ANYONE_CAN_PAY_MAINNET.HASH_TYPE
process.env.MAINNET_ACP_SCRIPT_CODEHASH = '0xd369597ff47f29fbc0d47d2e3775370d1250b85140c670e4718af712983a2354'
process.env.MAINNET_ACP_SCRIPT_HASHTYPE = 'type'

process.env.LEGACY_MAINNET_ACP_DEP_TXHASH = '0x0000000000000000000000000000000000000000000000000000000000000001'
process.env.LEGACY_MAINNET_ACP_DEP_INDEX = '0'
Expand Down Expand Up @@ -59,12 +62,17 @@ jest.mock('dotenv', () => ({
process.env.TESTNET_SUDT_DEP_TYPE = 'code'
process.env.TESTNET_SUDT_SCRIPT_CODEHASH = '0x48dbf59b4c7ee1547238021b4869bceedf4eea6b43772e5d66ef8865b6ae7212'
process.env.TESTNET_SUDT_SCRIPT_HASHTYPE = 'data'
process.env.TESTNET_XUDT_DEP_TXHASH = '0x0000000000000000000000000000000000000000000000000000000000000011'
process.env.TESTNET_XUDT_DEP_INDEX = '0'
process.env.TESTNET_XUDT_DEP_TYPE = 'code'
process.env.TESTNET_XUDT_SCRIPT_CODEHASH = '0x0000000000000000000000000000000000000000000000000000000000000011'
process.env.TESTNET_XUDT_SCRIPT_HASHTYPE = 'data'

process.env.TESTNET_ACP_DEP_TXHASH = '0x4f32b3e39bd1b6350d326fdfafdfe05e5221865c3098ae323096f0bfc69e0a8c'
process.env.TESTNET_ACP_DEP_INDEX = '0'
process.env.TESTNET_ACP_DEP_TYPE = 'depGroup'
process.env.TESTNET_ACP_SCRIPT_CODEHASH = systemScripts.ANYONE_CAN_PAY_TESTNET.CODE_HASH
process.env.TESTNET_ACP_SCRIPT_HASHTYPE = systemScripts.ANYONE_CAN_PAY_TESTNET.HASH_TYPE
process.env.TESTNET_ACP_SCRIPT_CODEHASH = '0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356'
process.env.TESTNET_ACP_SCRIPT_HASHTYPE = 'type'

process.env.LEGACY_TESTNET_ACP_DEP_TXHASH = '0x0000000000000000000000000000000000000000000000000000000000000001'
process.env.LEGACY_TESTNET_ACP_DEP_INDEX = '0'
Expand All @@ -85,9 +93,9 @@ jest.mock('dotenv', () => ({
process.env.TESTNET_CHEQUE_SCRIPT_CODEHASH = '0x0000000000000000000000000000000000000000000000000000000000000003'
process.env.TESTNET_CHEQUE_SCRIPT_HASHTYPE = 'type'

process.env.SECP256K1_CODE_HASH = systemScripts.SECP256K1_BLAKE160.CODE_HASH
process.env.DAO_CODE_HASH = systemScripts.DAO.CODE_HASH
process.env.MULTISIG_CODE_HASH = systemScripts.SECP256K1_BLAKE160_MULTISIG.CODE_HASH
process.env.SECP256K1_CODE_HASH = '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8'
process.env.DAO_CODE_HASH = '0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e'
process.env.MULTISIG_CODE_HASH = '0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8'

process.env.TESTNET_NFT_SCRIPT_CODEHASH = '0xb1837b5ad01a88558731953062d1f5cb547adf89ece01e8934a9f0aeed2d959f'
process.env.TESTNET_NFT_SCRIPT_HASH_TYPE = 'type'
Expand Down
15 changes: 15 additions & 0 deletions packages/neuron-wallet/tests/utils/systemScripts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Add import env to load env
import env from '../../src/env'
import { AGGRON4, LINA } from '../../src/utils/systemScripts'

describe('Test env is loaded', () => {
it('test xudt', () => {
console.info(env.fileBasePath)
expect(LINA.SCRIPTS.XUDT.CODE_HASH).toBe('0x0000000000000000000000000000000000000000000000000000000000000001')
expect(AGGRON4.SCRIPTS.XUDT.CODE_HASH).toBe('0x0000000000000000000000000000000000000000000000000000000000000011')
})
it('test sudt', () => {
expect(LINA.SCRIPTS.SUDT.CODE_HASH).toBe('0x48dbf59b4c7ee1547238021b4869bceedf4eea6b43772e5d66ef8865b6ae7212')
expect(AGGRON4.SCRIPTS.SUDT.CODE_HASH).toBe('0x48dbf59b4c7ee1547238021b4869bceedf4eea6b43772e5d66ef8865b6ae7212')
})
})

2 comments on commit e9eab5e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 10383832629

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 10383832759

Please sign in to comment.