@canvas-js/chain-near
Advanced tools
Comparing version 0.4.10 to 0.4.11
@@ -1,2 +0,2 @@ | ||
export declare function createMockSolanaSigner(): { | ||
export declare function createMockNearSigner(): { | ||
isPhantom: boolean; | ||
@@ -7,3 +7,3 @@ isConnected: boolean; | ||
publicKey: { | ||
toBytes: () => Uint8Array; | ||
toBytes: () => string; | ||
}; | ||
@@ -10,0 +10,0 @@ signMessage: (message: Uint8Array) => Promise<{ |
@@ -1,5 +0,4 @@ | ||
import * as solw3 from "@solana/web3.js"; | ||
import nacl from "tweetnacl"; | ||
export function createMockSolanaSigner() { | ||
const keypair = solw3.Keypair.generate(); | ||
import { utils as nearApiUtils } from "near-api-js"; | ||
export function createMockNearSigner() { | ||
const keypair = nearApiUtils.KeyPairEd25519.fromRandom(); | ||
return { | ||
@@ -10,5 +9,5 @@ isPhantom: true, | ||
disconnect: async () => { }, | ||
publicKey: { toBytes: () => keypair.publicKey.toBytes() }, | ||
publicKey: { toBytes: () => keypair.getPublicKey().toString() }, | ||
signMessage: async (message) => { | ||
const signatureBytes = nacl.sign.detached(message, keypair.secretKey); | ||
const { signature: signatureBytes } = keypair.sign(message); | ||
return { signature: signatureBytes }; | ||
@@ -15,0 +14,0 @@ }, |
@@ -1,10 +0,6 @@ | ||
import solw3 from "@solana/web3.js"; | ||
import { utils as nearApiUtils } from "near-api-js"; | ||
import type { Action, ActionPayload, ChainImplementation, Session, SessionPayload } from "@canvas-js/interfaces"; | ||
interface SolanaWindowSigner { | ||
publicKey?: solw3.PublicKey; | ||
signMessage(message: Uint8Array): Promise<{ | ||
signature: Uint8Array; | ||
}>; | ||
interface NearWindowSigner { | ||
} | ||
export declare class SolanaChainImplementation implements ChainImplementation<SolanaWindowSigner, solw3.Keypair> { | ||
export declare class NearChainImplementation implements ChainImplementation<NearWindowSigner, nearApiUtils.KeyPairEd25519> { | ||
readonly genesisHash: string; | ||
@@ -16,12 +12,12 @@ readonly chain: string; | ||
verifySession(session: Session): Promise<void>; | ||
getSignerAddress: (signer: SolanaWindowSigner) => Promise<string>; | ||
getDelegatedSignerAddress: (wallet: solw3.Keypair) => Promise<string>; | ||
signSession(signer: SolanaWindowSigner, payload: SessionPayload): Promise<Session>; | ||
signAction(signer: SolanaWindowSigner, payload: ActionPayload): Promise<Action>; | ||
signDelegatedAction(wallet: solw3.Keypair, payload: ActionPayload): Promise<Action>; | ||
importDelegatedSigner: (secretKey: string) => solw3.Keypair; | ||
exportDelegatedSigner: (wallet: solw3.Keypair) => string; | ||
generateDelegatedSigner: () => Promise<solw3.Keypair>; | ||
getSignerAddress: (signer: NearWindowSigner) => Promise<never>; | ||
getDelegatedSignerAddress: (keypair: nearApiUtils.KeyPairEd25519) => Promise<never>; | ||
signSession(signer: NearWindowSigner, payload: SessionPayload): Promise<Session>; | ||
signAction(signer: NearWindowSigner, payload: ActionPayload): Promise<Action>; | ||
signDelegatedAction(keypair: nearApiUtils.KeyPairEd25519, payload: ActionPayload): Promise<Action>; | ||
importDelegatedSigner: (secretKey: string) => nearApiUtils.key_pair.KeyPairEd25519; | ||
exportDelegatedSigner: (keypair: nearApiUtils.KeyPairEd25519) => string; | ||
generateDelegatedSigner: () => Promise<nearApiUtils.KeyPairEd25519>; | ||
getLatestBlock(): Promise<string>; | ||
} | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import solw3 from "@solana/web3.js"; | ||
import { utils as nearApiUtils } from "near-api-js"; | ||
import nacl from "tweetnacl"; | ||
@@ -11,16 +11,15 @@ import bs58 from "bs58"; | ||
}; | ||
export class SolanaChainImplementation { | ||
export class NearChainImplementation { | ||
constructor(genesisHash = "mainnet") { | ||
this.genesisHash = genesisHash; | ||
this.getSignerAddress = async (signer) => { | ||
if (signer.publicKey === undefined) { | ||
throw new Error("Wallet not connected"); | ||
} | ||
return bs58.encode(signer.publicKey.toBytes()); | ||
throw new Error("Unimplemented"); | ||
}; | ||
this.getDelegatedSignerAddress = async (wallet) => bs58.encode(wallet.publicKey.toBytes()); | ||
this.importDelegatedSigner = (secretKey) => solw3.Keypair.fromSecretKey(bs58.decode(secretKey)); | ||
this.exportDelegatedSigner = (wallet) => bs58.encode(wallet.secretKey); | ||
this.generateDelegatedSigner = async () => solw3.Keypair.generate(); | ||
this.chain = `solana:${genesisHash.slice(0, 32)}`; | ||
this.getDelegatedSignerAddress = async (keypair) => { | ||
throw new Error("Unimplemented"); | ||
}; | ||
this.importDelegatedSigner = (secretKey) => new nearApiUtils.KeyPairEd25519(secretKey); | ||
this.exportDelegatedSigner = (keypair) => keypair.secretKey; | ||
this.generateDelegatedSigner = async () => nearApiUtils.KeyPairEd25519.fromRandom(); | ||
this.chain = `near:${genesisHash.slice(0, 32)}`; | ||
} | ||
@@ -34,47 +33,34 @@ hasProvider() { | ||
const signatureBytes = bs58.decode(action.signature); | ||
const valid = nacl.sign.detached.verify(message, signatureBytes, bs58.decode(expectedAddress)); | ||
const publicKey = nearApiUtils.PublicKey.fromString(expectedAddress); | ||
const valid = nacl.sign.detached.verify(message, signatureBytes, publicKey.data); | ||
if (!valid) { | ||
throw new Error("Invalid session signature"); | ||
throw new Error("Invalid action signature"); | ||
} | ||
} | ||
async verifySession(session) { | ||
const expectedAddress = session.payload.from; | ||
const message = getSessionSignatureData(session.payload); | ||
const signatureBytes = bs58.decode(session.signature); | ||
const valid = nacl.sign.detached.verify(message, signatureBytes, bs58.decode(expectedAddress)); | ||
const stringPayload = serializeSessionPayload(session.payload); | ||
const message = new TextEncoder().encode(stringPayload); | ||
const { signature: signatureEncoded, publicKey: publicKeyEncoded } = JSON.parse(session.signature); | ||
const publicKey = nearApiUtils.PublicKey.fromString(bs58.encode(Buffer.from(publicKeyEncoded, "base64"))); | ||
const signatureBytes = Buffer.from(signatureEncoded, "base64"); | ||
const valid = nacl.sign.detached.verify(message, signatureBytes, publicKey.data); | ||
if (!valid) { | ||
throw new Error("Invalid action signature"); | ||
throw new Error("Invalid session signature"); | ||
} | ||
} | ||
async signSession(signer, payload) { | ||
if (signer.publicKey === null || signer.publicKey === undefined) { | ||
throw new Error("Wallet not connected"); | ||
} | ||
const address = bs58.encode(signer.publicKey.toBytes()); | ||
if (address !== payload.from) { | ||
throw new Error("Signer address did not match payload.from"); | ||
} | ||
const message = getSessionSignatureData(payload); | ||
const { signature } = await signer.signMessage(message); | ||
return { type: "session", payload, signature: bs58.encode(signature) }; | ||
throw new Error("Unimplemented"); | ||
} | ||
async signAction(signer, payload) { | ||
if (signer.publicKey === null || signer.publicKey === undefined) | ||
throw new Error("Wallet not connected"); | ||
const address = bs58.encode(signer.publicKey.toBytes()); | ||
if (address !== payload.from) { | ||
throw new Error("Signer address did not match payload.from"); | ||
} | ||
const message = getActionSignatureData(payload); | ||
const { signature } = await signer.signMessage(message); | ||
return { type: "action", payload, signature: bs58.encode(signature), session: null }; | ||
throw new Error("Unimplemented"); | ||
} | ||
async signDelegatedAction(wallet, payload) { | ||
async signDelegatedAction(keypair, payload) { | ||
const message = getActionSignatureData(payload); | ||
const signature = nacl.sign.detached(message, wallet.secretKey); | ||
const { signature: signatureBytes } = keypair.sign(message); | ||
const signature = bs58.encode(signatureBytes); | ||
return { | ||
type: "action", | ||
payload, | ||
signature: bs58.encode(signature), | ||
session: bs58.encode(wallet.publicKey.toBytes()), | ||
signature, | ||
session: keypair.getPublicKey().toString(), | ||
}; | ||
@@ -81,0 +67,0 @@ } |
{ | ||
"name": "@canvas-js/chain-near", | ||
"version": "0.4.10", | ||
"version": "0.4.11", | ||
"type": "module", | ||
@@ -12,3 +12,3 @@ "author": "Canvas Technologies, Inc. (https://canvas.xyz)", | ||
"dependencies": { | ||
"@canvas-js/interfaces": "0.4.10", | ||
"@canvas-js/interfaces": "0.4.11", | ||
"bs58": "^5.0.0", | ||
@@ -15,0 +15,0 @@ "near-api-js": "^2.1.3", |
5758
6
122
+ Added@canvas-js/interfaces@0.4.11(transitive)
- Removed@canvas-js/interfaces@0.4.10(transitive)
Updated@canvas-js/interfaces@0.4.11