-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,637 additions
and
611 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,25 @@ | ||
import type { Struct } from '@polkadot/types-codec'; | ||
import type { Balance } from '@polkadot/types/interfaces/runtime'; | ||
import { ApiPromise, HttpProvider } from '@polkadot/api'; | ||
|
||
const ROCOCO_RPC_URL = 'https://rococo-rpc.polkadot.io/'; | ||
|
||
export type AccountData = { | ||
readonly free: Balance; | ||
} & Struct; | ||
|
||
export async function initApi(): Promise<ApiPromise> { | ||
let provider: HttpProvider; | ||
|
||
try { | ||
provider = new HttpProvider(ROCOCO_RPC_URL); | ||
} catch (error) { | ||
console.error('Error on provider creation', error); | ||
throw error; | ||
} | ||
|
||
const apiPromise = new ApiPromise({ provider }); | ||
await apiPromise.isReady; | ||
|
||
return apiPromise; | ||
} |
This file contains 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,24 @@ | ||
import { getBIP44AddressKeyDeriver } from '@metamask/key-tree'; | ||
import { Keyring } from '@polkadot/api'; | ||
|
||
async function getKeyPair() { | ||
const polkadotTypeNode = await snap.request({ | ||
method: 'snap_getBip44Entropy', | ||
params: { | ||
coinType: 354, | ||
}, | ||
}); | ||
|
||
const deriveAddressKey = await getBIP44AddressKeyDeriver(polkadotTypeNode); | ||
const addressKey0 = await deriveAddressKey(0); | ||
|
||
const keyring = new Keyring({ ss58Format: 42 }); | ||
|
||
if (!addressKey0.privateKeyBytes) { | ||
throw new Error('No private key found'); | ||
} | ||
|
||
return keyring.addFromSeed(addressKey0.privateKeyBytes); | ||
} | ||
|
||
export default getKeyPair; |
This file contains 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,24 +1,9 @@ | ||
import { getBIP44AddressKeyDeriver } from '@metamask/key-tree'; | ||
import { Keyring } from '@polkadot/api'; | ||
import getKeyPair from '../polkadot/getKeyPair'; | ||
|
||
async function getAddress() { | ||
const polkadotTypeNode = await snap.request({ | ||
method: 'snap_getBip44Entropy', | ||
params: { | ||
coinType: 434, | ||
}, | ||
}); | ||
const keypair = await getKeyPair(); | ||
|
||
const deriveAddressKey = await getBIP44AddressKeyDeriver(polkadotTypeNode); | ||
const addressKey0 = await deriveAddressKey(0); | ||
|
||
const keyring = new Keyring({ ss58Format: 2 }); | ||
|
||
if (!addressKey0.privateKeyBytes) { | ||
throw new Error('No private key found'); | ||
} | ||
|
||
return keyring.addFromSeed(addressKey0.privateKeyBytes).address; | ||
return keypair.address; | ||
} | ||
|
||
export default getAddress; |
This file contains 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 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,31 @@ | ||
import { heading, panel, text } from '@metamask/snaps-ui'; | ||
import getKeyPair from '../polkadot/getKeyPair'; | ||
import { initApi } from '../polkadot/api'; | ||
|
||
export async function transfer( | ||
to: string, | ||
amount: string, | ||
): Promise<{ signature: string } | void> { | ||
const isConfirmed = await snap.request({ | ||
method: 'snap_dialog', | ||
params: { | ||
type: 'confirmation', | ||
content: panel([ | ||
heading('Confirm Transfer'), | ||
text(`Are you sure you want to transfer ${amount} to ${to}?`), | ||
]), | ||
}, | ||
}); | ||
|
||
if (isConfirmed) { | ||
const user = await getKeyPair(); | ||
const api = await initApi(); | ||
|
||
const transferTransaction = await api.tx.balances | ||
.transferKeepAlive(to, amount) | ||
.signAndSend(user); | ||
|
||
console.log('paymentInfo', transferTransaction); | ||
console.log(JSON.stringify(transferTransaction)); | ||
} | ||
} |
This file contains 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 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.