Skip to content

Commit

Permalink
Add wallet contract version to keys
Browse files Browse the repository at this point in the history
Necessary for moving seeds between wallets
  • Loading branch information
peachbits committed Oct 31, 2024
1 parent 257d8f8 commit a0dc1ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/ton/TonTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
mergeDeeply,
shuffleArray
} from '../common/utils'
import { asTonPrivateKeys, TonInfoPayload, TonNetworkInfo } from './tonTypes'
import {
asTonPrivateKeys,
TonInfoPayload,
TonNetworkInfo,
wasTonPrivateKeys
} from './tonTypes'

export class TonTools implements EdgeCurrencyTools {
io: EdgeIo
Expand Down Expand Up @@ -100,7 +105,7 @@ export class TonTools implements EdgeCurrencyTools {
): Promise<string> {
const { pluginId } = this.currencyInfo
const keys = asTonPrivateKeys(pluginId)(privateWalletInfo.keys)
return keys.mnemonic
return `${keys.mnemonic}\n\nVersion: ${keys.walletContract}`
}

async getDisplayPublicKey(publicWalletInfo: EdgeWalletInfo): Promise<string> {
Expand All @@ -112,7 +117,10 @@ export class TonTools implements EdgeCurrencyTools {
const isValid = validateMnemonic(input)
if (!isValid) throw new Error('Invalid mnemonic')

return { [this.networkInfo.pluginMnemonicKeyName]: input }
return wasTonPrivateKeys(this.currencyInfo.pluginId)({
mnemonic: input,
walletContract: this.networkInfo.defaultWalletContract
})
}

async createPrivateKey(walletType: string): Promise<JsonObject> {
Expand Down
1 change: 1 addition & 0 deletions src/ton/tonInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { TonTools } from './TonTools'
import { asTonInfoPayload, TonInfoPayload, TonNetworkInfo } from './tonTypes'

const networkInfo: TonNetworkInfo = {
defaultWalletContract: 'v5r1',
minimumAddressBalance: '50000000', // 0.5 TON There isn't a hardcoded minimum but the user needs to keep something left
pluginMnemonicKeyName: 'tonMnemonic',
tonCenterUrl: 'https://toncenter.com/api/v2/jsonRPC',
Expand Down
16 changes: 12 additions & 4 deletions src/ton/tonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
asObject,
asOptional,
asString,
Cleaner
Cleaner,
uncleaner
} from 'cleaners'

export interface TonNetworkInfo {
defaultWalletContract: string
minimumAddressBalance: string
pluginMnemonicKeyName: string
tonCenterUrl: string
Expand Down Expand Up @@ -37,26 +39,32 @@ export type TonWalletOtherData = ReturnType<typeof asTonWalletOtherData>

export interface TonPrivateKeys {
mnemonic: string
walletContract: string
}
export const asTonPrivateKeys = (pluginId: string): Cleaner<TonPrivateKeys> => {
const asKeys = asObject({
[`${pluginId}Mnemonic`]: asString
[`${pluginId}Mnemonic`]: asString,
walletContract: asString
})

return asCodec(
raw => {
const from = asKeys(raw)
return {
mnemonic: from[`${pluginId}Mnemonic`]
mnemonic: from[`${pluginId}Mnemonic`],
walletContract: from.walletContract
}
},
clean => {
return {
[`${pluginId}Mnemonic`]: clean.mnemonic
[`${pluginId}Mnemonic`]: clean.mnemonic,
walletContract: clean.walletContract
}
}
)
}
export const wasTonPrivateKeys = (pluginId: string): Cleaner<TonPrivateKeys> =>
uncleaner(asTonPrivateKeys(pluginId))

const asBigInt = (val: any): BigInt => {
if (typeof val !== 'bigint') {
Expand Down

0 comments on commit a0dc1ce

Please sign in to comment.