-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamadeusFunctions.ts
More file actions
75 lines (60 loc) · 2.11 KB
/
amadeusFunctions.ts
File metadata and controls
75 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { AmadeusSDK, generateKeypair, derivePublicKeyFromSeedBase58, fromAtomicAma, deriveSkAndSeed64FromBase58Seed } from '@amadeus-protocol/sdk'
import { bls12_381 } from "@noble/curves/bls12-381.js";
import { hexToBytes } from "@noble/curves/utils.js";
import bs58 from "bs58";
// Initialize SDK (uses default node URL if not specified)
const sdk = new AmadeusSDK({
baseUrl: 'https://nodes.amadeus.bot/api'
})
/**
* Generates a new Amadeus wallet
*/
export async function generateAmadeusWallet() {
const wallet = generateKeypair();
console.log("keypair generated", wallet)
const pubKey = wallet.publicKey;
console.log("public key", pubKey)
const privKey = wallet.privateKey;
console.log("private key", privKey)
const bal = await sdk.wallet.getBalance(pubKey);
const balance = fromAtomicAma(bal.balance.flat).toFixed(4);
console.log("wallet balance", balance)
return {
pubKey,
privKey,
balance
}
}
/**
* Gets the balance of an Amadeus wallet, default token is AMA
*/
export async function getAmadeusBalance(pubKey: string, token?: string) {
const bal = await sdk.wallet.getBalance(pubKey, token);
const balance = fromAtomicAma(bal.balance.flat).toFixed(4);
console.log("wallet balance", balance)
return balance
}
/**
* Sign a transaction on amadeus network using the derived secret key
*/
export function signTransaction(signingPayload: string, privateKeyB58: string): string {
// Derive the actual secret key scalar from the seed
const { sk } = deriveSkAndSeed64FromBase58Seed(privateKeyB58);
const blsl = bls12_381.longSignatures;
const signingHash = hexToBytes(signingPayload);
const DST = "AMADEUS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_TX_";
const msgPoint = blsl.hash(signingHash, DST);
const signature = blsl.sign(msgPoint, sk);
return bs58.encode(signature.toBytes(true));
}
/**
* Validates an Amadeus private key
*/
export function validateAmadeusPrivateKey(privKey: string) {
const pubKey = derivePublicKeyFromSeedBase58(privKey);
console.log("derived key", pubKey)
if (pubKey) {
return { success: true, pubKey };
}
return { success: false, pubKey: "" };
}